Uploading photos to Instagram via your own iOS app

后端 未结 8 1348
执笔经年
执笔经年 2020-12-23 09:57

Instagram recently made a change to their API policy which allowed developers to post pictures to the Instagram platform via their own app. Several other techniques we\'re p

相关标签:
8条回答
  • 2020-12-23 10:53

    I have used below code for sharing photo in Instagram.

        NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
        if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not
        {
            NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
            NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
            NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
            NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"insta.igo"]]; //add our image to the path
            [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
            NSLog(@"image saved");
            
            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];
            NSLog(@"url Path %@",igImageHookFile);
            
            self.documentController.UTI = @"com.instagram.exclusivegram";
           // self.documentController = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
            
        
            self.documentController=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
            NSString *caption = @"#Your Text"; //settext as Default Caption
            self.documentController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",caption],@"InstagramCaption", nil];
            [self.documentController presentOpenInMenuFromRect:rect inView: self.view animated:YES];
        }
        else
        {
            UIAlertView *errMsg = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"No Instagram Available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [errMsg show];
        }
        

    0 讨论(0)
  • 2020-12-23 10:57

    You can simply use UIActivityViewController as well,

    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"instagram://app"]]) //check for App is install or not
        {
            NSArray * activityItems = [NSArray arrayWithObjects:newImage, nil];
            UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
            activityController.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, nil];
            
            [self presentViewController:activityController animated:YES completion:nil];
            
            //after sent
            [activityController setCompletionWithItemsHandler:^(NSString *act, BOOL done, NSArray *returnedItems, NSError *activityError){
                
                if (done){
                    
                    UIAlertController *alert;
                    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
                        [alert dismissViewControllerAnimated:YES completion:nil];
                    }];
                    
                    alert = [UIAlertController alertControllerWithTitle:@"Successfully Posted!" preferredStyle:UIAlertControllerStyleAlert];
                    [alert addAction: ok];
                    [self presentViewController:alert animated:YES completion:nil];
                    
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
            }];
    }
    
    0 讨论(0)
提交回复
热议问题