Swift UIActivityViewController Image&Url 分享无法与 FB 一起使用
Swift UIActivityViewController Image&Url Share not working with FB
我正在使用 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 的图片。
此代码的结果:
这是我的解决方案。实施 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)
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)
我正在使用 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 的图片。
此代码的结果:
这是我的解决方案。实施 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)
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)