Facebook SDK 登录分享

Facebook SDK

typedef void (^FacebookLogin)();

- (void)facebookCompletion:(FacebookLogin)completion {

    [FBSDKProfileenableUpdatesOnAccessTokenChange:YES];

    FBSDKLoginManager *login = [[FBSDKLoginManageralloc]init];

    [login logInWithReadPermissions:@[@"public_profile"]

                 fromViewController:self

                            handler:^(FBSDKLoginManagerLoginResult *result,NSError *error) {

                                if (error) {

                                    DLog(@"Process error");

                                } elseif (result.isCancelled) {

                                    DLog(@"Cancelled");

                                } else {

                                    DLog(@"Logged in");

                                    completion();

                                }

                            }];

    [[[NSNotificationCenterdefaultCenter]rac_addObserverForName:FBSDKProfileDidChangeNotificationobject:nil]subscribeNext:^(id x) {

        FBSDKProfile *profile = [FBSDKProfilecurrentProfile];

   

    }];


}



- (void)shareWithFacebook {

    if (![FBSDKAccessTokencurrentAccessToken]) {

        [selffacebookCompletion:^{

           [selfshareWithFacebook];

        }];

        return;

    }

    if (![[FBSDKAccessTokencurrentAccessToken]hasGranted:@"publish_actions"]) {

        FBSDKLoginManager *login = [[FBSDKLoginManageralloc]init];

        [login logInWithPublishPermissions:@[@"publish_actions"]

                     fromViewController:navigationController

                                handler:^(FBSDKLoginManagerLoginResult *result,NSError *error) {

                                    if ([[FBSDKAccessTokencurrentAccessToken]hasGranted:@"publish_actions"]) {

                                        [selfshareWithFacebook];

                                    }

                                }];

        return;

    }

   

  if([self isFacebookInstalled] == NO) {

            FBSDKShareLinkContent *content = [[FBSDKShareLinkContent allocinit];

            content.contentTitle = @"name";

            content.contentDescription = @"shareContent";

            content.contentURL = [NSURL URLWithString:@"shareUrl"];

            content.imageURL = [NSURL URLWithString:@"imageUrl"];

            [FBSDKShareDialog showFromViewController:self

                                         withContent:content

                                            delegate:self];

            return;

        }

        __block NSURL *assetShareURL;      

  if ( [NSURL URLWithString:@"video"]) {

                [[[ALAssetsLibrary allocinitassetForURL: [NSURL URLWithString:@"video"] resultBlock:^(ALAsset *asset) {

                    assetShareURL =  [NSURL URLWithString:@"video"];

   [self shareVideo:assetShareURL];

                } failureBlock:^(NSError *error) {

                    

 NSString *videoPath = @"videoPath";

[assetsLibrarywriteVideoAtPathToSavedPhotosAlbum:[NSURLfileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL,NSError *error) {

assetShareURL = assetURL;

[self shareVideo:assetURL];

}];

                }];

                

            }

}


- (void)shareVideo:(NSURL *)assetShareURL {

FBSDKShareVideo *video = [FBSDKShareVideo videoWithVideoURL:assetShareURL];

                FBSDKShareVideoContent *content = [[FBSDKShareVideoContent allocinit];

                content.video = video;

                [FBSDKShareDialog showFromViewController:self

                                             withContent:content

                                                delegate:self];

}

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {

    NSLog(@"%@",results);

}


- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {

    NSLog(@"%@",error);

}


- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {

    NSLog(@"%@",sharer);

}



遇到的一些问题:

登录 error code 308 

通过更新SDK解决

FBSDKShareVideo 以及 FBSDKSharePhoto 只支持本地安装了facebook App的


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值