使用 facebook sdk 4.0 + iOS 分享内容

Sharing content with facebook sdk 4.0 + iOS

我在 iOS (objective-c) 上使用 Facebook SDK 在 Facebook 上分享图像。这是我使用的代码:

-(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];  
}

其中 image 是我通过从用户的画廊中挑选它来设置的 UIImage。当我尝试按下我的按钮(使用此 IBAction)时,应用程序崩溃并出现此错误:

7 月 1 日 10:50:23.local pkd[917]:检索 pid 922 的权利时出错 7 月 1 日 10:50:23 -[MainViewController sharer:didFailWithError:]:发送到实例 0x7fd70940efd0 的无法识别的选择器 7 月 1 日 10:50:23:*** 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[MainViewController sharer:didFailWithError:]:无法识别的选择器发送到实例 0x7fd70940efd0'

这是通过这种方式修复代码后的新错误

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: 无法检索区域信息 7 月 1 日 15:55:06 com.apple.CoreSimulator.SimDevice.D482DFDD-3051-4759-B70D-94EC93BFF5D0.launchd_sim[471] (com.apple.imfoundation.IMRemoteURLConnectionAgent):_DirtyJetsamMemoryLimit 密钥在此平台上不可用。 7 月 1 日 15:55:07 o.local pkd[498]:检索 pid 1169 的权利时出错 7 月 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 共享对话框支持 FBSDKShareLinkContent。,com.facebook.sdk:FBSDKErrorArgumentNameKey=shareContent} 7 月 1 日 15:55:10 local securityd[499]:SecTaskCopyAccessGroups 在模拟器中 运行 时未指定钥匙串访问组,回退到默认设置

终于用不同的模拟器解决了。

早期版本的 FB SDK 中存在一个错误,但在这里您似乎缺少委托方法的必需实现。

        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];
        }

并实现所需的委托方法

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