Skip to content

Instantly share code, notes, and snippets.

@kublaios
Created July 1, 2014 07:03
Show Gist options
  • Save kublaios/226dbea44105dd9b1c60 to your computer and use it in GitHub Desktop.
Save kublaios/226dbea44105dd9b1c60 to your computer and use it in GitHub Desktop.
Sample iOS Game Sharing High Score With UIActivityViewController
- (void)share:(NSNotification *)notification {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// high score stuff
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"score"]) {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"score"] respondsToSelector:@selector(intValue)]) {
int highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"score"] intValue];
NSString *shareText = [NSString stringWithFormat:@"My Score is %i", highScore];
NSArray *activityProviders = @[shareText, image];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:activityProviders applicationActivities:nil];
// tell the activity view controller which activities should NOT appear
activityViewController.excludedActivityTypes = @[UIActivityTypeAddToReadingList,UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop,UIActivityTypePrint, UIActivityTypeAssignToContact];
// display the options for sharing
activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:activityViewController animated:YES completion:nil];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment