根据您对 iOS 9 和 10 的期望问题,所以我正在为两者进行解释。
我在 Instagram 使用 iPhone Hooks 中定义的 Instagram 标准文档 API
第一步:
打开您的 info.plist 文件并添加 LSApplicationQueriesSchemes 并添加您将在 canOpenURL 中调用的 URL 方案,如下所示。
它也是由 kitty scatter 在WWDC 2015 Session 703 中定义的。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
它将打开安装在您设备中的 instargram 应用程序。
注意由于所需的应用程序不可用,它永远无法在您的模拟器中工作。
第二步:
在@interface 行结束的.h 文件中,将此<UIDocumentInteractionControllerDelegate> 添加到同一行@interface
第三步:
在您的 Instagram 分享按钮的操作中添加以下代码以分享图片
// This is your Image you want to share. you can write
UIImage *image = imageView.image;
//Add this line of code instead of doing step One if you are at early version of iOS9 till iOS 9.2.3 but if you are using iOS9.5 or above like as iOS10 not use this line and do step1 instead of writing this line of code
NSURL *instagram = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagram]) {
//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];
NSURL *igImageHookFile = [NSURL URLWithString:newJpgPath];
NSLog(@"url Path %@",igImageHookFile);
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
documentController.delegate = self;
[documentController setUTI:@"com.instagram.exclusivegram"];
[documentController presentOpenInMenuFromRect:rect inView:self.view animated:YES];
} else {
// NSLog (@"Instagram not found");
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Install Instagram"
message:@" Before Sharing to Instagram, Please Install Instagram app to your Device. "
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Thanks" style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
最后说明:
如果您使用 iOS SDK 9.5 或更高版本(如 10),请按照第一步操作。并删除第三步中提到的第二行代码。如果您使用 iOS 9 到 9.2.3,则无需执行第一步,您应该遵循第三步的第二行代码。
希望它能像水流一样顺畅地工作。 . . :)