我想实现一个 swift3 函数,比如 facebook login api。这是 facebook 的示例代码:( https://developers.facebook.com/docs/swift/login )
@objc func loginButtonClicked() {
let loginManager = LoginManager()
loginManager.logIn([ .PublicProfile ], viewController: self) { loginResult in
switch loginResult {
case .Failed(let error):
print(error)
case .Cancelled:
print("User cancelled login.")
case .Success(let grantedPermissions, let declinedPermissions, let accessToken):
print("Logged in!")
}
}
请参阅函数“loginManager.logIn”在完成块中返回“loginResult”。并且登录结果应该是在oauth调用Appdelegate中的sdk函数之后创建的
public func application(_ app: UIApplication, open url: URL, options:
[UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
app,
open: url as URL!,
sourceApplication:
options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation]
)
}
public func application(_ application: UIApplication, open url: URL,
sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
application,
open: url as URL!,
sourceApplication: sourceApplication,
annotation: annotation)
}
我尝试在函数中使用 DispatchSemaphore 来等待结果。但它阻塞了我的主线程并使我的 SafariViewController 冻结。是否有更好的解决方案来实现像 facebook 这样的 swift3 功能?
非常感谢