iOS 9 和 10 的 Instagram 分享按钮
Instagram Sharing button for iOS 9 & 10
我是编程新手,我编写了一个即将完成的 iOS 应用程序,现在我想将内容(图像、文本和 URL)共享功能集成到我的应用程序中,我已经实现了 Facebook 和 Twitter 共享功能,但是两天以来我一直停留在 Instagram 共享功能上。
我研究了很多 material(早期在这里 Whosebug, also tried MGInstagram 和少数其他第三方 API 提出的问题)但所有这些都已经过时并且无法在 iOS9 上工作& iOS10,
我试图从 Instagram 网站获得一些帮助,但我也没有被清除。
任何人都可以定义一些简单的方法来将 Instagram 共享按钮代码集成到我的 iOS 应用程序中,并明确定义来自 Instagram 网站的身份验证过程,例如开发人员所需的令牌和其他权限。
谢谢
这应该会在设备上打开 Instagram 并将照片分享到 Instagram。
- (IBAction)imageShareToInsta:(id)sender {
NSString *imagePath = [NSString stringWithFormat:@"%@/image.igo", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]];
[[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
[UIImagePNGRepresentation(self.imageView.image) writeToFile:imagePath atomically:YES];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
docController.delegate=self;
docController.UTI = @"com.instagram.exclusivegram";
[docController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}
在您的 ViewController.h 文件中声明 <UIDocumentInteractionControllerDelegate>
希望它能帮助你。
根据您对 iOS 9 和 10 的期望问题,所以我对两者都进行了解释。
我正在使用 Instagram 的标准文档 API,定义见 InstagramiPhone Hooks
第一步:
打开您的 info.plist 文件并添加 LSApplicationQueriesSchemes
并添加您要在 canOpenURL
中调用的 URL 方案,就像这样。
它也在 WWDC 2015 Session 703 中由 kitty scatter.
定义
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
它将打开安装在您设备中的 instargram 应用程序。
注意由于所需应用程序不可用,它永远无法在您的模拟器中运行。
第二步:
在你的 .h 文件中你的 @interface
行结束添加这个 <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,则无需执行第一步,您应该遵循第三步的第二行代码。
希望它能像水流一样顺畅。 . . :)
我是编程新手,我编写了一个即将完成的 iOS 应用程序,现在我想将内容(图像、文本和 URL)共享功能集成到我的应用程序中,我已经实现了 Facebook 和 Twitter 共享功能,但是两天以来我一直停留在 Instagram 共享功能上。 我研究了很多 material(早期在这里 Whosebug, also tried MGInstagram 和少数其他第三方 API 提出的问题)但所有这些都已经过时并且无法在 iOS9 上工作& iOS10, 我试图从 Instagram 网站获得一些帮助,但我也没有被清除。
任何人都可以定义一些简单的方法来将 Instagram 共享按钮代码集成到我的 iOS 应用程序中,并明确定义来自 Instagram 网站的身份验证过程,例如开发人员所需的令牌和其他权限。
谢谢
这应该会在设备上打开 Instagram 并将照片分享到 Instagram。
- (IBAction)imageShareToInsta:(id)sender {
NSString *imagePath = [NSString stringWithFormat:@"%@/image.igo", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]];
[[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
[UIImagePNGRepresentation(self.imageView.image) writeToFile:imagePath atomically:YES];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
docController.delegate=self;
docController.UTI = @"com.instagram.exclusivegram";
[docController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}
在您的 ViewController.h 文件中声明 <UIDocumentInteractionControllerDelegate>
希望它能帮助你。
根据您对 iOS 9 和 10 的期望问题,所以我对两者都进行了解释。 我正在使用 Instagram 的标准文档 API,定义见 InstagramiPhone Hooks
第一步:
打开您的 info.plist 文件并添加 LSApplicationQueriesSchemes
并添加您要在 canOpenURL
中调用的 URL 方案,就像这样。
它也在 WWDC 2015 Session 703 中由 kitty scatter.
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
它将打开安装在您设备中的 instargram 应用程序。
注意由于所需应用程序不可用,它永远无法在您的模拟器中运行。
第二步:
在你的 .h 文件中你的 @interface
行结束添加这个 <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,则无需执行第一步,您应该遵循第三步的第二行代码。
希望它能像水流一样顺畅。 . . :)