我正在尝试分享图片到Instagram。文档(https://www.instagram.com/developer/mobile-sharing/iphone-hooks/)仅针对show Instagram展示UTI“com.instagram.exclusivegram”和图片扩展名“igo”。但是UIDocumentInteractionController为我显示了很多选项。不仅是Instagram。有没有办法直接分享到Instagram? (请参阅UIDocumentInteractionController中的单个选项)
UIDocumentInteractionController *docInteraction=[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:@".../image.igo"]];
docInteraction.UTI=@"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];
答案 0 :(得分: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];