0

1- i try to create framework for my project but i didnt get true way to call my methods in another project inside appdelegate;

2- Using framework in another project App Transport Security warning !

my example framework codes under below;

myFramework.swift

import UIKit

var loginUrl = "http://bla/login.php"
let prefs = UserDefaults.standard


public class myFramework: NSObject {

    public override init (){
        print("Started.")
    }


    public func doSomething(){
    print("works")
    }


    public func login(secret : String)
    {
        print("Login Request")
        let post_data: NSDictionary = NSMutableDictionary()

        post_data.setValue(secret, forKey: "secret")

        let url:URL = URL(string: loginUrl)!
        let session = URLSession.shared

        let request = NSMutableURLRequest(url: url)
        request.httpMethod = "POST"
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
        var paramString = ""

        for (key, value) in post_data
        {
            paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
        }
        request.httpBody = paramString.data(using: String.Encoding.utf8)


        let task = session.dataTask(with: request as URLRequest, completionHandler: {
            (
            data, response, error) in

            guard let _:Data = data, let _:URLResponse = response  , error == nil else {
                return
            }
             let json: Any?
            do
            {
                json = try JSONSerialization.jsonObject(with: data!, options: [])
            }
            catch
            {
                return
            }
            guard let server_response = json as? NSDictionary else
            {
                return
            }

            if let data_block = server_response["login"] as? NSDictionary
            {
                if let checksuccess = data_block["success"] as? Bool
                {
                    if checksuccess == true {

                        if let getUserId = data_block["userId"] {

                        print("Service Connected")
                        print(getUserId)

                        prefs.set(secret, forKey: "secret")
                        prefs.set(getUserId, forKey: "userId")

                        }

                    }else{

                        if let getMessage = data_block["message"] {

                         print(getMessage)
                        }
                     }

                    DispatchQueue.main.async(execute: self.LoginDone)
                }
            }

         })

        task.resume()


    }

   public func LoginDone()
    {

    }

}

Calling another project inside Appdelegate file.

import myFramework

  myFramework().login(secret : "234234234234")

but i want to use `myframework without ()

must be;

 myFramework.login(secret : "234234234234")

1- How can i do it?

(My all framework codes inside myFramework.swift)

2- When my framework using another project says me app App Transport Security warning , how can i fix it in my framework ? Warning message under below.

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file

Thank you !

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85

2 Answers2

1

You don't need to put your functions inside a public class. Just make them public.

public func login() {
    // insert code here
}

public func secondFunction() {
    internalFunction()
}

internal func internalFunction() {

}

In your app:

include MyFramework

login()
secondFunction()
  • Note that you should name your frameworks like you do classes - capitalized.
  • You don't need the prefix your function calls with MyFramework.
  • Your app can see login() and secondFunction(), but cannot see internalFunction() as it's declared internal.

EDIT: Just saw your second question. Click on your framework target in the app explorer. Under General, DeploymentInfo, you'll see a checkbox labelled Allow App Extension API Only - check it. (I make this mistake often too!)

1

In regards to your first question, if you are set on scoping your functions inside of a class, make your functions static or class functions like so:

class myFramework: NSObject {

    public class func login(secret : String)
    {
        ...
    }

}

Now in code, you can call it like this:

import myFramework

myFramework.login("234234234234")

For your second question, see this post:

App Transport Security has blocked a cleartext HTTP resource

Community
  • 1
  • 1