Search code examples
iosios6

Sharing image exclusively with another app


I have two I apps, sender and receiver, and I want to exclusively share a photo with receiver, how can I share directly with the receiver so I don't have to present an action sheet to the user?

I know I can use this to open an app:

[[UIApplication sharedApplication] openURL:recieverURL];

But I don't know how to send a photo to the app unless I use UIDocumentInteractionController which then I have to use an action sheet:

// within sender app
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"reciever://"]]) {

    NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"filename.jpg"], 1.0);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fullPathToFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"cameraawesome.ca"];
    [imageData writeToFile:fullPathToFile atomically:NO];

    self._interactionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@", self._filePath]]];
    self._interactionController.UTI = @"com.myapp.exclusive";
    self._interactionController.delegate = self; 

    [self._interactionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}

Solution

  • Use a private named pasteboard (UIPasteboard pasteboardWithName:create:). The sender app pastes the image to the named pasteboard. Then the sender app launches the receiver app with its custom URL scheme. Pass enough information on the URL so the receiver app knows that an image is available in the pasteboard. Possibly include the name of the pasteboard and the pasteboard type used to save the image. The receiver app can use this information to access the named pasteboard and read the image.