Skip to content

Instantly share code, notes, and snippets.

@pepicrft
Created February 22, 2015 10:42
Show Gist options
  • Select an option

  • Save pepicrft/1d7bef8d28d977b64332 to your computer and use it in GitHub Desktop.

Select an option

Save pepicrft/1d7bef8d28d977b64332 to your computer and use it in GitHub Desktop.
Facebook Share
+ (void)shareUsingFeedDialog:(NSString *)url completionBlock:(void (^)(NSError *))completion
{
void (^shareBlock)() = ^void() {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"http://8fit.com/workouts", @"link", nil];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
DDLogError(@"Error sharing using feed dialog");
if (completion) {
completion(error);
}
[ProgressHUD showError:[NSLocalizedString(@"error sharing", nil) capitalizedString]];
}
else {
if (result == FBWebDialogResultDialogNotCompleted) {
DDLogError(@"User did cancel sharing using feed dialog");
if (completion) {
completion(nil);
}
}
else {
// Handle the publish feed callback
NSDictionary *urlParams = [FacebookSharingController parseURLParams:[resultURL query]];
if (![urlParams valueForKey:@"post_id"]) {
// User cancelled.
DDLogError(@"User did cancel sharing using feed dialog");
if (completion) {
completion(nil);
}
}
else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: @"Posted story, id: %@", [urlParams valueForKey:@"post_id"]];
DDLogError(@"User did share using feed dialog. %@", result);
if (completion) {
completion(nil);
}
[ProgressHUD showSuccess:[NSLocalizedString(@"post sent", nil) capitalizedString]];
}
}
}
}];
};
if (![FBSession activeSession] || ![[FBSession activeSession] isOpen]) {
[FBSession openActiveSessionWithReadPermissions:[FacebookLoginController @[@"email", @"user_friends", @"public_profile"]] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
shareBlock();
}];
}
else {
shareBlock();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment