使用ShareSDK完成Facebook第三方登录和Facebook分享时没办法跳转到Facebook应用

时间:2024-03-22 08:05:43

楼主是通过cocoapod接入ShareSDK, 后来发现无论是使用fb分享还是登录, 都是跳出了网页认证(即使我的手机有安装了fb)

后来mob的技术客服小哥告诉我在构造分享参数的时候, 执行参数字典的SSDKEnableUseClientShare方法, 也就是

NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"Rinpe"
images:nil
url:[NSURL URLWithString:@"http://www.baidu.com"]
title:@"Rinpe"
type:SSDKContentTypeAuto];
[shareParams SSDKEnableUseClientShare];

然后登录呢?由于一开始我在初始化ShareSDK的时候(也就是下面的方法)

+ (void)registerApp:(NSString *)appKey
activePlatforms:(NSArray *)activePlatforms
onImport:(SSDKImportHandler)importHandler
onConfiguration:(SSDKConfigurationHandler)configurationHandler;

在onImport这个block里面需要连接到fb的SDK, 但是我找了半天没找到类似下面这样的方法

[ShareSDKConnector connectFacebook:[xxxx class]];

然后Mob客服的小哥说要用

[ShareSDKConnector connectFacebookMessenger:[FBSDKMessengerSharer class]];

然后我又找了半天没有找到包含FBSDKMessengerSharer这个类的文件, 小哥就说使用pod 'ShareSDK3/ShareSDKPlatforms/FacebookMessengerSDK'

来集成这个SDK, 然而我不知道是我的原因还是他们的原因, pod update老是报错...

迫于无奈, 我自己找到了FBSDKMessengerShareKit, 也就是使用pod 'FBSDKMessengerShareKit'来集成这个SDK...

好吧, 上面的问题解决了..结果一运行...

-canOpenURL: failed for URL: "fbauth2://authorize?client_id=xxxxx 出现了这个错误...

好吧..最后我找到了一个网站...

http://discoverpioneer.com/blog/2015/09/18/updating-facebook-integration-for-ios-9/

按上面的步骤给info.plist添加相应的参数...

问题终于解决了...

原文如下:

Updating Facebook Integration for iOS 9

使用ShareSDK完成Facebook第三方登录和Facebook分享时没办法跳转到Facebook应用

So with iOS 9 out, there are a few enhancements in security. With that being said, the way your app integrates Facebook may be acting strange, or not working at all.

More than likely this is because you are being affected by App Transport Security. ‘App Transport Security is a feature that improves the security of connections between an app and web services. The feature consists of default connection requirements that conform to best practices for secure connections. Apps can override this default behavior and turn off transport security. ‘ -Apple

With all this being said, this is definitely something that you want to keep inside of your app. But now…. how do you make it work!?!

There are two solutions

1.

First: Whitelist Facebook Servers for Network Requests.
To do this add the following to your info.plist file:

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

Then, Whitelist Facebook Apps by adding this to your info.plist as well:

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

There you go, that should do the trick 使用ShareSDK完成Facebook第三方登录和Facebook分享时没办法跳转到Facebook应用

2.

If this seems to be too much of a hassle for you, the second option is to turn off App Transport Security. You can do that by adding the following to your info.plist file:

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>