我想在Facebook上分享图片,为此我做了:
let photo: FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = croppedImage
photo.userGenerated = true
photo.caption = "Add Your caption"
let content: FBSDKSharePhotoContent = FBSDKSharePhotoContent()
content.photos = [photo]
FBSDKShareDialog.showFromViewController(self, withContent: content, delegate: nil)
但它没有显示我的ShareViewController。但是当我尝试这段代码时:
let content: FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "http://")
content.contentTitle = "App Testing"
content.contentDescription = "I'm working over the app!"
如何修复第一个代码段?
答案 0 :(得分:2)
我知道这是一个老问题,但万一有人面临同样的问题:
根据documentation,您需要安装原生Facebook应用才能使用FBSDKSharePhotoContent
,在这种情况下您无法使用FBSDKShareDialog
。
使用FBSDKShareDialog
(仅支持FBSDKShareLinkContent
)使用{em>几乎相同的结果的近似解决方法如下:
let content: FBSDKShareLinkContent = FBSDKShareLinkContent()
content.imageURL = URL(string: "www.example.com/my_image.jpg")
content.contentURL = URL(string: "www.example.com/target_url")
content.contentTitle = "My Title"
content.contentDescription = "My Description"
let dialog: FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self // make sure to conform to FBSDKSharingDelegate protocol
dialog.shareContent = content
if !dialog.canShow() {
// fallback to non-native mode
dialog.mode = .feedBrowser
}
dialog.show()
您可以找到协议的方法here。
答案 1 :(得分:0)
仅在安装Facebook应用程序时才有效。另外,明确指定mode = .native
。
let content = FBSDKSharePhotoContent()
let photo = FBSDKSharePhoto(image: image, userGenerated: true)
content.photos = [photo].flatMap({ $0 })
let dialog = FBSDKShareDialog()
dialog.fromViewController = viewController
dialog.shareContent = content
dialog.delegate = self
dialog.mode = .native
dialog.show()
如果没有,它将使用SLComposeViewController
,您将收到错误
SLComposeViewController isAvailableForServiceType got serviceType com.apple.social.facebook isAvailable 0
良好的做法是在FBSDKShareDialog.m
上进行调试,看看事情是否适合您
- (BOOL)show
{
BOOL didShow = NO;
NSError *error = nil;
if ([self _validateWithError:&error]) {
switch (self.mode) {
case FBSDKShareDialogModeAutomatic:{
didShow = [self _showAutomatic:&error];
break;
}
case FBSDKShareDialogModeBrowser:{
didShow = [self _showBrowser:&error];
break;
}
case FBSDKShareDialogModeFeedBrowser:{
didShow = [self _showFeedBrowser:&error];
break;
}
case FBSDKShareDialogModeFeedWeb:{
didShow = [self _showFeedWeb:&error];
break;
}
case FBSDKShareDialogModeNative:{
didShow = [self _showNativeWithCanShowError:&error validationError:&error];
break;
}
case FBSDKShareDialogModeShareSheet:{
didShow = [self _showShareSheetWithCanShowError:&error validationError:&error];
break;
}
case FBSDKShareDialogModeWeb:{
didShow = [self _showWeb:&error];
break;
}
}
}
if (!didShow) {
[self _invokeDelegateDidFailWithError:error];
} else {
[self _logDialogShow];
[FBSDKInternalUtility registerTransientObject:self];
}
return didShow;
}
获得的经验: