1

当按下共享按钮时,我有这段代码通过 Apple 共享菜单共享应用程序的屏幕截图、初始文本和 URL。它就像 Twitter、消息、电子邮件等的魅力,但是当我想分享到 Facebook 或 Facebook Messenger 时,文本不会显示(但图像和 URL 会显示)。知道为什么会这样以及如何解决吗?

我能够使用其他功能将文本和 URL 分享到 Facebook(但他们没有提供正确的屏幕截图,所以我切换到了这个)。

提前致谢!

编辑:文本确实在 xcode 模拟器中共享,而不是在 iPhone 上。

   func takeSnapshot(view: UIView) {
        let bounds = UIScreen.mainScreen().bounds
        UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
        self.view.drawViewHierarchyInRect(bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        let text = "Text to be shared... #Hashtag"
        let URL = NSURL(string: "http://stackoverflow.com/")!
        let activityViewController = UIActivityViewController(activityItems: [image, text, URL], applicationActivities: nil)
        self.presentViewController(activityViewController, animated: true, completion: nil)

    }
4

1 回答 1

1

Facebook 应用程序的更新已将内置的 Facebook 分享活动替换为忽略状态文本的活动 - 如果您从设备中删除 Facebook 应用程序,文本将使用您拥有的代码显示。如果您安装了 Facebook 应用程序,您将获得图像、URL,但不会获得文本。

按照说明操作:https ://developers.facebook.com/docs/sharing/ios

然后你可以做这样的事情:

分享链接

let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "<INSERT STRING HERE>")
content.contentTitle = "<INSERT STRING HERE>"
content.contentDescription = "<INSERT STRING HERE>"
content.imageURL = NSURL(string: "<INSERT STRING HERE>")

let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25)
self.view.addSubview(button)

分享照片

class ViewController: UIViewController, FBSDKLoginButtonDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
 let button : UIButton = UIButton()
 button.backgroundColor = UIColor.blueColor()
 button.setTitle("Choose Photo", forState: .Normal)
 button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 150) * 0.5, 125, 150, 25)
 button.addTarget(self, action: "photoBtnClicked", forControlEvents: .TouchUpInside)
 self.view.addSubview(button)

 let label : UILabel = UILabel()
 label.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 200) * 0.5, 100, 200, 25)
 label.text = "Photos Example"
 label.textAlignment = .Center
 self.view.addSubview(label)

func photoBtnClicked(){
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
        println("Photo capture")
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }   
}

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

    let photo : FBSDKSharePhoto = FBSDKSharePhoto()
    photo.image = info[UIImagePickerControllerOriginalImage] as! UIImage
    photo.userGenerated = true
    let content : FBSDKSharePhotoContent = FBSDKSharePhotoContent()
    content.photos = [photo]
}

照片大小必须小于 12 MB

在 Facebook 上分享视频

class ViewController: UIViewController, FBSDKLoginButtonDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
 let button : UIButton = UIButton()
 button.backgroundColor = UIColor.blueColor()
 button.setTitle("Choose Video", forState: .Normal)
 button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 150) * 0.5, 200, 150, 25)
 button.addTarget(self, action: "videoBtnClicked", forControlEvents: .TouchUpInside)
 self.view.addSubview(button)

 let label : UILabel = UILabel()
 label.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 200) * 0.5, 175, 200, 25)
 label.text = "Video Example"
 label.textAlignment = .Center
 self.view.addSubview(label)
 func videoBtnClicked(){
     if  UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSource Type.SavedPhotosAlbum){
         println("Video capture")
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }

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

        let video : FBSDKShareVideo = FBSDKShareVideo()
        video.videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
        let content : FBSDKShareVideoContent = FBSDKShareVideoContent()
        content.video = video
}

视频大小必须小于 12MB。我希望我对你有所帮助。(对不起我的英语不好)

于 2015-08-30T17:45:19.473 回答