ios - 如何在 swift 2.0 中使用 facebook 登录

标签 ios swift

我使用 facebook 应用程序创建了一个登录名。起初我转到我的个人资料并创建了 App ID 并添加了捆绑标识符。所以,我简单地复制并粘贴到在 facebook 上使用 App ID 和 Bundle Identifier 创建的 info.plist 中。然后我添加了 FBSDKCoreKit.frameworkFBSDKLoginKit。框架。我添加了一些代码。问题是,Facebook 登录成功。但是当我按下下面的按钮时,会打印错误,并且不会调用 FBSDKLoginButtonDelegate 函数。我的系统是联想 B460e 上的 Hackintosh Yosemite。请参阅代码中的单行注释以供引用。提前谢谢你。

错误:

2016-08-17 19:51:40.736 Facebook[925:34154] -canOpenURL:URL 失败:“fbauth2:/”- 错误:“(null)” 2016-08-17 19:51:40.808 Facebook[925:34154] -canOpenURL:URL 失败:“fbauth2:/”- 错误:“(null)”

我试过的代码

信息.plist

enter image description here

这是我的 AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    return true
}

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
func applicationWillResignActive(application: UIApplication) {

}

func applicationDidEnterBackground(application: UIApplication) {

}

func applicationWillEnterForeground(application: UIApplication) {
}

func applicationDidBecomeActive(application: UIApplication) {

    FBSDKAppEvents.activateApp()
}

func applicationWillTerminate(application: UIApplication) {

}


}

这是我的 View Controller.swift

import UIKit

class ViewController: UIViewController,FBSDKLoginButtonDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    let loginButton: FBSDKLoginButton = FBSDKLoginButton()
    loginButton.center = self.view.center
    self.view!.addSubview(loginButton)

    if (FBSDKAccessToken.currentAccessToken() != nil) {

        print("Hello World!")
        self.fetchProfile() //This function called and name,Id printed when the application opens initially that already logged in.
    }

}

func fetchProfile()
{
    print("Profile Fetched success fully")

    let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,name"], tokenString: "accessToken.tokenString", version: nil, HTTPMethod: "GET")
    req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
        if(error == nil)
        {
            print("result \(result)")
        }
        else
        {
            print("error \(error)")
        }
    })

}


func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

    print("Hola") //This function not called when got name and ID
}

func loginButtonWillLogin(loginButton: FBSDKLoginButton!) -> Bool {

    print("Hello") //This function not called when i log in
    return true
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {

    print("Ni Hao") //This function not called when i log out

}

最佳答案

这与您的应用程序的应用程序传输安全设置有关。 Apple 最近为任意 http 加载添加了额外的安全措施。请尝试以下方法:

第 1 步:转到您的 info.plist 文件

第 2 步:添加新 key :“App Transport Security Settings”

第 3 步:添加一个 bool 子项“允许任意加载”,将其设置为 YES

第 4 步:添加字典子项“Exception Domains”

第 5 步:将字符串子项添加到“异常域”,其中包含导致错误的 url(在本例中为 fbauth2:/)

最后,它应该是这样的(当然还有你的 url)

enter image description here

作为旁注,Facebook SDK 声明您的应用程序应该仍然可以工作,无论此警告如何(因为您已将正确的条目添加到 LSApplicationQueriesScheme)。您确定将 FB 登录按钮的委托(delegate)分配给接收 Controller 吗?

关于ios - 如何在 swift 2.0 中使用 facebook 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38998195/

相关文章:

swift - iOS swift 3 如何更改 switch 语句中的类型

ios - TouchID - 检测添加的新指纹 - evaluatedPolicyDomainState 何时更改?

ios - 从 Swift 中的字典数组返回特定​​字典

swift : Sorting three arrays based on a particular array

objective-c - KVO 或如何在 Swift 中监听来自外部的属性更改

swift - 以编程方式填充 STPPaymentCardTextField

ios - 如何将数据传递给嵌套 Controller (UIContainerView)?

ios - 周围有尖锐阴影的文字

objective-c - 用[x] [y]访问我的malloc二维数组

ios - 在整洁的架构中使用 NSFetchedResultsController