9

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.

Andy Shephard
  • 1,726
  • 17
  • 26

2 Answers2

17

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.

Andy Shephard
  • 1,726
  • 17
  • 26
1

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.

Community
  • 1
  • 1
embersofadyingfire
  • 523
  • 1
  • 4
  • 16
  • I had already stated in my question that I had added the URL scheme for Instagram. With the addition of the URL scheme, iOS 9 still refused to open Instagram via my app and the UIDocumentInteractionController. – Andy Shephard Sep 28 '15 at 20:32
  • whoops, I missed that part, but that's weird that it didn't work for you because it worked for me. – embersofadyingfire Sep 29 '15 at 00:32
  • It could be that you already had the correct initialisation method for NSURL, and were only missing the URL scheme. A console log message stating “This app is not allowed to query for scheme xxx” would appear if that were the case. Appreciate your answer though! – Andy Shephard Sep 29 '15 at 15:34