一损合、創(chuàng)建Facebook的應(yīng)用
https://developers.facebook.com
1爹殊、點擊我的應(yīng)用
2构挤、點擊創(chuàng)建應(yīng)用
3、選擇應(yīng)用類型
4、填寫應(yīng)用名稱妹沙,聯(lián)系郵箱
5、填寫基本信息熟吏,隱私權(quán)政策網(wǎng)址距糖,數(shù)據(jù)刪除回調(diào)地址等
6、添加平臺牵寺,是iOS的要選擇iOS悍引,安卓的選擇安卓平臺
7、添加成功后帽氓,iOS的需要把Bundle id填寫進去趣斤,必需要和項目里的填寫一致,然后保存黎休,并且應(yīng)用模式的開關(guān)需要打開
二浓领、項目配置
1、項目中通過pod集成SDK势腮,手動集成也可以
pod 'FBSDKCoreKit'
pod 'FBSDKShareKit'
2联贩、在info.plist文件中添加以下代碼:
<key>FacebookAppID</key>
<string>應(yīng)用編號</string>
<key>FacebookClientToken</key>
<string>應(yīng)用密鑰</string>
<key>FacebookDisplayName</key>
<string>app名稱</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.clatia.plamclati</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb應(yīng)用編號</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
3、在Appdelegate中捎拯,導(dǎo)入#import <FBSDKCoreKit/FBSDKCoreKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{
BOOL result = [[FBSDKApplicationDelegate sharedInstance] application:app openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
return result;
}
4泪幌、在進行分享的地方處理邏輯
FBSDKShareLinkContent*linkContent = [[FBSDKShareLinkContent alloc]init];
linkContent.contentURL= [NSURL URLWithString:@"分享鏈接"];
linkContent.quote = @"描述信息";
FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc]init];
// 需要指定模式,否則會調(diào)起web分享
shareDialog.mode = FBSDKShareDialogModeNative;
if (![shareDialog canShow]) {
shareDialog.mode = FBSDKShareDialogModeWeb;
}
shareDialog.shareContent = linkContent;
shareDialog.delegate = self;
shareDialog.fromViewController = self;
[shareDialog show];
5署照、實現(xiàn)代理方法
分享后代理方法總是回調(diào)到sharerDidCancel祸泪,有知道原因和解決方法的小伙伴麻煩告知下
#pragma mark - FBSDKSharingDelegate
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary<NSString *,id> *)results {
NSString *postId = results[@"postId"];
FBSDKShareDialog *dialog = (FBSDKShareDialog *)sharer;
if (dialog.mode == FBSDKShareDialogModeBrowser && (postId == nil || [postId isEqualToString:@""])) {
// 如果使用webview分享的,但postId是空的建芙,
// 這種情況是用戶點擊了『完成』按鈕没隘,并沒有真的分享
NSLog(@"Cancel");
} else {
NSLog(@"Success");
}
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
FBSDKShareDialog *dialog = (FBSDKShareDialog *)sharer;
if (error == nil && dialog.mode == FBSDKShareDialogModeNative) {
// 如果使用原生登錄失敗,但error為空禁荸,那是因為用戶沒有安裝Facebook app
// 重設(shè)dialog的mode升略,再次彈出對話框
dialog.mode = FBSDKShareDialogModeBrowser;
[dialog show];
} else {
NSLog(@"Failure");
}
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {
NSLog(@"Cancel");
}