我正在我的应用中使用 Facebook 登录。我有 pod 来导入依赖项。我在 pod 文件中使用了 pod FBSDKCoreKit
和 pod FBSDKLoginKit
。
我的代码是
//应用程序委托(delegate)
import FBSDKCoreKit
import FBSDKLoginKi
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//Realm migration
realmMigration()
window = UIWindow(frame: UIScreen.main.bounds)
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.lightGray
navigationBarAppearace.barTintColor = UIColor.darkGray
let landingViewObj = LandingView(nibName: (UIDevice.current.userInterfaceIdiom == .pad ? "LandingView" : "LandingView"), bundle: nil)
navigationController = UINavigationController(rootViewController: landingViewObj)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
IQKeyboardManager.sharedManager().enable = true
FIRApp.configure()
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any])
-> Bool
{
let facebookDidHandle = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: "UIApplicationOpenURLOptionsKey", annotation: [:])
let googleDidHandle = GIDSignIn.sharedInstance().handle(url,
sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
return googleDidHandle || facebookDidHandle
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
{
let facebookDidHandle = FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)
let googleDidHandle = GIDSignIn.sharedInstance().handle(url,
sourceApplication: sourceApplication,
annotation: annotation)
return googleDidHandle || facebookDidHandle
}
在我的 View Controller 中,
import FBSDKCoreKit
import FBSDKLoginKit
在我的 Facebook 登录自定义按钮操作中,
@IBAction func facebookSigninAction(_ sender: Any)
{
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name,email, picture.type(large)"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil)
{
print("Error: \(error)")
}
else
{
let data:[String:AnyObject] = result as! [String : AnyObject]
print(data)
}
})
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError?)
{
if let error = error
{
print(error.localizedDescription)
return
}
// ...
}
在 plist 文件中我添加了以下内容
<key>FacebookAppID</key>
<string>349152685470803</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbauth</string>
<string>fbauth2</string>
<string>fb</string>
<string>fb-messenger-api20140430</string>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>facebook</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb349152685470803</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.316518738586-d36rr39tpllb40056330jrra12v1tn86</string>
</array>
</dict>
</array>
我的问题是我的日志中出现以下错误,并且应用程序没有调用我可以获取结果的闭包。
2017-01-11 17:10:21.047 MyApp[7525:1384017] -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
还有其他方法可以请求权限并读取结果对象吗?
最佳答案
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>*** Add BundleId</string>
<key>CFBundleURLSchemes</key>
<array>
<string> fb349152685470803 </string>
</array>
关于ios - Facebook 登录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41590288/