美文网首页
IOS 原生通过Facebook 进行应用邀请

IOS 原生通过Facebook 进行应用邀请

作者: 山_里_娃 | 来源:发表于2018-07-05 13:39 被阅读51次

需求:

通过IOS系统原生的分享集成Facebook 的应用邀请 功能。

前期准备:

配置Facebook分享的一些必要库到自己的工程,详细教程可以看看facebook官网
https://developers.facebook.com/docs/sharing/ios 国内用户需要翻墙哦!

下面介绍下必要的步骤供大家参考:
1.在Facebook 官网注册并关联你的app 具体地址:https://developers.facebook.com/docs/ios/getting-started/
2.下载并导入必要的.framework文件

WX20180705-104643@2x.png

3.配置你的项目.plist文件 看下几个关键点


image.png

4.在你的AppDelegate.m中加入以下代码:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];

    // Add any custom logic here.
    return YES;
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    
    BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                  openURL:url
                                                        sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                               annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
                    ];
    // Add any custom logic here.
    return handled;
}

-(void)applicationDidBecomeActive:(UIApplication*)application
{
    [FBSDKAppEvents activateApp];
}

5.关键的一步 通过UIActivityViewController发起分享 看下参考代码:

- (void)showActivityViewController {
    NSString *textToShare = @"logo";
    UIImage* imageShare = [UIImage imageNamed:@""];
    NSArray *activityItems = @[textToShare, imageShare];
    
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    activityViewController.excludedActivityTypes = @[UIActivityTypeAirDrop];
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {//适配ipad
        [activityViewController setModalPresentationStyle:UIModalPresentationFormSheet];
        
        UIPopoverPresentationController *popPresenter = [activityViewController popoverPresentationController];
        popPresenter.sourceView = self.view;
        popPresenter.sourceRect = self.view.bounds;
        popPresenter.permittedArrowDirections = 0;
    }
    
    [self presentViewController:activityViewController animated:YES completion:nil];
    //分享结果回调方法
    UIActivityViewControllerCompletionHandler myblock = ^(NSString *type,BOOL completed){
        //new
        NSLog(@"%d %@",completed,type);
        if ([type isEqualToString:@"com.apple.UIKit.activity.PostToFacebook"]) {
            [self inviteFBFriends];
        }
        
    };
    activityViewController.completionHandler = myblock;
    
}

//new
-(void)inviteFBFriends
{
    if (![FBSDKAccessToken currentAccessToken])
    {
        FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
        
        [login logInWithReadPermissions:@[@"public_profile",@"email", @"user_photos"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
            
            if (error) {
                // Process error
                
            } else if (result.isCancelled) {
                // Handle cancellations
                
            } else if([FBSDKAccessToken currentAccessToken]){
                [self shareLinkContent];
            }
        }];
        return;
    }else {
        [self shareLinkContent];
    }
    
}
//new
//分享链接
- (void)shareLinkContent {
    FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
    content.contentURL = [NSURL URLWithString:@"https://www.jianshu.com/"];
    [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];
}

5.特别注意的地方:


1530758597988.jpg

其他可能出现的问题补充:
1.IOS11上分享总是不成功可以参考这篇文章:https://www.jianshu.com/p/fe4aaa54288f
2.白名单问题参考https://developers.facebook.com/docs/ios/ios9

最后给大家推荐个不错的公众号 "说神码",或者大家可以扫描下面的二维码关注


qrcode_for_gh_3b0177133bdb_258.jpg

相关文章

网友评论

      本文标题:IOS 原生通过Facebook 进行应用邀请

      本文链接:https://www.haomeiwen.com/subject/eeguuftx.html