UIActivityViewController在iOS 8 iPad上崩溃
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIActivityViewController在iOS 8 iPad上崩溃相关的知识,希望对你有一定的参考价值。
我目前正在使用Xcode 6(Beta 6)测试我的应用程序。 UIActivityViewController可与iPhone设备和模拟器配合使用,但与iPad模拟器和设备(ios 8)崩溃并带有以下日志
Terminating app due to uncaught exception 'NSGenericException',
reason: 'UIPopoverPresentationController
(<_UIAlertControllerActionSheetRegularPresentationController: 0x7fc7a874bd90>)
should have a non-nil sourceView or barButtonItem set before the presentation occurs.
我正在为iOS 7和iOS 8使用以下iPhone和iPad代码
NSData *myData = [NSData dataWithContentsOfFile:_filename];
NSArray *activityItems = [NSArray arrayWithObjects:myData, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:nil applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeCopyToPasteboard];
[self presentViewController:activityViewController animated:YES completion:nil];
我也得到了一个类似的崩溃我的其他应用程序。你能指导我吗? iOS 8中的UIActivityViewController有什么变化吗?我查了但是我没有找到任何关于此的内容
在iPad上,活动视图控制器将使用新的UIPopoverPresentationController显示为弹出窗口,它要求您使用以下三个属性之一为弹出窗口的表示指定锚点:
为了指定锚点,您需要获取对UIActivityController的UIPopoverPresentationController的引用,并设置其中一个属性,如下所示:
if ( [activityViewController respondsToSelector:@selector(popoverPresentationController)] ) {
// iOS8
activityViewController.popoverPresentationController.sourceView =
parentView;
}
迅速:
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
//if iPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone) {
self.presentViewController(activityViewController, animated: true, completion: nil)
} else { //if iPad
// Change Rect to position Popover
var popoverCntlr = UIPopoverController(contentViewController: activityViewController)
popoverCntlr.presentPopoverFromRect(CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0), inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
如果在单击UIActivityViewController
时显示UIBarButtonItem
,请使用以下代码:
activityViewController.popoverPresentationController?.barButtonItem = sender
否则,如果您使用其他控件,例如UIButton
,请使用以下代码:
activityViewController.popoverPresentationController?.sourceView = sender
activityViewController.popoverPresentationController?.sourceRect = sender.bounds
从文档到UIPopoverPresentationController
:
var barButtonItem: UIBarButtonItem? { get set }
为此属性指定值以将弹出框架锚定到指定的条形按钮项。显示时,弹出框的箭头指向指定的项目。或者,您可以使用sourceView和sourceRect属性为弹出窗口指定锚点位置。
swift = ios7 / ios8
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
//if iPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone) {
// go on..
} else {
//if iPad
if activityViewController.respondsToSelector(Selector("popoverPresentationController")) {
// on iOS8
activityViewController.popoverPresentationController!.barButtonItem = self.shareButtonItem;
}
}
self.presentViewController(activityViewController, animated: true, completion: nil)
Objective-C的解决方案并使用UIPopoverPresentationController
UIActivityViewController *controller = /*Init your Controller*/;
//if iPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self presentViewController:controller animated:YES completion:nil];
}
//if iPad
else {
UIPopoverPresentationController* popOver = controller.popoverPresentationController
if(popOver){
popOver.sourceView = controller.view;
popOver.sourceRect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0);
[self presentViewController:controller animated:YES completion:nil];
}
}
我找到了这个解决方案首先,你呈现popover的视图控制器应该实现<UIPopoverPresentationControllerDelegate>
协议。
接下来,您需要设置popoverPresentationController
的委托。
添加这些功能:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Assuming you've hooked this all up in a Storyboard with a popover presentation style
if ([segue.identifier isEqualToString:@"showPopover"]) {
UINavigationController *destNav = segue.destinationViewController;
PopoverContentsViewController *vc = destNav.viewControllers.firstObject;
// This is the important part
UIPopoverPresentationController *popPC = destNav.popoverPresentationController;
popPC.delegate = self;
}
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController: (UIPresentationController *)controller {
return UIModalPresentationNone;
}
我尝试了下一个代码,它的工作原理:
首先在View Controller中放置一个条形按钮项,然后创建一个IBOutlet:
@property(weak,nonatomic)IBOutlet UIBarButtonItem *barButtonItem;
接下来是.m文件:yourUIActivityViewController.popoverPresentationController.barButtonItem = self.barButtonItem;
在swift 4下面的代码在iphone和ipad中工作。根据文件
您有责任使用适用于给定设备习惯用法的方法来呈现和关闭视图控制器。在iPad上,您必须在弹出窗口中显示视图控制器。在其他设备上,您必须以模态方式呈现它。
let activityViewController = UIActivityViewController(activityItems: activityitems, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
if activityViewController.responds(to: #selector(getter: UIViewController.popoverPresentationController)) {
activityViewController.popoverPresentationController?.sourceView = self.view
}
}
self.present(activityViewController, animated: true, completion: nil)
对于Swift 2.0。我发现如果你试图将popover锚定到iPad上的分享按钮,这是有效的。这假设您已在工具栏中为共享按钮创建了一个插座。
func share(sender: AnyObject) {
let firstActivityItem = "test"
let activityViewController = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone {
self.presentViewController(activityViewController, animated: true, completion: nil)
}
else {
if activityViewController.respondsToSelector("popoverPresentationController") {
activityViewController.popoverPresentationController!.barButtonItem = sender as? UIBarButtonItem
self.presentViewController(activityViewController, animated: true, completion: nil)
}
}
}
如果您使用swift开发iPad,请小心,它在调试时可以正常工作,但在发布时会崩溃。为了使其与testFlight和AppStore一起使用,请使用-none
禁用针对swift的优化以进行发布。
同样的问题来到我的项目然后我找到解决方案,在iPad中打开UIActivityViewController,我们必须使用UIPopoverController
这是一个在iPhone和iPad上使用它的代码
//to attach the image and text with sharing
UIImage *image=[UIImage imageNamed:@"giraffe.png"];
NSString *str=@"Image form My app";
NSArray *postItems=@[str,image];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];
//if iPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self presentViewController:controller animated:YES completion:nil];
}
//if iPad
else {
// Change Rect to position Popover
UIPopoverController *popup = [[UIPopover以上是关于UIActivityViewController在iOS 8 iPad上崩溃的主要内容,如果未能解决你的问题,请参考以下文章
UIActivityViewController共享消息取消未显示
Facebook 未显示在 UIActivityViewController 中,但 twitter 显示
使用 UIActivityViewController 共享存储在相机胶卷中的 PHAsset
UIActivityViewController 分享消息取消不显示