ios - Facebook 登录问题

标签 ios swift swift3 facebook-ios-sdk

我正在我的应用中使用 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)"

还有其他方法可以请求权限并读取结果对象吗?

最佳答案

enter image description here您需要像这样添加 bundle ID。

<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/

相关文章:

ios - 后台运行3分钟后闹钟不响

ios - 试图让用户保存他们的个人资料 img。但我一直收到无法无参数调用 'user?' 的消息。

ios - Xcode 8.3.2 构建脚本 - 指定的应用程序不存在或不是捆绑目录

swift - 快速显示动态范围的循环

ios - Spotify 播放列表 ios sdk swift 问题

ios - 快速搜索栏 Controller

iOS:在 UIBarButtonItem 和 Button 中附加带有标题的 FontAwesome 图标

objective-c - 在当前 View 中访问 subview

iphone - 如何更改 NSURLConnection 的优先级?

ios - 如何在按下按钮后将 View 中的特定信息添加到警报消息?