2016-10-28 16 views
0

我是編程新手,我編寫了幾乎完成的iOS應用程序,現在我想將內容(圖像,文本和URL)共享功能集成到我的應用程序中,已經實現了Facebook和Twitter共享功能,但是我兩天後就停留在Instagram分享功能上。 我研究過很多材料(剛纔問此問題StackOverFlow,也試過MGInstagram和其他一些第三方的API),但所有這些都已經過時&不工作iOS9 & iOS10, 我試圖從Instagram網站達到一定的幫助,但我也沒有清除。用於iOS 9和10的Instagram分享按鈕

任何人都可以定義一些簡單的方法來集成Instagram的我的iOS應用程序共享按鈕代碼也可以定義從Instagram網站的身份驗證過程,就像開發人員需要的其他權限。

謝謝

+0

看看這個鏈接https://cocoapods.org/pods/instagram-ios-sdk –

+0

正在檢查指定的鏈接,我會從Instagram獲取客戶端ID,但可以請你分享我按下按鈕的實際代碼事件當我按下按鈕,它分享圖片一些文字和網址爲我的應用程序到Instagram的 –

+0

好吧,我已經對後者從Instagram的和JSON響應並顯示用戶的最新Instagram的照片在ImageView的,如果一個樣本項目工作你想我可以上傳到Github,你可以查看項目 –

回答

2

根據你所希望的iOS 9和10的問題所以我解釋了兩者。 我使用的Instagram,S在Instagram的

第一步在iPhone Hooks定義標準文檔API:

打開Info.plist文件,並添加LSApplicationQueriesSchemes並添加你要在打電話給你的URL方案的canOpenURL是這樣的。 它也在WWDC 2015 Session 703kitty 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,那麼不需要做第一步,你應該遵循第三步的第二行代碼。

希望它像水流一樣順暢地工作。 。 。:)

+0

讓我試試,我馬上回來。 –

+0

感謝@Faiz_Fareed你的答案可以正常使用 –

+0

我試圖把它標記,一個彈出消息告訴我,我至少需要15個聲譽標記爲向上。所以只是我批准接受。 謝謝 –

0

這應該在設備上打開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]; 
    } 

聲明<UIDocumentInteractionControllerDelegate>在ViewController.h文件 希望它可以幫助你。

+0

你確定嗎?這段代碼有效嗎?我的意思是測試? –

+0

但只有在設備不模擬器因爲它需要安裝instagram。測試一下,然後接受我的回答 –

+0

從哪裏複製的?你能給我一些參考嗎? –