UIActivityViewControllerを使用して画像を共有しています。 Facebookの最近の変更後。画像とURLを個別に共有することはできますが、画像と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)
}
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)
}
そしてURL付きの画像を投稿しています。
このコードの結果:
スウィフト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)
これが私の解決策です。 UIActivityItemSource
プロトコルを実装します。テキスト、URL、画像をサポートします。
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
}
}
画像とテキストを共有するには
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)