Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS9 - Sharing to Instagram (w/ hooks) not working

I'm currently updating one of my apps to be iOS9 compatible, and I'm having trouble with the share to Instagram function. I'm using the Instagram hooks as stated on their developer site: (https://instagram.com/developer/mobile-sharing/iphone-hooks/)

The image I wish to share is being generated successfully, with the .igo suffix, and the functionality is still working as intended on iOS8. It just seems to have broken with the new version of iOS.

Here's the code for sharing to Instagram, using the UIDocumentInteractionController:

NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];

if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {

    //convert image into .png format.
    NSData *imageData = UIImagePNGRepresentation(image);

    //create instance of NSFileManager
    NSFileManager *fileManager = [NSFileManager defaultManager];

    //create an array and store result of our search for the documents directory in it
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    //create NSString object, that holds our exact path to the documents directory
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //add our image to the path
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"insta.igo"]];

    //finally save the path (image)
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

    CGRect rect = CGRectMake(0 ,0 , 0, 0);
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();

    NSString *fileNameToSave = [NSString stringWithFormat:@"Documents/insta.igo"];
    NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:fileNameToSave];
    NSLog(@"jpg path %@",jpgPath);

    NSString *newJpgPath = [NSString stringWithFormat:@"file://%@",jpgPath];
    NSLog(@"with File path %@",newJpgPath);

    NSURL *igImageHookFile = [[NSURL alloc]initFileURLWithPath:newJpgPath];
    NSLog(@"url Path %@",igImageHookFile);

    self.documentController = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
    [self.documentController setDelegate:self];
    [self.documentController setUTI:@"com.instagram.exclusivegram"];
    [self.documentController presentOpenInMenuFromRect:rect inView:self.view animated:YES];

} else {
    NSLog (@"Instagram not found");
}

It's probably worth mentioning I've already configured the URL schemes in the info.plist as required with the iOS9 changes.

The UIDocumentInteractionController does appear, and has the option 'Copy to Instagram'. Pressing this option just leads to the controller being dismissed, with no log messages or breakpoints being called on the controller's delegate (set to self; the view controller).

If anyone has, or has had trouble with this, it would be great to hear your thoughts, or better yet, how it was solved.

Update

It's also worth mentioning, on an iOS8 device, the Document Interaction Controller shows an 'Open in Instagram' button. The iOS9 device shows a 'Copy to Instagram' button.

like image 718
Andy Shephard Avatar asked Sep 15 '15 19:09

Andy Shephard


2 Answers

You have to add a new key to your Info.plist file; it's an iOS 9 change for URL schemes. Check out the first answer for this question: iOS 9 not opening Instagram app with URL SCHEME. And just FYI, iOS 9 changes the "Open in Instagram" title for the UIDocumentInteractionController to "Copy to Instagram." Not sure why.

like image 122
embersofadyingfire Avatar answered Nov 17 '22 09:11

embersofadyingfire


After changing this line of code:

NSURL *igImageHookFile = [[NSURL alloc] initFileURLWithPath:newJpgPath];

to this:

NSURL *igImageHookFile = [NSURL URLWithString:newJpgPath];  

The Instagram-share function for iOS 9 is now working. It seems that the previous line of code, converting the NSString to an NSURL would place "--://file" at the end of the URL path, which doesn't seem to register well with iOS 9. Simply converting the NSString to NSURL without initialising as a file URL seems to work.

like image 17
Andy Shephard Avatar answered Nov 17 '22 10:11

Andy Shephard