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 alloc] init];
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 alloc] init] assetForURL: [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 alloc] init];
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的