【问题标题】:IOS 8 iPad App Crashes When UIActivityViewController Is CalledIOS 8 iPad 应用程序在调用 UIActivityViewController 时崩溃
【发布时间】:2015-04-17 09:31:24
【问题描述】:

当在这个应用程序中在 iPhone 上调用 UIActivityViewController 时,它可以正常工作,但是当在 iPad 上调用时,应用程序会崩溃。下面是我使用的代码:

func shareButtonPress() {

    //when the share button is pressed, default share phrase is added, cropped image of highscore is added

    var sharingItems = [AnyObject]()

    var shareButtonHighscore = NSUserDefaults.standardUserDefaults().objectForKey("highscore") as Int!

    sharingItems.append("Just hit \(shareButtonHighscore)! Beat it! #Swath")

    UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    sharingItems.append(image)

    let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)

    var barButtonItem: UIBarButtonItem! = UIBarButtonItem()

    activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]

    self.presentViewController(activityViewController, animated: true, completion: nil)

}

如您所见,我正在使用 Swift 和 SpriteKit 框架进行编程,但我不明白应用程序为什么会崩溃。

我收到此错误:

Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7fc7a874bd90>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'

我可以做些什么来解决这个问题?

【问题讨论】:

  • > 我可以做些什么来解决这个问题? -- 1.阅读错误信息。 2.查看UIPopoverController上的文档

标签: swift ios8 crash sprite-kit uiactivityviewcontroller


【解决方案1】:

在呈现UIActivityViewController之前,添加这行代码:

activityViewController.popoverPresentationController?.sourceView = self.view

这样,视图控制器就知道在 GameViewController 的哪一帧中出现了。

【讨论】:

    【解决方案2】:

    如果您阅读了说明如何修复它的错误,则需要设置 barButtonItem 或 sourceView 从中呈现弹出窗口,在您的情况下:

    func shareButtonPress(pressedButton: UIBarButtonItem) { 
    
        ...
    
        activityViewController.popoverPresentationController.barButtonItem = pressedButton
    
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }
    

    【讨论】:

    • 现在我收到此错误:[MyGame.GameViewController shareButtonPress]: unrecognized selector sent to instance 0x15f8c610'
    • 我能做些什么来纠正这个问题?我不能用实际按钮的名称替换 UIBarButtonItem,因为实际按钮是在 GameScene 中定义的。
    • 如何将目标添加到 UIBarButtonItem?
    • 这是一个比其他解决方案更好的解决方案,因为这个视图控制器应该显示在“调用者”视图的顶部。
    【解决方案3】:

    Swift 5

    检查设备是iPhone 还是iPad,并在此基础上添加sourceView 并显示activityController

    let activity = UIActivityViewController(activityItems: [self], applicationActivities: nil)
    if UIDevice.current.userInterfaceIdiom == .phone {
        UIApplication.topViewController?.present(activity, animated: true, completion: nil)
    } else {
        activity.popoverPresentationController?.sourceView = UIApplication.topViewController!.view
        UIApplication.topViewController?.present(activity, animated: true, completion: nil)
    }
    

    【讨论】:

      【解决方案4】:

      有两个选项,动作来自 UIBarButtonitem 或 UIButton,即 UIView。

      func shareButtonPress() {
      
          ...
      
          if let actv = activityViewController.popoverPresentationController {
              actv.barButtonItem = someBarButton // if it is a UIBarButtonItem
      
              // Or if it is a view you can get the view rect
              actv.sourceView = someView
              // actv.sourceRect = someView.frame // you can also specify the CGRect
          }
      
          self.presentViewController(activityViewController, animated: true, completion: nil)
      }
      

      您可能需要将发件人添加到您的函数中,例如 func shareButtonPress(sender: UIBarButtonItem)func shareButtonPress(sender: UIButton)

      【讨论】:

        【解决方案5】:

        我为 Swift 3 添加了:

        activityViewController.popoverPresentationController?.sourceView = self.view
        

        解决了我的问题。

        【讨论】:

          猜你喜欢
          • 2014-10-27
          • 1970-01-01
          • 1970-01-01
          • 2011-10-26
          • 1970-01-01
          • 1970-01-01
          • 2016-03-30
          • 2015-03-27
          • 1970-01-01
          相关资源
          最近更新 更多