本文介绍了IOS 8 iPad 应用程序在调用 UIActivityViewController 时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When a UIActivityViewController is called on the iPhone in this app, it works perfectly, but when called on a iPad, the app crashes. Below is the code I used:

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 框架进行编程,但我不明白应用程序崩溃的原因.

As you can see, I'm programming in Swift, in the SpriteKit Framework, and I don't understand why the app is crashing.

我收到此错误:

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

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

What can I do to fix this problem?

推荐答案

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

Before presenting the UIActivityViewController, add in this line of code:

activityViewController.popoverPresentationController?.sourceView = self.view

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

This way, the view controller knows in which frame of the GameViewController to appear in.

这篇关于IOS 8 iPad 应用程序在调用 UIActivityViewController 时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:08