![](/img/trans.png)
[英]Instagram Sharing to Feed iOS not working (UIDocumentInteractionController)
[英]iOS9 - Sharing to Instagram (w/ hooks) not working
我目前正在更新我的一个应用程序与iOS9兼容,我在分享到Instagram功能时遇到了麻烦。 我正在使用他们开发者网站上所述的Instagram钩子:( https://instagram.com/developer/mobile-sharing/iphone-hooks/ )
我希望分享的图像是使用.igo后缀成功生成的,并且该功能仍在iOS8上按预期工作。 它似乎已经打破了新版本的iOS。
这是使用UIDocumentInteractionController共享到Instagram的代码:
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");
}
值得一提的是,我已根据iOS9更改的要求在info.plist中配置了URL方案。
UIDocumentInteractionController确实出现,并具有“Copy to Instagram”选项。 按此选项只会导致控制器被解除,控制器的委托上没有调用日志消息或断点(设置为self;视图控制器)。
如果有人或者遇到过这方面的麻烦,那么听听你的想法会更好,或者更好的是,它是如何解决的。
更新
值得一提的是,在iOS8设备上,文档交互控制器显示“在Instagram中打开”按钮。 iOS9设备显示“复制到Instagram”按钮。
更改此行代码后:
NSURL *igImageHookFile = [[NSURL alloc] initFileURLWithPath:newJpgPath];
对此:
NSURL *igImageHookFile = [NSURL URLWithString:newJpgPath];
iOS 9的Instagram共享功能现在正在运行。 似乎前一行代码,将NSString转换为NSURL会在URL路径的末尾放置“ - :// file”,这似乎不能很好地适用于iOS 9.简单地将NSString转换为NSURL没有初始化为文件URL似乎工作。
您必须在Info.plist文件中添加新密钥; 这是针对URL方案的iOS 9更改。 看看这个问题的第一个答案: iOS 9没有使用URL SCHEME打开Instagram应用程序 。 仅举,仅供参考,iOS 9将UIDocumentInteractionController的“Open in Instagram”标题更改为“Copy to Instagram”。 不知道为什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.