我正在使用UIActivityViewController来共享图像。 Facebook最近发生变化后。我可以单独分享图像和Url,但我无法同时共享图像和网址。相同的代码适用于Mail,twitter等。不确定我在Facebook上缺少什么。
func shareImage() {
var myWebsite = NSURL(string:"http://www.google.com/")
var img: UIImage = currentPhoto!
var shareItems:Array = [img, myWebsite]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
答案 0 :(得分:13)
我修改了swift 2.0的代码:
@IBAction func shareButtonPressed(sender: AnyObject) {
let myWebsite = NSURL(string:"http://www.google.com/")
let img: UIImage = image!
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [img, url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
它正在发布带有网址的图片。
使用此代码的结果:
答案 1 :(得分:5)
SWIFT 3
let myWebsite = NSURL(string: visitedlink)
let img: UIImage = UIImage(named:"Logo")!
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [img,url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo]
self.present(activityViewController, animated: true, completion: nil)
答案 2 :(得分:2)
这是我的解决方案。实施UIActivityItemSource
协议。支持文字,网址和图片。
func doShare() {
let viewController = UIActivityViewController(activityItems: ["ScreenSort for iOS.", URL.init(string: "https://itunes.apple.com/app/id1170886809")!], applicationActivities: nil)
viewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
self.present(viewController, animated: true, completion: nil)
}
extension YourViewController: UIActivityItemSource {
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
return URL.init(string: "https://itunes.apple.com/app/id1170886809")!
}
func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String {
return "ScreenSort for iOS: https://itunes.apple.com/app/id1170886809"
}
func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivityType?, suggestedSize size: CGSize) -> UIImage? {
return nil
}
}
答案 3 :(得分:1)
分享图片和文字
let imgShare : UIImage = UIImage(named: "store")!
let textToShare = "Image form My app."
let objectsToShare = [textToShare, imgShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = sender
self.presentViewController(activityVC, animated: true, completion: nil)