Instagram open UTI directly

2019-03-24 16:02发布

问题:

I recently stumbled upon the following interesting feature: Instagram iPhone Hooks

I was wondering if one is able to open an app through the documentinteractioncontroller immediately, WITHOUT having to show a preview (– presentPreviewAnimated:) or an action sheet (– presentOpenInMenuFromRect:inView:animated:). Seems to me that it's the only way, but I might be missing something.

-(void) saveToInstagram {
    NSURL *url;
    UIDocumentInteractionController *dic;
    CGRect rect = CGRectMake(0 ,0 , 0, 0);
    UIImage *i = imageView.image;
    CGRect cropRect = CGRectMake(i.size.width - 306 ,i.size.height - 306 , 612, 612);
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.ig"];
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], cropRect);
    UIImage *img = [[UIImage alloc] initWithCGImage:imageRef];
    CGImageRelease(imageRef);

    [UIImageJPEGRepresentation(img, 1.0) writeToFile:jpgPath atomically:YES];
    url = [[NSURL alloc] initFileURLWithPath:jpgPath];
    dic = [self setupControllerWithURL:url usingDelegate:self];
    [dic presentOpenInMenuFromRect:rect inView:self.view animated:YES];
    [dic retain];
    [img release];
    [url release];
}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;

    return interactionController;
}

- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller {

}

回答1:

First save your JPEG with a .ig or .igo extension instead of .jpg or .jpeg then create a NSURL with a file:// directive. Also I used the .igo extension and the UTI com.instagram.exclusivegram for exclusivity to Instagram but you can use the .ig extension and the UTI com.instagram.gram

For Example:

// URL TO THE IMAGE FOR THE DOCUMENT INTERACTION
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];

docInteractionController.UTI = @"com.instagram.exclusivegram";
[self setupDocumentControllerWithURL:igImageHookFile];

// OPEN THE HOOK
[docInteractionController presentOpenInMenuFromRect:self.frame inView:self animated:YES];


回答2:

The only way to do it would be if Instagram registers a custom URL handler (e.g. igram://) and can accept data either as part of that URL (e.g. base64 encoded) or via the pasteboard.

Basically the limitations that UIDocumentInteractionController (and this technique) workaround are:

  1. Apps are not allowed to query for or directly launch other apps.
  2. Apps cannot generally open files from the sandboxed user directory of other apps.