user5647824
user5647824

Reputation:

UIActivityViewController not working on iPad? iOS 8

I've followed suggestions from similar SO questions and can't seem to make this work. I'm working on a game, which reports the last score and then a share button for that last score.

This works on iPhone and my code for the iPad as shown below doesn't. The iPad version just crashes as soon as I hit the Share button. This code was from this tutorial on Coding Explorer.

iPad code (doesn't work)

- (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 code (does work)

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

Upvotes: 4

Views: 4579

Answers (2)

rockdaswift
rockdaswift

Reputation: 9983

I also had to set the modalPresentationStyle, preferredContentSize and sourceRect for the Pipiks answer to work.

Upvotes: 0

Pipiks
Pipiks

Reputation: 2048

You must add a source view.

Try to add this line :

activityVC.popoverPresentationController.sourceView = self.view;

You can also add the arrow direction :

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

Upvotes: 5

Related Questions