UIActivityView按钮错误

时间:2016-11-04 09:03:01

标签: swift xcode uiactivityviewcontroller

我尝试使用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

1 个答案:

答案 0 :(得分:0)

试试这个。至少,它没有错误地为我工作..

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)

如果您想将UIActivityViewController用于iPad,则需要为popoverPresentationController?.sourceView指定activityVC。例如:

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)