根據你所希望的iOS 9和10的問題所以我解釋了兩者。 我使用的Instagram,S在Instagram的
第一步在iPhone Hooks定義標準文檔API:
打開Info.plist文件,並添加LSApplicationQueriesSchemes
並添加你要在打電話給你的URL方案的canOpenURL
是這樣的。 它也在WWDC 2015 Session 703由kitty scatter定義。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
它會打開安裝在您的設備上的instargram應用程序。
note它將永遠不會在你的模擬器工作,因爲無法使用所需的應用程序。
第二步:
在.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或更高版本,請執行步驟一,並刪除第三步中提到的第二行代碼。如果你使用的是iOS 9到9.2.3,那麼不需要做第一步,你應該遵循第三步的第二行代碼。
希望它像水流一樣順暢地工作。 。 。:)
看看這個鏈接https://cocoapods.org/pods/instagram-ios-sdk –
正在檢查指定的鏈接,我會從Instagram獲取客戶端ID,但可以請你分享我按下按鈕的實際代碼事件當我按下按鈕,它分享圖片一些文字和網址爲我的應用程序到Instagram的 –
好吧,我已經對後者從Instagram的和JSON響應並顯示用戶的最新Instagram的照片在ImageView的,如果一個樣本項目工作你想我可以上傳到Github,你可以查看項目 –