[swift]ios9 facebook 登录问题

标签: ios Facebook Swift
发布时间: 2017/3/6 23:16:45
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

在我的应用程序我集成登录 facebook,并把所需的所有设置。但当我试着登录,我收到这个错误

-canOpenURL︰ 失败,URL:"fbauth2: /"-错误:"(null)"

但我把

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>

在.plist 文件中,有什么发现问题一用,可以吗?

编辑

 self.loginButton.delegate = self;
        if (FBSDKAccessToken.currentAccessToken() != nil)
        {
            // User is already logged in, do work such as go to next view controller.
        }
        else
        {

            loginButton.center = self.view.center
            loginButton.readPermissions = ["public_profile", "email", "user_friends"]
            loginButton.delegate = self
        }


    //MARK : facebook integration
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

        print(result);
        if ((error) != nil)
        {
            // Process error
            self.showCancelAlert("Error", message: "Error occured")
        }
        else if result.isCancelled {
            // Handle cancellations
        }
        else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if result.grantedPermissions.contains("email")
            {
                // Do work
                self.returnUserData();

            }else{
                self.showCancelAlert("Error", message: "Cannot process with your email address")

            }
        }
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {

    }

    // accessToken is your Facebook id
    func returnUserProfileImage(accessToken: NSString) -> String
    {
        let userID = accessToken as NSString
        let facebookProfileUrl = NSURL(string: "http://graph.facebook.com/\(userID)/picture?type=large")

        if facebookProfileUrl?.absoluteString != nil{
            return (facebookProfileUrl?.absoluteString)!;
        }else{
            return "";
        }

    }




    func returnUserData()
    {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil)
            {
                // Process error
                 print("error \(error)")

            }
            else
            {
                let userName : NSString = result.valueForKey("name") as! NSString

                 UserDefaults.sharedInstace.setUsername(userName as String)

                if result.valueForKey("email") != nil{

                    let userEmail : NSString = result.valueForKey("email") as! NSString

                    if let id: NSString = result.valueForKey("id") as? NSString {
                        print("ID is: \(id)")

                        self.showLoading("Signin...")
                        WebClient.sharedInstace.socialLogin(SocialLogin(uid:String(id) , name: userName as String, imageUrl: self.returnUserProfileImage(id), email: userEmail as String, provider: "facebook"))

                    } else {
                        print("ID es null")

                        self.showCancelAlert("Error", message: "Cannot process with your email address")

                        self.loginButtonDidLogOut(self.loginButton)
                    }
                }else{

                    FBSDKLoginManager().logOut()

                    self.loginButtonDidLogOut(self.loginButton)

                    print("email es null")

                    self.showCancelAlert("Error", message: "Cannot process with your email address")

                }

            }
        })
    }

解决方法 1:

我想有一对夫妇的事情可能导致您的问题。

1) 是你确定你添加的最新的 FBSDKCoreKit 和 FBSDKLoginKit,都将这些行添加到顶部的你 swift 文件︰

import FBSDKCoreKit
import FBSDKLoginKit

2) 我读过,这是在模拟器上的只是一个错误,应该被忽略。 请确保您在测试此功能的实际设备上。

3) 有你包括 FBSDKLoginButtonDelegate 在类声明的标题吗?

4) 我会确保你 info.plist 源代码是所有包含和正确。 下面是我为 Facebook 已经添加的在我的应用程序︰

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string> fb + FACEBOOKAPPIDFROMTHEFACEBOOKDEVCONSOLE </string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>FACEBOOKAPPIDFROMTHEFACEBOOKDEVCONSOLE</string>
<key>FacebookDisplayName</key>
<string>NAME OF YOUR APP</string>

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>facebook.com</key>
        <dict>
            <key>NSIncludesSubdomains</key> <true />
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
        </dict>
        <key>fbcdn.net</key>
        <dict>
            <key>NSIncludesSubdomains</key> <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
        </dict>
        <key>akamaihd.net</key>
        <dict>
            <key>NSIncludesSubdomains</key> <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> <false/>
        </dict>
    </dict>
</dict>
赞助商