UIActivityController 共享 URL 文件
UIActivityController to share URL file
我想分享在应用程序中创建的 URL 文件和文本。但它似乎只能共享文本,不能共享 URL 或 UIImage
等任何数据。
我使用的代码:
let sharedVideo = Video(Title: _data[1], VideoID: _data[0], Duration: _data[3], ViewCount: _data[2])
let sharedURL = VideoManager.exportData(video: sharedVideo)
let shareItems:Array = [sharedURL,"check this out baby!"] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
我还使用 UIImage
对象而不是共享 URL 来查看 URL 是否有问题。
即使是图像文件也不起作用。当我单击 UIActivityViewController
内的共享按钮时,它仅适用于文本,不适用于 URL 或图像。
我在 Swift 3 中使用 Xcode 8.
谢谢。
PS:我确信 sharedURL 对象不是 nil 也不是未定义的。
试试这个:
@IBAction func btnExport(sender: AnyObject)
{
let someText:String = "Hello want to share text also"
let objectsToShare:URL = URL(string: "http://www.google.com")!
let sharedObjects:[AnyObject] = [objectsToShare as AnyObject,someText as AnyObject]
let activityViewController = UIActivityViewController(activityItems : sharedObjects, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook,UIActivityType.postToTwitter,UIActivityType.mail]
self.present(activityViewController, animated: true, completion: nil)
}
使用 Swift 5+
的简化解决方案
let url = URL(string: shareUrlString)!
let text = "Some text that you want shared"
let activity = UIActivityViewController(activityItems: [url, text], applicationActivities: nil)
present(activity, animated: true)
我想分享在应用程序中创建的 URL 文件和文本。但它似乎只能共享文本,不能共享 URL 或 UIImage
等任何数据。
我使用的代码:
let sharedVideo = Video(Title: _data[1], VideoID: _data[0], Duration: _data[3], ViewCount: _data[2])
let sharedURL = VideoManager.exportData(video: sharedVideo)
let shareItems:Array = [sharedURL,"check this out baby!"] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
我还使用 UIImage
对象而不是共享 URL 来查看 URL 是否有问题。
即使是图像文件也不起作用。当我单击 UIActivityViewController
内的共享按钮时,它仅适用于文本,不适用于 URL 或图像。
我在 Swift 3 中使用 Xcode 8.
谢谢。
PS:我确信 sharedURL 对象不是 nil 也不是未定义的。
试试这个:
@IBAction func btnExport(sender: AnyObject)
{
let someText:String = "Hello want to share text also"
let objectsToShare:URL = URL(string: "http://www.google.com")!
let sharedObjects:[AnyObject] = [objectsToShare as AnyObject,someText as AnyObject]
let activityViewController = UIActivityViewController(activityItems : sharedObjects, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook,UIActivityType.postToTwitter,UIActivityType.mail]
self.present(activityViewController, animated: true, completion: nil)
}
使用 Swift 5+
的简化解决方案let url = URL(string: shareUrlString)!
let text = "Some text that you want shared"
let activity = UIActivityViewController(activityItems: [url, text], applicationActivities: nil)
present(activity, animated: true)