Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customizing UIActivityViewController

I'm trying to attach a screenshot to my mail in my UIActivityViewController without the screenshot being saved to my library. Here's my code so far:

-(IBAction)ActivityController:(id)sender {{



    NSString *shareString = @"";
    UIImage *shareImage = [UIImage imageNamed:@""];


    NSURL *shareUrl = [NSURL URLWithString:@""];
    NSArray *activityItems = [NSArray arrayWithObjects:shareString,shareImage, shareUrl, nil];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:activityViewController animated:YES completion:nil];

    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];

    mailController.mailComposeDelegate = self;
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *exportData = UIImageJPEGRepresentation(image ,1.0);
    [mailController addAttachmentData:exportData mimeType:@"image/jpeg" fileName:@"Screenshot.jpeg"];

There is no screenshot attached to the email.

And how do I create an action when I press the cancel button in UIActivityViewcontroller?

like image 913
user2667306 Avatar asked Apr 28 '26 02:04

user2667306


1 Answers

UIActivityViewController help you easy to share data from iOS6. Just call it, input data and click which one you want to share. Don't init MFMailComposeViewController.

This is sample common code working in iPad and iPhone

-(IBAction)actionButton:(id)sender {
  //Popover for iPad
    //if (self.popover) {
    //    if ([self.popover isPopoverVisible]) {
      //      return;
      //  } else {
       //     [self.popover dismissPopoverAnimated:YES];
      //      self.popover = nil;
      //  }
   // }

    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSString *shareText = @"Share Text";
    NSURL *shareURL = [NSURL URLWithString:@"http://shareURL.com"];

    /* iOS 6 sharing, */
    UIActivity *activity = [[UIActivity alloc] init];

    NSArray *activityItems = @[image, shareText, shareURL];
    NSArray *applicationActivities = @[activity];
    NSArray *excludeActivities = @[];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
    activityController.excludedActivityTypes = excludeActivities;

    // switch for iPhone and iPad.
   // if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
     //   self.popover = [[UIPopoverController alloc] initWithContentViewController:activityController];
      //  self.popover.delegate = self;
       // [self.popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
   // } else {
        [self presentViewController:activityController animated:YES completion:^{
            NSLog(@"Activity complete");
        }];
   // }


}

EDIT If you want to know when Cancel button click, you need to modify a little in presentViewController

    [self presentViewController:activityController animated:YES completion:nil];
    [activityController setCompletionHandler:^(NSString *act, BOOL done)
     {
         if (!act) {
             NSLog(@"Cancel");
         } 
     }];
like image 88
LE SANG Avatar answered Apr 30 '26 02:04

LE SANG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!