Swift发布到Facebook无法正常工作?

时间:2015-07-29 13:45:25

标签: ios facebook swift facebook-graph-api uiimage

我试图将照片发布到Facebook,并使用以下代码正常工作:

   func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

        self.selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        var photo = FBSDKSharePhoto()
        photo.image = self.selectedImage
        photo.userGenerated = true

        var content = FBSDKSharePhotoContent()
        content.photos = [photo]


        self.dismissViewControllerAnimated(true, completion: nil)

        shareDialog.fromViewController = self
        shareDialog.shareContent = content
        shareDialog.show()

}

但是,这仅在用户下载了Facebook应用时才有效。否则它不会打开网络浏览器来分享照片,即使facebook状态:

  

然后,共享对话框切换到原生Facebook for iOS应用程序   发布帖子后,将控制权返回给您的应用。 如果有人   如果没有安装Facebook应用程序,它将自动退回   到基于网络的对话框。

有什么建议吗?感谢,谢谢。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

func postToFacebook(image: UIImage!) {
    var SocialMedia :SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    SocialMedia.completionHandler = {
        result -> Void in
        var getResult = result as SLComposeViewControllerResult;
        switch(getResult.rawValue) {
        case SLComposeViewControllerResult.Cancelled.rawValue: println("Cancelled")
        case SLComposeViewControllerResult.Done.rawValue: println("It's Work!")
        default: println("Error!")
        }
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    self.presentViewController(SocialMedia, animated: true, completion: nil)
    SocialMedia.setInitialText("...")
    SocialMedia.addImage(image)
}