Search code examples
iosobjective-cinstagramsocial-networkinguidocumentinteraction

How to differentiate actions selected for `UIDocumentInteractionController` "Open in" menu


I am trying to implement easy sharing for images that exist in my app using UIDocumentInteractionController. With the below code I am using, everything looks fine. (image can be forwarded to whatsapp, viber, instagram. Image can be saved to camera roll and so on)

open in and options sheet

However, I need to differentiate between some of the actions in the menus. This is because for instance Instagram only accepts square images and preprocessing is needed before i feed the image to instagram. (Instagram engineers should start the app from the crop screen instead of the filters scren!@#!$) Otherwise instagram auto crops the bottom or right part of the image automatically.

So do you think there is a way to feed different images for different menu item actions? Or do I have to first present an actioncontroller and say "open in instagram", "open in rest"? If the other apps like whatsapp, twitter, facebook had exclusive "open in" UTIs, I would have gone that way.

By the way do you have any idea why facebook is not showns in the list? (i looked in the edit/manage list. its not there)

    NSString *documentDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    //NSString *saveImagePath = [documentDirectory stringByAppendingPathComponent:@"Image.png"];
    NSString *saveImagePath = [documentDirectory stringByAppendingPathComponent:@"Image.ig"];
    //NSString *saveImagePath = [documentDirectory stringByAppendingPathComponent:@"Image.igo"];    // *.igo is exclusive to instagram
    NSData *imageData = UIImagePNGRepresentation(capsObject.capImage);
    [imageData writeToFile:saveImagePath atomically:YES];
    NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];

    self.docController=[[UIDocumentInteractionController alloc]init];
    self.docController.delegate = self;
    //self.docController.UTI = @"public.image";
    self.docController.UTI = @"com.instagram.photo";
    //self.docController.UTI = @"com.instagram.exclusivegram";
    [self.docController setURL:imageURL];
    //[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
    [self.docController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];

Solution

  • You need to implement the method of the delegate.

    - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application{
       NSLog(@"Will beginsending to application");
    // Detect if the application is instagram
    
      if(![application isEqualToString:@"com.instagram.photo"]){
       //Make the custom implementation of your image.
        UIImage     * iconImage = [self prepareImage];
       //save it to documents path
        NSString    * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Image.ig"];
        [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
       // change the URL of your file
        controller.URL=[NSURL fileURLWithPath:savePath];
    
       }
    }