PopOverViewController中的UIActivityViewController在Swift中崩溃

时间:2015-04-10 20:36:58

标签: ios xcode ipad uipopovercontroller uiactivityviewcontroller

我有一个UIActivityViewController用于分享按钮。对于iPhone,我把它作为常规的UIActivityViewController和iPad的PopOverViewController。这是我的代码

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)

   if let popUpVC = activityVC.popoverPresentationController {
      popUpVC.permittedArrowDirections = .Any
      popUpVC.sourceRect = share.frame
   }
   self.view?.window?.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}

当我按下iPad上的分享按钮时,它只是用(lldb)崩溃。但是当我从一个视图中呈现它时它起作用但是不在正确的位置。这是我从现在开始使用的代码。

popUpVC.sourceView = self.view

1 个答案:

答案 0 :(得分:2)

试试这个,你必须检查你当前运行的设备是否响应popoverPresentationController,因为popoverPresentationController是iOS 8的新功能,并且会在iOS 7上崩溃。它在iPhone上也是零,因为它只是在iPad上UIPopover

let activityViewController = UIActivityViewController(activityItems: [myText, myUrl], applicationActivities: nil)

if activityViewController.respondsToSelector("popoverPresentationController") {
   // iOS8+
   view.presentViewController(activityViewController, animated: true, completion: nil)
   activityViewController.popoverPresentationController?.sourceView = view
} else {
   view.presentViewController(activityViewController, animated: true, completion: nil)
}