UIActivityView 按钮错误

UIActivityView Button Error

我正在尝试使用 UIActivityView 为我的应用创建共享按钮。当我按下应用程序中的按钮时,出现错误。代码有什么问题?

@IBAction func shareButton(_ sender: UIButton) {

        let texttoshare = "Sharing"
        let URLtoshare = NSURL(string: "www.google.com")
        let objectsToShare:NSArray = [texttoshare, URLtoshare!]
        let activityVC = UIActivityViewController(activityItems: objectsToShare as! [Any], applicationActivities: nil)

        self.present(activityVC, animated: true, completion: nil)

            }
    }

错误:是线程 1:信号 SIGABRT

试试这个。至少,它对我有用,没有错误..

let texttoshare = "Sharing"
let URLtoshare = URL(string: "www.google.com")
let activityVC = UIActivityViewController(activityItems: [texttoshare, URLtoshare!], applicationActivities: nil)
self.present(activityVC, animated: true, completion: nil)

如果要在 iPad 上使用 UIActivityViewController,则需要为 activityVC 指定 popoverPresentationController?.sourceView。例如:

let texttoshare = "Sharing"
let URLtoshare = URL(string: "www.google.com")
let activityVC = UIActivityViewController(activityItems: [texttoshare, URLtoshare!], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = button...
self.present(activityVC, animated: true, completion: nil)