打开共享表时,iPad App崩溃

时间:2015-11-22 18:00:38

标签: ios objective-c ipad

当我在iPad上开发的应用中打开共享表时,应用会崩溃。它在iPhone上运行良好。我了解到这是因为它必须有一个弹出窗口。但是,我对如何实施并不是很确定。这是我为了打开共享表而拥有的代码:

- (IBAction)showActivityView:(id)sender {
    // Implement share sheet
    NSString *shareText = anotherWebView.request.URL.absoluteString;
    NSArray *itemsToShare = @[shareText];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    activityVC.excludedActivityTypes = @[];
    [self presentViewController:activityVC animated:YES completion:nil];

}

我需要添加什么才能使其在iPad上正常运行?

1 个答案:

答案 0 :(得分:0)

如您所知,在iPad上UIActivityViewController需要使用UIPopoverController显示为弹出框。但是,由于您正在开发通用应用程序,因此您需要检查当前正在运行的应用程序设备,以便它可以在两者中运行。这就是你需要做的事情:

- (IBAction)showActivityView:(id)sender {
    // Implement share sheet
    NSString *shareText = anotherWebView.request.URL.absoluteString;
    NSArray *itemsToShare = @[shareText];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    activityVC.excludedActivityTypes = @[];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        // For iPhone
        [self presentViewController:activityVC animated:YES completion:nil];
    }
    else {
        // For iPad, present it as a popover as you already know
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityVC];
        //Change rect according to where you need to display it. Using a junk value here
        [popup presentPopoverFromRect:CGRectMake(0, 0, 0, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

希望这就是你要找的东西。

相关问题