简体   繁体   中英

iPad App Crashes When Opening Share Sheet

When I open the share sheet in an app that I developed on an iPad, the app will crash. It works fine on the iPhone. I've learned that this is because it has to have a popover. However, I'm not really sure on how to implement is. This is the code that I have in order to bring up the share 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];

}

What do I need to add to it in order to make it work properly on an iPad?

As you already know, on iPad the UIActivityViewController need to be displayed as a popover using UIPopoverController . But, since you are developing universal application, you need to check which device your app is currently running in so that it will work in both. This is how you need to do it:

- (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];
    }
}

Hope this was what you were looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM