UIActivityViewController 在 iPad 中不工作?

UIActivityViewController is not working in iPad?

我想在用户单击共享按钮时显示一个 popoverPresentationController。我使用动作 sheet 作为分享按钮。 这是我的代码,它不适用于 iPad:

    NSString *aText = @"Some Text";

    NSURL *aUrl = [NSURL URLWithString:@"www.demo.com"];

    NSArray *objectsToShare = @[aText,aUrl];
    NSArray * applicationActivities = nil;

    UIActivityViewController * activityViewController = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:applicationActivities];

    activityViewController.excludedActivityTypes = @[
                                                     UIActivityTypePostToWeibo,
                                                     UIActivityTypeAssignToContact,
                                                     UIActivityTypeAirDrop,
                                                     UIActivityTypeAddToReadingList,
                                                     UIActivityTypeCopyToPasteboard,
                                                     UIActivityTypeSaveToCameraRoll,
                                                     UIActivityTypePrint,
                                                     UIActivityTypePostToFlickr,
                                                     UIActivityTypePostToTencentWeibo,];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self presentViewController:activityViewController animated:YES completion:nil];
    }
    else {

        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
        [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4 + 20, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    }

iPad 错误(屏幕上没有显示)。那我该如何解决呢?

是的,我知道并解决了我的问题。

在 iPad 上,activity 视图控制器将使用 popoverPresentationController 显示为弹出窗口,并使用 UIAlertController 显示弹出窗口。

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

[actionSheet addAction:[UIAlertAction actionWithTitle:@"Share" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSString *aText = @"Some Text";

NSURL *aUrl = [NSURL URLWithString:@"www.demo.com"];

NSArray *objectsToShare = @[aText,aUrl];
NSArray * applicationActivities = nil;

UIActivityViewController * activityViewController = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:applicationActivities];

activityViewController.excludedActivityTypes = @[
                                                 UIActivityTypePostToWeibo,
                                                 UIActivityTypeAssignToContact,
                                                 UIActivityTypeAirDrop,
                                                 UIActivityTypeAddToReadingList,
                                                 UIActivityTypeCopyToPasteboard,
                                                 UIActivityTypeSaveToCameraRoll,
                                                 UIActivityTypePrint,
                                                 UIActivityTypePostToFlickr,
                                                 UIActivityTypePostToTencentWeibo,];

        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
        [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4 , 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

    }]];

    actionSheet.popoverPresentationController.sourceView = self.view;
    actionSheet.popoverPresentationController.sourceRect = CGRectMake(self.view.frame.size.width, btn.frame.origin.y +btn.frame.size.height , 0, 0);
    actionSheet.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;

    [self presentViewController:actionSheet animated:YES completion:nil];