UIActivityViewController 无法在 iPad 上工作? iOS 8

UIActivityViewController not working on iPad? iOS 8

我遵循了类似 SO 问题的建议,但似乎无法完成这项工作。我正在开发一款游戏,它会报告最后的得分,然后是最后得分的分享按钮。

这适用于 iPhone 而我的代码 iPad 如下所示不起作用。 iPad 版本在我点击“分享”按钮后立即崩溃。此代码来自 this tutorial on Coding Explorer.

iPad 代码(不起作用)

- (IBAction)shareButton:(id)sender;
{
    NSString * text = [NSString stringWithFormat:@"I just scored %li in Flying Freddy. Think you can beat it? https://itunes.apple.com/app/id1033007818", (long)_score];

NSArray *objectsToShare = @[text];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                               UIActivityTypePrint,
                               UIActivityTypeAssignToContact,
                               UIActivityTypeSaveToCameraRoll,
                               UIActivityTypeAddToReadingList,
                               UIActivityTypePostToFlickr,
                               UIActivityTypePostToVimeo];

activityVC.excludedActivityTypes = excludeActivities;

[self presentViewController:activityVC animated:YES completion:nil];
}

iPhone 代码(有效)

- (IBAction)ShareIt {

    NSString * text = [NSString stringWithFormat:@"I just scored %li in Flying Freddy. Think you can beat it? https://itunes.apple.com/app/id1033007818", (long)_score];

    NSArray * activityItems = @[text];
    UIActivityViewController * avc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    avc.excludedActivityTypes = @[ UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];

    [self presentViewController:avc animated:YES completion:nil];    
}

您必须添加源视图。

尝试添加这一行:

activityVC.popoverPresentationController.sourceView = self.view;

您还可以添加箭头方向:

activityVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionRight;
activityVC.popoverPresentationController.sourceView = sender;

我还必须为 Pipiks 答案设置 modalPresentationStyle、preferredContentSize 和 sourceRect。