iOS Facebook SDK startWithGraphPath:参数以编程方式发布给一些朋友

时间:2014-05-07 23:19:30

标签: ios facebook facebook-graph-api

在Facebook中,您可以发布内容并与某些朋友分享。 如何使用Facebook SDK在iOS上以编程方式完成此操作?

我的应用程序有一个“紧急按钮”,它发送:a)地图上的用户位置(图片); b)紧急文本(消息); c)帖子仅与用户在配置中选择的朋友共享。部分(隐私)。

- (void)sendMessage {
 //Just an Example: Send an Emergency Post with: message, picture, just to SOME friends.
 //This action may not need the User to actually press a button.

 //Privacy parameter obtained from:
 //https://developers.facebook.com/docs/graph-api/reference/v2.0/post/

 NSMutableDictionary *privacy = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                 @"100000278095294,100000278095528", @"allow", //Friends Id separeted with commas.
                                 @"", @"deny",
                                 @"CUSTOM", @"value",         //Privacy Custom Value
                                 nil];

NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:self.emergencyText          forKey:@"message"];
[params setObject:self.locationMap            forKey:@"picture"];
[params setObject:privacy                     forKey:@"privacy"];

[FBRequestConnection startWithGraphPath:@"me/feed"
                             parameters:params
                             HTTPMethod:@"POST"
                      completionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error) {
                          if (error) {
                              NSLog(@"Error");
                              NSLog(@"Error descriptcion %@",error.description);
                          }else{
                              NSLog(@"Success");
                          }
}];

}

此代码未运行。我从Facebook收到错误。 如果我评论这一行:

// [params setObject:privacy forKey:@"privacy"];

然后它运行正常,我在FB中看到帖子,但它是公共帖子。

我需要发布消息,图片,仅发送给一些朋友。

欢迎使用startWithGraphPath或使用任何其他命令的任何解决方案!

1 个答案:

答案 0 :(得分:3)

您必须将隐私格式化为JSON字符串,然后将该json字符串分配给params NSMutableDictionary。

例如:

NSMutableDictionary *privacy = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                             @"100000278095294,100000278095528", @"allow", //Friends Id      separeted with commas.
                             @"", @"deny",
                             @"CUSTOM", @"value",         //Privacy Custom Value
                             nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:privacy
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

NSString *jsonString;

if (! jsonData) {
    NSLog(@"Got an error: %@", error2);
} else {
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:@"to my friend"          forKey:@"message"];
[params setObject:self.locationMap               forKey:@"picture"];
[params setObject:jsonString                     forKey:@"privacy"];