UIActivityViewController in Swift Crashes on iPad

一世执手 提交于 2020-01-11 05:01:27

问题


I am making a share function in my game and I have the code and it works fine on iPhone but when I test it on a iPad, when I tap the share button the app crashes. I am using the following code for the share button

let textToShare = "Check out this website!"

if let myWebsite = NSURL(string: "http://www.apple.com/") {
   let objectsToShare = [textToShare, myWebsite]
   let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
   self.view?.window?.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}

回答1:


The UIActivityViewController's has non-null popoverPresentationController property when running on iPad. So, try below.

if let wPPC = activityVC.popoverPresentationController {
    wPPC.sourceView = some view
    //  or
    wPPC.barButtonItem = some bar button item
}
presentViewController( activityVC, animated: true, completion: nil )


来源:https://stackoverflow.com/questions/29550849/uiactivityviewcontroller-in-swift-crashes-on-ipad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!