FaceBook分享其實(shí)很好做宜肉, 可以直接通過(guò)pod集成sdk:
pod 'FBSDKCoreKit'
pod 'FBSDKShareKit'
這里要注意橘霎, pod時(shí)要記得在加上 use_frameworks!
荆针,如下:
platform :ios, '9.0'
use_frameworks!
target 'XXX' do
集成sdk之后腌且, 在info.plist文件中添加以下代碼:
<key>FacebookAppID</key>
<string>你的app編號(hào)</string>
<key>FacebookDisplayName</key>
<string>你的app名字</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>facebook</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb你的app編號(hào)</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>
在Appdelegate.m中, 導(dǎo)入#import <FBSDKCoreKit/FBSDKCoreKit.h>
梗肝,進(jìn)行如下的配置:
- (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;
}
配置完成, 就可以在進(jìn)行分享的地方處理邏輯了铺董。
Facebook的分享有兩種方式:
一種是直接使用他自帶的分享按鈕FBSDKShareButton
巫击,
一種是使用對(duì)話框分享FBSDKShareDialog
如果你們有UI設(shè)計(jì)按鈕樣式, 建議使用FBSDKShareDialog
精续。 不論哪一種分享方式坝锰, 都要先創(chuàng)建一個(gè)分享的內(nèi)容對(duì)象FBSDKShareLinkContent
:
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc]init];
content.contentURL = [NSURL URLWithString:self.share_url];
在之前的版本, FBSDKShareLinkContent 還可以設(shè)置屬性contentDescription
重付、contentTitle
什黑、imageURL
等, 但我更新后看了看這三個(gè)參數(shù)都已經(jīng)改為只讀屬性堪夭, 不可再進(jìn)行設(shè)置, 所以我們可以用quote
對(duì)這個(gè)分享鏈接進(jìn)行描述:
content.quote = shareText;
// 以下三種類型分享可進(jìn)行參考拣凹,導(dǎo)入其對(duì)應(yīng)的頭文件即可
// 分享圖片:
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.image = image; photo.userGenerated = YES;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = @[photo];
// 分享視頻:
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoURL;
FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
content.video = video;
// 混合分享:
FBSDKSharePhoto *photo = [FBSDKSharePhoto photoWith...
FBSDKShareVideo *video = [FBSDKShareVideo videoWith...
FBSDKShareMediaContent *content = [FBSDKShareMediaContent new];
content.media = @[photo, video];
分享的內(nèi)容對(duì)象創(chuàng)建完成后森爽, 就可以直接調(diào)用對(duì)話框進(jìn)行分享跳轉(zhuǎn)了, 直接打開facebook網(wǎng)頁(yè)進(jìn)行登錄測(cè)試:
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc]init];
content.contentURL = [NSURL URLWithString:self.share_url];
content.quote = shareText;
[FBSDKShareDialog showFromViewController:self withContent:content delegate:self];
實(shí)現(xiàn)FBSDKShareDialog
的代理:<FBSDKSharingDelegate>
:
#pragma mark - FBSDKSharingDelegate
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results{
DLog(@"--results:%@",results);
[MBProgressHUD showError:@"分享成功"];
[self anchorShareSucess];
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error{
DLog(@"--error");
[MBProgressHUD showError:@"分享失敗"];
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer{
DLog(@"--cancel");
}
以上嚣镜, 即可簡(jiǎn)單的實(shí)現(xiàn)facebook分享功能爬迟,更多可查看Facebook集成分享(記得找個(gè)提子出去看)