1-我尝试为我的项目创建framework
,但我没有真正的方法在appdelegate
内的另一个项目中调用我的方法;
2-在另一个项目App Transport Security警告中使用framework
!
我的示例framework
代码如下;
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()
{
}
}
在Appdelegate
文件中调用另一个项目。
import myFramework
myFramework().login(secret : "234234234234")
但我想使用`myframework without ()
必须是;
myFramework.login(secret : "234234234234")
1-我该怎么办?
(我在myFramework.swift中的所有框架代码)
2-当我使用另一个项目的框架说我app app Transport Security警告时,如何在我的框架中修复它?下面的警告信息。
App Transport Security已阻止明文HTTP(http://)资源加载,因为它不安全。可以通过应用程序的Info.plist文件配置临时例外
谢谢!
答案 0 :(得分:1)
您不需要将您的功能置于公共课程中。把它们公之于众。
public func login() {
// insert code here
}
public func secondFunction() {
internalFunction()
}
internal func internalFunction() {
}
在你的应用中:
include MyFramework
login()
secondFunction()
答案 1 :(得分:1)
关于你的第一个问题,如果你在一个类中设置了函数的范围,那么使你的函数成为静态或类函数如下:
class myFramework: NSObject {
public class func login(secret : String)
{
...
}
}
现在在代码中,您可以这样调用它:
import myFramework
myFramework.login("234234234234")
关于你的第二个问题,请看这篇文章:
App Transport Security has blocked a cleartext HTTP resource