简体   繁体   中英

Sharing content with facebook sdk 4.0 + iOS

I use the Facebook SDK on iOS (objective-c) to share an image on Facebook. Here's the code that I use:

-(IBAction)facebookShare:(UIButton *)sender {
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.image = self.image;
photo.userGenerated = YES;
FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = @[photo];
[FBSDKShareDialog showFromViewController:self withContent:content delegate:self];  
}

Where image is an UIImage that I set by picking it from the user's gallery. When I try to press my button (with this IBAction) the app crashes giving this error:

Jul 1 10:50:23 .local pkd[917]: error retrieving entitlements for pid 922 Jul 1 10:50:23 -[MainViewController sharer:didFailWithError:]: unrecognized selector sent to instance 0x7fd70940efd0 Jul 1 10:50:23: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController sharer:didFailWithError:]: unrecognized selector sent to instance 0x7fd70940efd0'

here's the new error after fixing the code in this way

FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
        photo.image = //YOUR IMAGE                                                                 photo.userGenerated = YES;
        FBSDKSharePhotoContent * photoContent = [[FBSDKSharePhotoContent alloc] init];
        photoContent.photos = @[photo];
        FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init];
        shareDialog.shareContent = photoContent;
        shareDialog.delegate = (id)self;
        shareDialog.fromViewController = self;
        NSError * error = nil;
        BOOL validation = [shareDialog validateWithError:&error];
        if (validation) {
            [shareDialog show];
        }

:

ul 1 15:55:02 .local Dailypic[1169]: libMobileGestalt MobileGestalt.c:1025: Could not retrieve region info Jul 1 15:55:06 com.apple.CoreSimulator.SimDevice.D482DFDD-3051-4759-B70D-94EC93BFF5D0.launchd_sim[471] (com.apple.imfoundation.IMRemoteURLConnectionAgent): The _DirtyJetsamMemoryLimit key is not available on this platform. Jul 1 15:55:07 o.local pkd[498]: error retrieving entitlements for pid 1169 Jul 1 15:55:07 local Dailypic[1169]: Error Domain=com.facebook.sdk.share Code=2 "The operation couldn't be completed. (com.facebook.sdk.share error 2.)" UserInfo=0x7ffc8a59bcd0 {com.facebook.sdk:FBSDKErrorArgumentValueKey=, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Feed share dialogs support FBSDKShareLinkContent., com.facebook.sdk:FBSDKErrorArgumentNameKey=shareContent} Jul 1 15:55:10 local securityd[499]: SecTaskCopyAccessGroups No keychain access group specified whilst running in simulator, falling back to default set

Finally solved using a different Emulator.

There was a bug in the earlier version of FB SDK, but here it seems that you are missing a required implementation of a delegate methods.

        FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
        photo.image = //YOUR IMAGE                                                                 photo.userGenerated = YES;
        FBSDKSharePhotoContent * photoContent = [[FBSDKSharePhotoContent alloc] init];
        photoContent.photos = @[photo];
        FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init];
        shareDialog.shareContent = photoContent;
        shareDialog.delegate = (id)self;
        shareDialog.fromViewController = self;
        NSError * error = nil;
        BOOL validation = [shareDialog validateWithError:&error];
        if (validation) {
            [shareDialog show];
        }

And implement the required delegates method

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
    // handle
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error{
    // handle
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer{
    // handle
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM