iPad 打开共享时应用程序崩溃 Sheet

iPad App Crashes When Opening Share Sheet

当我在 iPad 上开发的应用程序中打开共享 sheet 时,该应用程序会崩溃。它在 iPhone 上运行良好。我了解到这是因为它必须有一个弹出窗口。但是,我不太确定如何实施。这是我用来调出份额的代码 sheet:

- (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 上正常工作?

如您所知,在 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];
    }
}

希望这就是您要找的。