Reputation: 443
I'm using UIDocumentInteractionController to share a picture in Instagram and other apps like Whatsapp, Google Drive, Dropbox... (as many of you already know the Instagram pictures has the ".ig" or ".igo" file extensions). If I share the picture.ig in other apps instead of Instagram then the pictures appears like a file and not like a picture. So I implement the delegate method:
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
if (![application isEqualToString:@"com.burbn.instagram"]) {
NSURL *newPath;
NSString *oldPathStr = [controller.URL absoluteString];
NSString *newPathStr = [oldPathStr stringByReplacingOccurrencesOfString:@"ig" withString:@"jpg"];
newPath = [NSURL URLWithString:newPathStr];
controller.URL = newPath;
controller.UTI = @"public.jpeg";
}}
That way if the app is not Instagram I could share the other version of the file in jpg (I have both .ig and .jpg versions stored in disk).
But, the problem is that DocumentInteractionController doesn't call this delegate method when I am trying to share the picture with the system apps like Mail, Messages, Twitter or Facebook so I could't use the jpg URL and people receive a .ig file in their iPhones or laptops that they don't know how to open. If I try to open other apps like Whatsapp there isn't any problem and the delegate is called.
Do you know how could I take control of this before the system open the mail composer, message composer or social composer?
Thank you so much.
Upvotes: 2
Views: 3623
Reputation: 443
I find a solution. First of all I configure a UIDocumentInteractionController
- (void)setupDocumentControllerWithURL:(NSURL *)url {
if (self.docInteractionController == nil) {
self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
self.docInteractionController.delegate = self;
}
else {
self.docInteractionController.URL = url;
}
}
Then open the Instagram .ig file with the docInteractionController, if you open the .jpg instead of the .ig file, then the Instagram app wouldn't appear in the "Open In App" menu
//share the Instagram .ig picture fileURL
[self setupDocumentControllerWithURL:fileURL];
[self.docInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
Then you have to change the URL inside a delegate method to allow the system apps to share the .jpg instead a file with .ig extension (only Instagram and a few other apps recognize the .ig file as a picture
- (void)documentInteractionControllerWillPresentOptionsMenu:(UIDocumentInteractionController *)controller {
NSURL *theUrl = controller.URL;
NSString *theInstagramPath = theUrl.path;
NSString *jpgPath = [theInstagramPath stringByReplacingOccurrencesOfString:@"ig" withString:@"jpg"];
NSURL *jpgURL = [NSURL fileURLWithPath:jpgPath];
controller.URL = jpgURL;
}
After that Mail app, Messages, Twitter and Facebook (and non system apps) will open the file as a jpg. But we have to change the url again if we want to open the file inside Instagram:
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
//Check if the app that is calling is Instagram
if ([application isEqualToString:@"com.burbn.instagram"]) {
NSURL *newPath;
NSString *oldPathStr = [controller.URL absoluteString];
NSString *newPathStr = [oldPathStr stringByReplacingOccurrencesOfString:@"jpg" withString:@"ig"];
newPath = [NSURL URLWithString:newPathStr];
controller.URL = newPath;
}}
And that's all. It seems that the willBeginSendingToApplication: is not being called if you select a system app in the menu.
Upvotes: 4
Reputation: 25692
you have missed to set the delegate of your UIDocumentInteractionController instance.
after creation of UIDocumentInteractionController do like this
YourUIDocumentInteractionControllerObject.delegate = ObjectWhichHaswillBeginSendingToApplicationMethodImplementation;
Upvotes: 0