Cap*_*ggz 4 facebook ios facebook-ios-sdk
我想使用新的iOS 4.0 SDK在Facebook上明确分享一个开放的图形动作.以下不起作用.它显示在我的活动日志中,但不显示在我的时间轴上.
// Construct an FBSDKSharePhoto
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.image = img;
photo.userGenerated = YES;
// Create an object
NSDictionary *properties = @{
@"og:type": @"theprose:post",
@"og:title": post.title,
@"og:description": post.text,
@"og:caption": @"A fresh picked Prose for you.",
@"og:url": post.url,
@"fb:explicitly_shared": @"true"
};
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties];
// Create an action
FBSDKShareOpenGraphAction *act = [[FBSDKShareOpenGraphAction alloc] init];
act.actionType = @"theprose:share";
[act setObject:object forKey:@"theprose:post"];
[act setPhoto:photo forKey:@"image"];
// Create the content
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
content.action = act;
content.previewPropertyName = @"theprose:post";
[[FBSDKShareAPI shareWithContent:content delegate:nil] share];
Run Code Online (Sandbox Code Playgroud)
拼凑这个页面的答案,这对我有用:
1.选中"明确共享"框
转到打开的图表设置,操作类型,然后选择自定义操作.向下滚动到功能部分,确保选中"显式共享"框.

2.设置fb:explicitely_shared On Action
[action setString:@"true" forKey:@"fb:explicitly_shared"];
Run Code Online (Sandbox Code Playgroud)
3.设置动作和对象之间的关系
确保您知道将操作连接到对象的正确方法.在这种情况下,关键是"文章":
[FBSDKShareOpenGraphAction actionWithType:@"mynamespace:share" object:object key:@"article"]
Run Code Online (Sandbox Code Playgroud)
您可以通过查看在FB应用程序的Open Graph设置中创建的Action Type来找到该键.当您通过Story将Action与Object连接时,该关系的新键将显示在Property Name列的Action页面上.
原始海报使用namespace:property_name格式,而您真正需要的只是该键的property_name.这可能是OP的修复.
4.设置代理,查找错误
OP没有利用FBSDKSharingDelegate功能.它会报告错误并帮助您解决问题:
[[FBSDKShareAPI shareWithContent:content delegate:self] share];
Run Code Online (Sandbox Code Playgroud)
并在self中实现委托方法:
#pragma mark - FBSDKSharingDelegate
- (void) sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
NSLog(@"Facebook sharing completed: %@", results);
}
- (void) sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
NSLog(@"Facebook sharing failed: %@", error);
}
- (void) sharerDidCancel:(id<FBSDKSharing>)sharer {
NSLog(@"Facebook sharing cancelled.");
}
Run Code Online (Sandbox Code Playgroud)
供参考,这是我的工作代码
// Create the object
NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary: @{
@"og:type": @"mynamespace:article",
@"og:title": myModel.title,
@"og:description": @"myModel.description",
@"og:url": @"http://mywebsite.com"
}];
NSURL *imageURL = [myModel getImageURL];
if (imageURL) {
FBSDKSharePhoto *photo = [FBSDKSharePhoto photoWithImageURL:imageURL userGenerated:NO];
[properties setObject:@[photo] forKey:@"og:image"];
}
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties];
// Create the action
FBSDKShareOpenGraphAction *action = [FBSDKShareOpenGraphAction actionWithType:@"mynamespace:share" object:object key:@"article"];
[action setString:@"true" forKey:@"fb:explicitly_shared"];
// Create the content
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
content.action = action;
content.previewPropertyName = @"article";
// Share the content
FBSDKShareAPI *shareAPI = [[FBSDKShareAPI alloc] init];
shareAPI.shareContent = content;
shareAPI.delegate = self;
[shareAPI share];
Run Code Online (Sandbox Code Playgroud)