2017-05-24 86 views
0

我想分享图像到Instagram。文档(https://www.instagram.com/developer/mobile-sharing/iphone-hooks/)仅针对show Instagram说明了关于UTI“com.instagram.exclusivegram”和图像扩展名“igo”。但UIDocumentInteractionController为我显示了很多选项。不仅仅是Instagram。有什么方法可以直接分享给Instagram? (见UIDocumentInteractionController单选项)Instagram的iPhone共享图像

UIDocumentInteractionController *docInteraction=[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:@".../image.igo"]]; 
[email protected]"com.instagram.exclusivegram"; 
BOOL presented=[docInteraction presentOpenInMenuFromRect:CGRectMake(0, self.view.frame.size.height-1, self.view.frame.size.width, 1) 
                inView:self.view 
               animated:YES]; 

screenshot

+0

试试这是开放的instagram直接NSURL * instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@“instagram:// library?LocalIdentifier =%@”,asset.localIdentifier]];如果([[UIApplication sharedApplication] canOpenURL:instagramURL]){ [[UIApplication sharedApplication] openURL:instagramURL]; } else { NSLog(@“Instagram not found”); } – Birendra

回答

0

使用此下方类在Instagram的份额和它是100%的工作。

文件名:InstagramSharing.h

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 

@interface InstagramSharing : NSObject <UIDocumentInteractionControllerDelegate> 
+ (InstagramSharing*)sharedData; 

@property (nonatomic, retain) UIDocumentInteractionController *documentController; 

-(void)instaGramWallPost:(NSString *)imgName andExt:(NSString *)ext inViewController:(UIViewController *)viewController; 

@end 

文件名:InstagramSharing.m

#import "InstagramSharing.h" 


@implementation InstagramSharing 
+ (id) sharedData 
    { 
     static InstagramSharing *singletonInstance = nil; 
     static dispatch_once_t onceToken; 

     dispatch_once(&onceToken, ^{ 
      if(!singletonInstance) { 
       singletonInstance = [[InstagramSharing alloc] init]; 
      } 
     }); 

     return singletonInstance; 
    } 

-(NSString*) getFilePath:(NSString *)fileName{ 
     // NSString *searchFilename = @"hello.pdf"; // name of the PDF you are searching for 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory]; 

     NSString *documentsSubpath; 
     while (documentsSubpath = [direnum nextObject]) 
     { 
      if (![documentsSubpath.lastPathComponent isEqual:fileName]) { 
       continue; 
      } 

      NSLog(@"found %@", documentsSubpath); 
     } 
     return documentsSubpath; 
    } 

-(void)instaGramWallPost:(NSString *)imgName andExt:(NSString *)ext inViewController:(UIViewController *)viewController 
    { 
     NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:ext]; 
     NSURL *myURL = [NSURL fileURLWithPath:path]; 

     NSData * imageData = [[NSData alloc] initWithContentsOfURL:myURL]; 
     UIImage *imgShare = [[UIImage alloc] initWithData:imageData]; 

     NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"]; 

     if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not 
     { 
      UIImage *imageToUse = imgShare; 
      NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
      NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.igo"]; 
      NSData *imageData=UIImagePNGRepresentation(imageToUse); 
      [imageData writeToFile:saveImagePath atomically:YES]; 
      NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath]; 

      NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:ext]; 
      NSURL *url = [NSURL fileURLWithPath:path]; 

      self.documentController=[[UIDocumentInteractionController alloc]init]; 
      self.documentController = [UIDocumentInteractionController interactionControllerWithURL:imageURL]; 
      self.documentController.delegate = self; 
      self.documentController.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Testing"], @"InstagramCaption", nil]; 
      self.documentController.UTI = @"com.instagram.exclusivegram"; 
      UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController; 
      [self.documentController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:viewController.view animated:YES]; 
     } 
     else { 
      //   DisplayAlertWithTitle(@"Instagram not found", @"") 
      NSLog(@"no image found"); 
     } 
    } 



@end 

如何以下行

使用

上共享行动呼吁

[[InstagramSharing sharedData] instaGramWallPost:@"nature1" andExt:@"jpeg" inViewController:self]; 
+0

不是。不仅仅是Instagram。 – user3026863

+0

@ user3026863没有得到你。 –