我一直在为这个问题苦苦挣扎三天: 我从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)
答案 0 :(得分:0)
我已经在8.3上检查了你的代码,它没有任何崩溃。 所以,我建议你把NSParametrAssert();对于要检查的数组上的项目,如果一切正常。即使思想问题是由代码的另一部分引起的,检查项目之前抛出NSParametrAssert然后将其放入具有字面初始化的数组中是一个好习惯。
如果您使用导航控制器尝试从navigationController中显示活动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];
}
希望这有帮助。