簡體   English   中英

適用於iOS 9和10的Instagram分享按鈕

[英]Instagram Sharing button for iOS 9 & 10

我是編程的新手,我已經編寫了一個即將完成的iOS應用程序,現在我想將內容(圖像,文本和URL)共享功能集成到我的應用程序中,我已經實現了Facebook和Twitter共享功能,但是我自兩天以來一直堅持使用Instagram分享功能。 我研究過很多材料(剛才問此問題的StackOverflow ,也試過MGInstagram和其他一些第三方的API),但所有這些都是過時的和不工作的iOS9&iOS10,我試圖實現從一些幫助Instagram的網站,但有太多我沒有被清除。

任何人都可以定義一種簡單的方法來在我的iOS應用程序中集成Instagram共享按鈕代碼的方法,也可以從Instagram網站明確定義身份驗證過程,例如令牌和開發人員所需的其他權限。

謝謝

根據您對iOS 9和iOS 10的期望問題,我為兩者進行解釋。 我正在使用Instagram的iPhone掛鈎中定義的Instagram標准文檔API

第1步:

打開info.plist文件,然后添加LSApplicationQueriesSchemes並添加您要在canOpenURL調用的URL方案,如下所示。 WWDC 2015 Session 703中kitty scatter也定義了它。

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>instagram</string>
</array> 

它將打開您設備中安裝的instargram應用。

注意由於必需的應用程序不可用,它將永遠無法在您的模擬器中工作。

第2步:

在您的@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), 請執行步驟1,並刪除第三步中提到的第二行代碼。 如果您使用的是iOS 9至9.2.3,則無需執行第一步,則應遵循第三步的第二行代碼。

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

這應該在設備上打開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>希望對您<UIDocumentInteractionControllerDelegate>幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM