共享按钮在 iPhone 上完美运行,但在 iPad 上崩溃

Fac*_*ose 2 social share swift

我正在尝试添加一个按钮,以便在 Twitter、Facebook 等中分享一些句子。它适用于所有 iPhone 型号,但模拟器在 iPad 上崩溃。

这是我的代码:

@IBAction func shareButton(sender: AnyObject) {

    frase = labelFrases.text!
    autor = labelAutores.text!


    var myShare = "\(frase) - \(autor)"

    let activityVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)



    self.presentViewController(activityVC, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

这是错误:

由于未捕获的异常“NSGenericException”而终止应用程序,原因:“UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7c0f9190>) 应该在演示发生之前设置一个非零的 sourceView 或 barButtonItem

我该如何解决?谢谢

小智 5

对于 ipad (iOS > 8.0) 你需要设置 popoverPresentationController:

    //check ipad
    if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)
    {
        //ios > 8.0
        if ( activityVC.respondsToSelector(Selector("popoverPresentationController")) ) {
            activityVC.popoverPresentationController?.sourceView = super.view
        }
    }

    self.presentViewController(activityVC, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

更多信息: UIActivityViewController 在 iOS8 iPad 上崩溃