I found this code in another user question, but its objective-c and i need it in Swift. I tried to translate it, but when the apple share dialog appears and i touch Instagram, the application crashes.
The code is this one: from How to share an image on Instagram in iOS?
UIImage *screenShot = finalImage;
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.igo"];
[UIImagePNGRepresentation(screenShot) writeToFile:savePath atomically:YES];
CGRect rect = CGRectMake(0 ,0 , 0, 0);
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.igo"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];
self.dic = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
self.dic.UTI = @"com.instagram.photo";
self.dic.annotation = [NSDictionary dictionaryWithObject:@"Enter your caption here" forKey:@"InstagramCaption"];
[self.dic presentOpenInMenuFromRect:rect inView:self.view animated:YES];
NSURL *instagramURL = [NSURL URLWithString:@"instagram://media?id=MEDIA_ID"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[self.dic presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
} else {
NSLog(@"No Instagram Found");
}
Of course, in the example the user uses finalImage to declare the screenShot UIImage, so i simply did this to try: (i have an asset called posteo-01 in the project)
func postImage() {
var imagePost = UIImage(named: "posteo-01")
var savePath = documentsDirectory().stringByAppendingPathComponent("Test.igo")
UIImageJPEGRepresentation(imagePost, 100).writeToFile(savePath, atomically: true)
var rect = CGRectMake(0, 0, 0, 0)
var igImageHookFile = NSURL(string: "file://\(savePath)")
var dic = UIDocumentInteractionController(URL: igImageHookFile!)
dic.UTI = "com.instagram.photo"
dic.annotation = ["InstagramCaption": "hola"]
dic.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
var instagramURL = NSURL(string: "instagram://media?id=MEDIA_ID")
if(UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
dic.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
} else {
println("no instagram found")
}
}
func documentsDirectory() -> String {
let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
return documentsFolderPath
}
Thanks!
Already solve it:
var docController = UIDocumentInteractionController()
func postImage2() {
var instagramURL = NSURL(string: "instagram://app")
if(UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
var imagePost = UIImage(named: "posteo-01")
var fullPath = documentsDirectory().stringByAppendingPathComponent("insta.igo")
var imageData = UIImagePNGRepresentation(imagePost).writeToFile(fullPath, atomically: true)
var rect = CGRectMake(0, 0, 0, 0)
self.docController.UTI = "com.instagram.exclusivegram"
var igImageHookFile = NSURL(string: "file://\(fullPath)")
self.docController = UIDocumentInteractionController(URL: igImageHookFile!)
self.docController.annotation = ["InstagramCaption":"Text"]
self.docController.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
} else {
println("no instagram found")
}
}
func documentsDirectory() -> String {
let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
return documentsFolderPath
}