在背景中与facebook分享照片到facebook

时间:2017-08-24 11:04:32

标签: ios swift facebook facebook-graph-api

我在背景中使用以下代码成功地将图像发布到Facebook,但不知何故不知道如何包含字幕。我已阅读官方文件但仍无法找到。请帮忙。提前谢谢。

import UIKit
import FBSDKLoginKit
import FBSDKShareKit


class ViewController: UIViewController, FBSDKLoginButtonDelegate, FBSDKSharingDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        let loginButton = FBSDKLoginButton()
        loginButton.center = view.center
        view.addSubview(loginButton)
        loginButton.delegate = self
    }

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { 
        if (FBSDKAccessToken.current() != nil) { FBSDKGraphRequest(graphPath: "me", parameters: nil).start(completionHandler: 
            {(connection, result, error) in
                if error == nil {
                    print("fetch user \(result)")
                }
            })
        }
     }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("you logout")
    }


    @IBAction func postImage(_ sender: Any) {
        let img = UIImage(named: "image")
        let content = FBSDKSharePhotoContent()
        content.photos = [FBSDKSharePhoto(image: img, userGenerated: true)]
        FBSDKShareAPI.share(with: content, delegate: self)   
    }

    func sharerDidCancel(_ sharer: FBSDKSharing!) {
        print("sharerDidCancel")
    }

    func sharer(_ sharer: FBSDKSharing!, didFailWithError error: Error!) {
        print("didFail")
    }

    func sharer(_ sharer: FBSDKSharing!, didCompleteWithResults results: [AnyHashable : Any]!) {
        print("didCompleteWithResult")
    }

}

1 个答案:

答案 0 :(得分:4)

试试这个: -

 @IBAction func postImage(_ sender: Any) {
    let img = UIImage(named: "image")
   let photo = FBSDKSharePhoto()
    photo.image = img
    photo.caption = "Test Caption"
    photo.isUserGenerated = true
    let content = FBSDKSharePhotoContent()
    content.photos = [photo]
    FBSDKShareAPI.share(with: content, delegate: self)   
}