Facebook SDK使用Swift 3 iOS 10登录

rob*_*ton 10 facebook login ios swift swift3

我通过Cocoapods安装了Facebook的Swift SDK:

pod 'FacebookCore', :git => "https://github.com/facebook/facebook-sdk-swift"
pod 'FacebookLogin', :git => "https://github.com/facebook/facebook-sdk-swift"
Run Code Online (Sandbox Code Playgroud)

我已经按照Facebook的Swift SDK(https://developers.facebook.com/docs/swift/login)的登录说明进行操作,但无法使其正常工作.

在我的视图控制器中,我有以下内容:

@objc fileprivate func facebookSignIn() {
    let loginManager = LoginManager()
    print("LOGIN MANAGER: \(loginManager)")
    loginManager.logIn([ .publicProfile, .email ], viewController: self) { loginResult in
        print("LOGIN RESULT! \(loginResult)")
        switch loginResult {
        case .failed(let error):
            print("FACEBOOK LOGIN FAILED: \(error)")
        case .cancelled:
            print("User cancelled login.")
        case .success(let grantedPermissions, let declinedPermissions, let accessToken):
            print("Logged in!")
            print("GRANTED PERMISSIONS: \(grantedPermissions)")
            print("DECLINED PERMISSIONS: \(declinedPermissions)")
            print("ACCESS TOKEN \(accessToken)")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

"登录经理:等等" 单击按钮打开时打印.我可以通过Facebook登录,然后网页视图变为空白,没有任何反应.该应用程序显示在我的Facebook应用程序中,因此已经进行了一些授权,但是如果我在Web视图中单击"完成"按钮,则回调的结果是.cancelled"用户取消登录".打印出来.

我已将Keychain Sharing添加到我的权利中,并且我已更新了我的info.plist:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string> [ FB STRING ] </string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>[ APP ID ]</string>
<key>FacebookDisplayName</key>
<string>[ APP NAME ]</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>
Run Code Online (Sandbox Code Playgroud)

我甚至改变了我的NSAppTransportSecurity设置.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

我知道这是iOS 9 Facebook登录模拟器的重复--canOpenURL:URL失败:"fbauth2:///" - 错误:"(null)"以及如何在iOS 10上使用Facebook iOS SDK但没有解决方案似乎对我有用,除非我错过了什么?任何建议表示赞赏.

还会打印几个错误:

[App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction


[error] error: (6922) I/O error for database at /var/mobile/Containers/Data/Application/F3D8E126-B0CC-4C06-81A5-D7A7F849B3BD/Documents/OfflineModel2.sqlite.  SQLite error code:6922, 'disk I/O error'
Run Code Online (Sandbox Code Playgroud)

当我没有在我的设备上安装FB应用程序时,我得到:

-canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
Run Code Online (Sandbox Code Playgroud)

小智 21

你必须用下面的didfinishlaunching和openurl AppDelegate方法替换:

public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool 
{
    return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}

public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool 
{
    return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
Run Code Online (Sandbox Code Playgroud)

  • @ifedapoolarewaju在这种情况下,您应该从前缀为FBSDK的模块中将导入更改为FacebookCore和FacebookLogin.据我所知,这些都是旧版本,但Swift模块依赖于它们,即使用Pods将它们拉下来. (3认同)

Pan*_*včí 21

Swift 3和Swift 4

将此添加到您的AppDelegate.swift

import FBSDKCoreKit
import FBSDKLoginKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
   return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
   return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
}
Run Code Online (Sandbox Code Playgroud)


Tho*_*eza 9

不推荐使用FBSDK库.较新的库可以在Facebook的Github存储库中找到.

对于此特定错误,请实现以下应用程序方法.

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    return SDKApplicationDelegate.shared.application(app, open: url, options: options)

}
Run Code Online (Sandbox Code Playgroud)


Dav*_*ord 5

Swift 3.0

在您的AppDelete.swift上:

import FBSDKCoreKit
import FBSDKLoginKit
Run Code Online (Sandbox Code Playgroud)

在你的内心:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
Run Code Online (Sandbox Code Playgroud)

加:

FBSDKApplicationDelegate .sharedInstance() .application(application, didFinishLaunchingWithOptions: launchOptions)
Run Code Online (Sandbox Code Playgroud)

然后创建一个新的func,如下所示:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
}
Run Code Online (Sandbox Code Playgroud)