我已经为这个问题挣扎了三天:我从SO那里得到的示例代码在ios7.11上运行,但崩溃时出现了奇怪的崩溃日志:
代码为:
UIImage *image=[UIImage imageNamed:@"wishlist_active.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 = [[UIPopoverController alloc] initWithContentViewController:controller];
[popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
iOS 8.3上的崩溃日志是:
2015-06-22 18:09:22.706 MyApp[827:61605] *** Terminating app due to uncaught exception 'NSRangeException, reason: *** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
***首次抛出调用堆栈:
(0x2341afef 0x31968c8b 0x2332d821 0x27120261 0x271202b7 0x26d0aab1 0x26e03dcd 0x26e037a7 0x26a6fb8f 0x26a6f8fd 0x26c3fb0b 0x26a6fb8f 0x26a6f8fd 0x26d61677 0x26d5c949 0x26d5da83 0x26d5f5e7 0x26b4ff5f 0x143c15 0x26aa0e2b 0x26aa0dd1 0x26a8b9c3 0x26aa083d 0x26aa0517 0x26a99df1 0x26a6ffe5 0x26ce68fb 0x26a6e9f9 0x233e0faf 0x233e03bf 0x233dea25 0x2332b201 0x2332b013 0x2ac0a201 0x26acfa59 0x144c11 0x31ef4aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我已经在8.3上检查了您的代码,它工作时没有任何崩溃。因此,我建议您将NSParametrAssert();对于数组上的项进行检查,如果一切正常。即使认为问题是由代码的另一部分引起的,在将其放入数组之前,最好先抛出NSParametrAssert进行文字初始化。
如果您使用导航控制器,请尝试从导航控制器中呈现活动VC,如下所示:
UIImage *image=[UIImage imageNamed:@"images.png"];
NSString *str=@"Image form My app";
NSParameterAssert(image && str);
NSArray *postItems=@[str,image];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];
NSParameterAssert(controller);
//if iPhone
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.navigationController presentViewController:controller animated:YES completion:nil];
}
//if iPad
else {
// Change Rect to position Popover
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
[popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
希望这能有所帮助。