【iOS开发】2020年最新FaceBook分享

2020-07-02  本文已影响0人  我是孙小白

FaceBook分享其实很好做, 可以直接通过pod集成sdk:

pod 'FBSDKCoreKit'
pod 'FBSDKShareKit'

这里要注意, pod时要记得在加上 use_frameworks!,如下:

platform :ios, '9.0'
use_frameworks!
target 'XXX' do

集成sdk之后, 在info.plist文件中添加以下代码:

<key>FacebookAppID</key>
<string>你的app编号</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编号</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中, 导入#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;
}

配置完成, 就可以在进行分享的地方处理逻辑了。

Facebook的分享有两种方式:
一种是直接使用他自带的分享按钮FBSDKShareButton
一种是使用对话框分享FBSDKShareDialog

如果你们有UI设计按钮样式, 建议使用FBSDKShareDialog。 不论哪一种分享方式, 都要先创建一个分享的内容对象FBSDKShareLinkContent

FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc]init];
content.contentURL = [NSURL URLWithString:self.share_url];

在之前的版本, FBSDKShareLinkContent 还可以设置属性contentDescriptioncontentTitleimageURL等, 但我更新后看了看这三个参数都已经改为只读属性, 不可再进行设置, 所以我们可以用quote对这个分享链接进行描述:

 content.quote = shareText;

// 以下三种类型分享可进行参考,导入其对应的头文件即可
// 分享图片:
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];

分享的内容对象创建完成后, 就可以直接调用对话框进行分享跳转了, 直接打开facebook网页进行登录测试:

FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc]init];
content.contentURL = [NSURL URLWithString:self.share_url];
content.quote = shareText;
    
[FBSDKShareDialog showFromViewController:self withContent:content delegate:self];

实现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");
}

以上, 即可简单的实现facebook分享功能,更多可查看Facebook集成分享(记得找个提子出去看)

上一篇下一篇

猜你喜欢

热点阅读