ios - 在 Facebook iOS SDK 4.0 上显式分享

标签 ios facebook 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];

最佳答案

将此页面的答案拼凑在一起,这对我有用:

<强>1。选中显式共享框

转到您的打开图设置、操作类型,然后选择您的自定义操作。向下滚动到功能部分并确保选中“明确共享”框。

Capabilities Settings

<强>2。设置 fb:explicitely_shared On Action

[action setString:@"true" forKey:@"fb:explicitly_shared"];

<强>3。设置 Action 与对象之间的关系

确保您知道将您的操作与您的对象相关联的正确方法。在这种情况下,关键是“文章”:

[FBSDKShareOpenGraphAction actionWithType:@"mynamespace:share" object:object key:@"article"]

您可以通过查看您在 Facebook 应用的开放图谱设置中创建的操作类型来找到该键。当您通过 Story 将 Action 与 Object 连接时,该关系的新键将出现在 Property Name 列下的 Action 页面上。

当您真正需要的只是该键的 property_name 时,原始海报使用的是 namespace:property_name 格式。这可能是针对 OP 的修复。

<强>4。设置委托(delegate),查找错误

OP 没有利用 FBSDKSharingDelegate 功能。它会报告错误并帮助您解决问题:

[[FBSDKShareAPI shareWithContent:content delegate:self] share];

并在自身中实现委托(delegate)方法:

#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.");
}

作为引用,这是我的工作代码

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

关于ios - 在 Facebook iOS SDK 4.0 上显式分享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29481921/

相关文章:

ios - 苹果拒绝app 10.6 因为Facebook打开Safari登录

ios - presentShareDialogWithParams发布到FB墙,但是回调处理程序结果显示错误

ios - 从 touchesEnded 获取图像名称

javascript - 如何从 Facebook javascript SDK 获取授权码

facebook - 如何定位 Facebook 弹出窗口?

ios - Cordova/Phonegap 社交分享

ios - 在iOS应用程序和Web应用程序上链接Facebook Graph

ios - 图像与身份的比较并映射相同的像素

ios - Xcode Playground : new pages cannot refer to source files

iphone - 如何防止我的 iPhone 4 手电筒应用程序在关闭时闪烁?