Liam Stockhomme
Liam Stockhomme

Reputation: 523

How to fix this social media view sharing (iOS)?

I have a button on my main page that when clicked makes a subview pop up that displays a few options to share on social media. So far I have the following code:

- (IBAction)showActivityView:(id)sender {
    NSString *shareText = @"The text I am sharing"; 
    //UIImage how can I add an image 
    NSArray *itemsToShare = @[shareText];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil]; //creating mini view controller that pops up that shows the social options, subclass of view controller
    activityVC.excludedActivityTypes = @[UIActivityTypeAirDrop, UIActivityTypeAssignToContact, UIActivityTypeMail, UIActivityTypePostToTencentWeibo, UIActivityTypePrint, UIActivityTypeCopyToPasteboard];
    [self presentViewController:activityVC animated:YES completion:nil];
}

This functions as I'd like, but I'm trying to figure out how to add an image (second line). I thought it would be UIImage *shareImage = @"img.png"; but it didn't seem to work. My other question is: Is there a way that after getting that UIImage line to work, is it possible that I could set up a piece of code that screenshots the users highscore before sharing it, as opposed to creating a status update or tweet with a static image?

Upvotes: 0

Views: 647

Answers (1)

roymckrank
roymckrank

Reputation: 699

Late answer, but here is the code that I'm using for something similar:

 //takes screenshot
 UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CGRect rect = [keyWindow bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyWindow.layer renderInContext:context];
    UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

 //Share image and text

NSString *text = [NSString stringWithFormat:@"My score"];

UIActivityViewController *controller =
[[UIActivityViewController alloc]
 initWithActivityItems:@[text, capturedScreen]
 applicationActivities:nil];

controller.excludedActivityTypes = @[
                                     UIActivityTypeAssignToContact,
                                     UIActivityTypePostToFlickr,
                                     UIActivityTypePostToVimeo,
                                     UIActivityTypePostToTencentWeibo,
                                     ];

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

I hope that help.

Upvotes: 1

Related Questions