Swift 2 解析 FacebookSDK iOS9

39

我正在尝试通过Facebook登录用户。在之前的Swift 1.2版本中,一切都运行得很完美,但迁移后,我似乎无法通过我的FB帐户登录,仍然会收到以下错误:

- canOpenURL:针对URL失败:"fbauth://authorize/?client_id=... 错误:"此应用程序不允许查询方案fbauth"

请问你能帮我解决这个问题吗?


为什么有这些负分? - kalafun
2
那些没有理由吗? - kalafun
iOS9 对查询方案添加了一些限制。我相信这是出于隐私方面的考虑 - 即使应用程序没有打算为给定的方案启动 URL,它们也可以看到您在手机上安装了什么。 - antsyawn
1
那么在这些框架更新之前,我就没有可能继续在iOS9上进行开发了吗? - kalafun
4个回答

34

从 WWDC 2015 的 "隐私与您的应用程序" 视频中,将以下内容添加到您的 info.plist 文件中:

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fbauth</string>
    </array>

尝试以下步骤,因为这对我起作用。在Xcode 7中创建一个新的Swift 2.0应用程序,用于iOS 9。 在AppDelegate.swift文件中,添加以下代码: let sharedApplication = UIApplication.sharedApplication(); let isFacebookAuthorized = sharedApplication.canOpenURL(NSURL(fileURLWithPath: "fbauth://authorize")); 放置在didFinishLaunchingWithOptions方法中。构建并运行应用程序。您应该会收到“不允许查询方案”的错误提示。接下来,以源代码模式打开Info.plist文件,并将代码插入到我的原始回复中,在</dict>前面。重新构建和运行应用程序。您应该不再收到错误提示。 - Mark A. Durham
可以在这里找到一个可工作的代码示例:https://github.com/gatzsche/LSApplicationQueriesSchemes-Working-Example - Awsed
10
遇到了相同的问题,在将此添加到 Info.plist 后,我收到了这个错误消息:“-canOpenURL: failed for URL: "fbauth://authorize/?client_id=…" - error: "(null)"”,看起来像是 FBSDK 出了一些问题。 - nebiros
@nebiros 我也遇到了同样的问题。唯一不同的是,在我的 plist 文件中添加了上述行之后,我得到了错误:“此应用程序不允许查询方案 fbauth”,但之前的错误仍然相同。-canOpenURL: failed for URL: "fbauth://authorize/?client_id=........ - Dimple Shah
如果您已将LSApplicationQueriesSchemes添加到info.plist中,则可以忽略那些错误消息(例如“此应用程序不允许查询方案...”)。这只是Xcode 7的一个错误,不会影响您与Facebook的集成。 - JSNoob
显示剩余4条评论

28

建议按照Facebook的指示,为iOS 9准备应用程序:

https://developers.facebook.com/docs/ios/ios9
  1. 下载最新的iOS Facebook SDK。

下载适合您的应用程序的Facebook SDK版本。

v4.x- 推荐使用。 v3.24.0- 仅在您尚未迁移到SDK v4.x的情况下使用。

2. 为网络请求白名单Facebook服务器。

如果您使用iOS SDK 9.0编译应用程序,您将受到应用传输安全的影响。 目前,您需要通过将以下内容添加到应用程序的plist中来白名单Facebook域名:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>facebook.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>                
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>fbcdn.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>akamaihd.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>

否则您将会遇到如下错误:

NSUnderlyingError=0x7f88f9436eb0 {Error Domain=kCFErrorDomainCFNetwork
Code=-1200 "An SSL error has occurred and a secure connection to
the server cannot be made." UserInfo={NSErrorFailingURLStringKey=
https://graph.facebook.com/v2.4, NSLocalizedRecoverySuggestion=
Would you like to connect to the server anyway?,
_kCFNetworkCFStreamSSLErrorOriginalValue=-9802,
kCFStreamPropertySSLPeerCertificates=<CFArray 0x7f88f9536e00
[0x10719f7c0]>{type = immutable, count = 2, values = (
0 : <cert(0x7f88f963f840) s: *.facebook.com (http://facebook.com/)
i: DigiCert High Assurance CA-3>
1 : <cert(0x7f88f96444c0) s: DigiCert High Assurance CA-3 i:
DigiCert High Assurance EV Root CA> )}, 
_kCFStreamPropertySSLClientCertificateState=0,
kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7f88f9644d10>,
NSLocalizedDescription=An SSL error has occurred and a secure connection
to the server cannot be made.,_kCFStreamErrorDomainKey=3,
NSErrorFailingURLKey=https://graph.facebook.com/v2.4,
_kCFStreamErrorCodeKey=-9802}}
  1. 白名单Facebook应用程序

如果您使用任何Facebook对话框(例如:登录、分享、应用邀请等),可以执行应用程序切换到Facebook应用程序,那么您需要更新应用程序的plist以处理canOpenURL的更改,有关详细信息,请参见https://developer.apple.com/videos/wwdc/2015/?id=703

如果您正在重新编译iOS SDK 9.0,则在应用程序的plist中添加以下内容,如果您使用的是SDK v4.5或更早版本:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fbapi20130214</string>
    <string>fbapi20130410</string>
    <string>fbapi20130702</string>
    <string>fbapi20131010</string>
    <string>fbapi20131219</string>    
    <string>fbapi20140410</string>
    <string>fbapi20140116</string>
    <string>fbapi20150313</string>
    <string>fbapi20150629</string>
    <string>fbauth</string>
    <string>fbauth2</string>
    <string>fb-messenger-api20140430</string>
</array>

如果您正在使用早于v4.6版本的FBSDKMessengerShareKit,请同时添加以下内容:

<string>fb-messenger-platform-20150128</string>
<string>fb-messenger-platform-20150218</string>
<string>fb-messenger-platform-20150305</string>

如果您正在使用4.6.0版本的SDK,您只需要添加:

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

这将使FacebookSDK集成能够正确识别已安装的Facebook应用程序以执行应用程序切换。如果您没有使用iOS SDK 9.0重新编译,则您的应用程序仅限于50个不同的方案(之后调用canOpenURL返回NO)。


我对这个响应中的 akamaihd.net 部分很好奇,因为它似乎与 Facebook 没有关联,于是我在谷歌上搜索并发现了大量与恶意软件和相关问题有关的信息。看起来这是 Facebook 使用的 CDN,但我们确定需要吗? - Mike
这个答案真的很棒,几乎涵盖了所有内容。 - Sruit A.Suk

5
请点击以下链接查看Facebook针对iOS 9的推荐更新:https://developers.facebook.com/docs/ios/ios9 为了修复这个特定的错误,
-canOpenURL: failed for URL: "fbauth://authorize/?client_id=... error: "This app is not allowed to query for scheme fbauth"

对于 Facebook SDK 4.6+ 版本,请打开您的 info.plist 文件并添加以下内容:

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

对于版本4.5或更早版本:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fbapi20130214</string>
    <string>fbapi20130410</string>
    <string>fbapi20130702</string>
    <string>fbapi20131010</string>
    <string>fbapi20131219</string>    
    <string>fbapi20140410</string>
    <string>fbapi20140116</string>
    <string>fbapi20150313</string>
    <string>fbapi20150629</string>
    <string>fbauth</string>
    <string>fbauth2</string>
    <string>fb-messenger-api20140430</string>
</array>

我正在运行<4.5,并且不得不添加<string>fb-messenger-platform-20150128</string>。换句话说,要添加来自我的错误消息的确切方案,该消息是“此应用程序不允许查询方案fb-messenger-platform-20150218”。 - Stan James

0

1] 在 Developer.facebook 上进行身份验证并生成 Facebook Id

2] 设置 Bitcode:从构建设置中选择“否”

3] 设置 Plist 文件

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb460481580805052</string>
        </array>
    </dict>
</array>

<key>FacebookAppID</key>
<string>460481580805052</string>

<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>akamaihd.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </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>
    </dict>
</dict>

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fbapi20130214</string>
    <string>fbapi20130410</string>
    <string>fbapi20130702</string>
    <string>fbapi20131010</string>
    <string>fbapi20131219</string>
    <string>fbapi20140410</string>
    <string>fbapi20140116</string>
    <string>fbapi20150313</string>
    <string>fbapi20150629</string>
    <string>fbauth</string>
    <string>fbauth2 </string>
    <string>fb-messenger-api20140430</string>
    <string>fb-messenger-platform-20150128</string>
    <string>fb-messenger-platform-20150218</string>
    <string>fb-messenger-platform-20150305</string>
</array>

4] 下载4个SDK框架,例如

=>Bolts.framework
=>FBSDKCoreKit.framework
=>FBSDKLoginKit.framework
=>FBSDKShareKit.framework

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接