objective-c - 使用新的 iOS Facebook SDK API (3.0) 上传照片

标签 objective-c ios facebook facebook-ios-sdk

如何使用新的 API/SDK 从 iOS 应用程序将照片上传到 Facebook?我已经尝试过了,但我一无所获,只是继续原地打转。这是我目前拥有的代码:

-(void)dataForFaceboo{
    self.postParams =
    [[NSMutableDictionary alloc] initWithObjectsAndKeys:
     self.uploadPhoto.image, @"picture", nil];
}

-(void)uploadToFacebook{

    [self dataForFacebook];

    NSLog(@"Going to facebook: %@", self.postParams);

    // Hide keyboard if showing when button clicked
    if ([self.photoCaption isFirstResponder]) {
        [self.photoCaption resignFirstResponder];
    }
    // Add user message parameter if user filled it in
    if (![self.photoCaption.text
        isEqualToString:kPlaceholderPostMessage] &&
        ![self.photoCaption.text isEqualToString:@""]) 
    {
        [self.postParams setObject:self.photoCaption.text forKey:@"message"];
    }
    [FBRequestConnection startWithGraphPath:@"me/feed"
        parameters:self.postParams
        HTTPMethod:@"POST"
        completionHandler:^(FBRequestConnection *connection,
                            id result,
                            NSError *error) 
    {
        NSString *alertText;
        if (error) {
            alertText = [NSString stringWithFormat:
                         @"error: domain = %@, code = %d",
                      error.domain, error.code];
        } else {
            alertText = [NSString stringWithFormat:
                         @"Posted action, id: %@",
                         [result objectForKey:@"id"]];
        }
        // Show the result in an alert
        [[[UIAlertView alloc] initWithTitle:@"Result"
                              message:alertText
                              delegate:self
                              cancelButtonTitle:@"OK!"
                              otherButtonTitles:nil] show];
    }];
}

最佳答案

您的代码很好,需要做一些小改动:

将图像以NSData格式添加到字典中,如

[params setObject:UIImagePNGRepresentation(_image) forKey:@"picture"];

并将图表路径更改为 "me/photos" 而不是 "me/feed"

进行这些更改,它对我有用。 请记住,您需要使用"publish_actions" 权限。

关于objective-c - 使用新的 iOS Facebook SDK API (3.0) 上传照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11974972/

相关文章:

ios - Swift 3.0 今天扩展了具有动态高度的紧凑模式

ios - 在 iOS 8 中自定义 ABPeoplePickerNavigationController 的导航栏

发布或取消后 Facebook 分享对话框不会关闭

android - 如何通过 Facebook Android SDK 向好友发送应用请求

ios - 在 viewDidLoad 方法中链接到另一个 ViewController

objective-c - 如何获取 NSButton 的发件人姓名

iphone - 使用 UIPinchGestureRecognizer 单方向缩放 uiviews

iOS:类似于邮件或消息应用程序的可滚动搜索栏

facebook - Firebase 规则 - 限制拥有用户的访问权限

ios - ios编程中的网络处理