iOS Facebook sdk 4.0 分享图片
Posted
技术标签:
【中文标题】iOS Facebook sdk 4.0 分享图片【英文标题】:iOS Facebook sdk 4.0 share an image 【发布时间】:2015-03-27 16:26:01 【问题描述】:我正在尝试使用新的 facebook sdk 4.0 共享图像。
我在 facebook 示例中使用了相同的代码,但是当我尝试呈现 FBSDKShareDialog
时出现此异常。
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法解析格式 字符串 "SELF NOT NULL"' * 第一次抛出调用堆栈:( 0 CoreFoundation 0x000000010b782a75 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010b35abb7 objc_exception_throw + 45 2 基础 0x000000010aca097d _qfqp2_performParsing + 8495 3 基础 0x000000010ac9e7e6 +[NSPredicate predicateWithFormat:arguments:] + 46 4 基础 0x000000010ac9e7a0 +[NSPredicate predicateWithFormat:] + 142 5 PhotoGunShot 0x0000000107f1a4fc -[FBSDKShareDialog _showShareSheetWithCanShowError:validationError:] + 653 6 PhotoGunShot 0x0000000107f18f7b -[FBSDKShareDialog 显示] + 147
这里是代码
- (UIAlertAction*) createActionForFacebookWithMediaPath:(NSString*) path andMediaType:(MediaType) mediaType
NSString * mediaName = mediaType == kMediaTypeImage ? NSLocalizedString(@"KEY_IMAGE", nil) : NSLocalizedString(@"KEY_VIDEO", nil);
UIAlertAction* facebookAction = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"KEY_SHARE_YOUR", nil),mediaName] style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
NSObject<FBSDKSharingContent> * content = nil;
if (mediaType == kMediaTypeImage)
NSURL * fileURL = [NSURL URLWithString:path];
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.imageURL = fileURL;
// photo.userGenerated = YES;
FBSDKSharePhotoContent * photoContent = [[FBSDKSharePhotoContent alloc] init];
photoContent.photos = @[photo];
content = photoContent;
else if (mediaType == kMediaTypeVideo)
NSURL *videoURL = [NSURL fileURLWithPath:path];
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoURL;
FBSDKShareVideoContent * videoContent = [[FBSDKShareVideoContent alloc] init];
videoContent.video = video;
content = videoContent;
[FBSDKShareDialog showFromViewController:self
withContent:content
delegate:nil];
];
return facebookAction;
- (UIAlertAction*) createActionForCameraRollWithMediaPath:(NSString*) path andMediaType:(MediaType) mediaType
return nil;
- (void) showShareOptionsForMediaPath:(NSString*)path
UIAlertController * alertControl =[UIAlertController alertControllerWithTitle:NSLocalizedString(@"KEY_SHARE_TITLE", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"KEY_CANCEL", nil) style:UIAlertActionStyleCancel handler:NULL];
[alertControl addAction:cancelAction];
MediaType mediaType = [self checkMediaTypeAtPath:path];
switch (mediaType)
case kMediaTypeImage:
UIAlertAction * fbAction =[self createActionForFacebookWithMediaPath:path andMediaType:kMediaTypeImage];
[alertControl addAction:fbAction];
break;
case kMediaTypeVideo:
UIAlertAction * fbAction =[self createActionForFacebookWithMediaPath:path andMediaType:kMediaTypeVideo];
[alertControl addAction:fbAction];
break;
case kMediaTypeUnknown:
//EXCEPTION
break;
default:
break;
[self presentViewController:alertControl animated:YES completion:nil];
首先我创建一个显示共享选项的 UIAlertController,并添加相对的UIAlertAction
,当我调用共享对话框时会生成异常。
有什么建议吗?
【问题讨论】:
我已经提交了一个错误,它似乎是一个已知问题 【参考方案1】:此问题将在下一个 SDK 版本中修复。
显然,您可以从 FBSDKShareDialog 中删除以下行并从源代码构建框架以解决它。 https://github.com/facebook/facebook-ios-sdk/blob/master/FBSDKShareKit/FBSDKShareKit/FBSDKShareDialog.m#L442
您可以在此处查看 Andrea 提交的错误:https://developers.facebook.com/bugs/964300063610778/
【讨论】:
以上是关于iOS Facebook sdk 4.0 分享图片的主要内容,如果未能解决你的问题,请参考以下文章
ParseFacebookUtilsV4 和 Facebook-iOS-SDK 4.0
Facebook iOS SDK > 4.0 替代 initWithAppId?
Facebook SDK 4.0 iOS 不再快速切换到设备上安装的 Facebook APP