Instantly share code, notes, and snippets.
Forked from BenBarahona/Share Image to instagram & Whatsapp
Created
October 27, 2015 01:13
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save verma-manish58/5d772d6ef27f3c8d0281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (IBAction)shareInstagramClicked:(id)sender | |
{ | |
//Instagram URL Scheme | |
NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"]; | |
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) | |
{ | |
NSString *message = @""; | |
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; | |
//Image must be saved as .igo to phoe directory in order to work. This will be removed on the doc interaction controller delegate | |
NSString *instagramTempImagePath = [documentsDirectory stringByAppendingPathComponent:@"InstagramImage.igo"]; | |
//este metodo getNSDataFromImage es de es una clase helper pero basicamente solo es sacar el NSData de una UIImage | |
NSData *imageData = [Util getNSDataFromImage:self.imageToShare isPNG:NO]; | |
[imageData writeToFile:instagramTempImagePath atomically:YES]; | |
NSURL *imageUrl = [NSURL fileURLWithPath:instagramTempImagePath]; | |
//ivar de tipo UIDocumentInteractionController | |
if(!docInterationController) | |
{ | |
docInterationController = [[UIDocumentInteractionController alloc] init]; | |
} | |
docInterationController.delegate = self; | |
docInterationController.UTI = @"com.instagram.exclusivegram"; | |
docInterationController.URL = imageUrl; | |
docInterationController.annotation = [NSDictionary dictionaryWithObject:message forKey:@"InstagramCaption"]; | |
[docInterationController presentOpenInMenuFromRect:CGRectZero inView:self animated:YES]; | |
} | |
else | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:APP_NAME message:@"This feature requires that you have installed Instagram" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil, nil]; | |
[alert show]; | |
} | |
} | |
- (IBAction)shareWhatsappClicked:(id)sender | |
{ | |
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://app"]; | |
if ([[UIApplication sharedApplication] canOpenURL:whatsappURL]) | |
{ | |
//For sharing messages or URLs | |
if(self.urlToShare){ | |
NSString *customMessage = [NSString stringWithFormat:@"made with #kamio %@", self.urlToShare]; | |
customMessage = [customMessage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
NSString *urlString = [NSString stringWithFormat:@"whatsapp://send?text=%@", customMessage]; | |
whatsappURL = [NSURL URLWithString:urlString]; | |
[[UIApplication sharedApplication] openURL:whatsappURL]; | |
}else{ | |
//For image sharing | |
if(self.imageToShare) | |
{ | |
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; | |
NSString *whatsappTempImagePath = [documentsDirectory stringByAppendingPathComponent:@"WhatsAppImage.wai"]; | |
NSData *imageData = [Util getNSDataFromImage:self.imageToShare isPNG:NO]; | |
[imageData writeToFile:whatsappTempImagePath atomically:YES]; | |
NSURL *imageUrl = [NSURL fileURLWithPath:whatsappTempImagePath]; | |
if(!docInterationController) | |
{ | |
docInterationController = [[UIDocumentInteractionController alloc] init]; | |
} | |
docInterationController.delegate = self; | |
docInterationController.UTI = @"net.whatsapp.image"; | |
docInterationController.URL = imageUrl; | |
if(self.urlToShare){ | |
docInterationController.annotation = [NSString stringWithFormat:@"made with #kamio %@",self.urlToShare]; | |
}else { | |
docInterationController.annotation = [NSString stringWithFormat:@"made with #kamio"]; | |
} | |
[docInterationController presentOpenInMenuFromRect:CGRectZero inView:self animated:YES]; | |
} | |
}/* | |
else{ | |
NSString *urlString = [NSString stringWithFormat:@"whatsapp://send?text=%@", self.urlToShare]; | |
whatsappURL = [NSURL URLWithString:urlString]; | |
[[UIApplication sharedApplication] openURL:whatsappURL]; | |
}*/ | |
} | |
else | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:APP_NAME message:@"This feature requires that you have installed Whatsapp" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil, nil]; | |
[alert show]; | |
} | |
} | |
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application | |
{ | |
//La imagen se mandocon exito a instagram, la borramos del directorio porque en realidad no tiene que seguir ahi | |
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; | |
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"InstagramImage.igo"]; | |
NSError *error = nil; | |
[[NSFileManager defaultManager] removeItemAtPath:path error:&error]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment