On iOS 7.1.1, I can share image by this code on Whatsapp..
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:#"whatsapp://app"]]){
UIImage * iconImage = [UIImage imageNamed:#"image.png"];
NSString * savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
self.docController.UTI = #"net.whatsapp.image";
self.docController.delegate = self;
//[self.docController setAnnotation:#{#"WhatsappCaption" : #"https://itunes.apple.com/us/app/epic-ar/id535122470?ls=1&mt=8"}];
[self.docController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:appDelegate.window.rootViewController.view animated: YES];
}
But I want to share video too, I follow tutorial on Wahtsapp tutorial
But How can be the code of video path ?
I mean, What is the alternative object of UIImage object to display video ?
Also, can I share "Link" ?
Thank you,
It took me a while, but I put the pieces together
references: Share image/text through WhatsApp in an iOS app
// http://www.whatsapp.com/faq/en/iphone/23559013
//---NEXT LINE OF CODE IS OPTIONAL AND NOT RECOMMENDED, BUT YOU CAN USE IT TO TEST TO SEE IF THEY HAVE THE WHATSAPP INSTALLED
// if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:#"whatsapp://app"]]){
NSString * savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wam"];
savePath = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = #"net.whatsapp.movie";
_documentInteractionController.delegate = (id)self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
This is only to answer your main question: Share video on whatsapp
Related
It successfully redirects to the Whatsapp app.There is no preview frame and when tapped Send, an error messages pops up "This video could not be sent. Please choose a different video.
Here is my code.
- (void)shareVideo {
NSLog(#"[WhatsAppShare] sharing video");
//NSString *nativePath = [[NSString alloc] initWithCString:path encoding:NSASCIIStringEncoding];
NSString *nativePath=[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"];
// Save video to path in documents directory
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wam"];
if([[NSFileManager defaultManager] fileExistsAtPath:savePath]){
if([[NSFileManager defaultManager] removeItemAtPath:savePath error:nil]){
[self shareVideoAtNativePath:nativePath SavePath:savePath];
}
} else {
[self shareVideoAtNativePath:nativePath SavePath:savePath];
}}
- (void)shareVideoAtNativePath:(NSString*)nativePath SavePath:(NSString*)savePath{
NSError*error;
BOOL isSuccess=[[NSFileManager defaultManager] copyItemAtPath:nativePath toPath:savePath error:&error];
if(isSuccess){
// Create interaction controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
self.documentInteractionController.UTI = #"net.whatsapp.movie";
self.documentInteractionController.delegate = self;
[self.documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 1, 1)
inView:[self view]
animated:YES];
} else{
NSLog(#"error %#", error);
}
}
We have observed the exact same problem. Everything was working until a recent update of WhatsApp. This is probably a bug on WhatsApp side.
Here is a workaround for this problem:
Do not use a wam file, use the mp4 file directly. So in your case, just call
[self shareVideoAtNativePath:nativePath SavePath:nativePath];
Change the UTI to public Mpeg4:
self.documentInteractionController.UTI = #"public.mpeg-4";
This seems to have solved our issue. However, there is a drawback that, the share dialog now contains many other apps/services that can open mp4 files.
We've faced the exact same issue.
The official .wam format brings up only WhatsApp in the share dialog, but fails to forward the video.
Using .m4v format is working for us. A few more options are displayed along with WhatsApp (Open in WhatsApp is the option we want). We are displaying an alert saying "Please select Whatsapp on the next screen" before sending user to the share dialog.
File format: m4v
UTI: net.whatsapp.movie
Please refer to the working code below:
UIDocumentInteractionController *documentInteractionController;
-----
-----
- (void)shareVideoViaWhatsApp:(NSURL*)url{
// Creating temp video to share specifically on whatsapp.
NSString *cachesFolder = [NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:#"video.m4v"]];
NSURL *file = [NSURL fileURLWithPath:cachesFolder];
[[NSData dataWithContentsOfURL:url] writeToURL:file options:NSDataWritingAtomic error:nil];
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL: file ];
documentInteractionController.UTI = #"net.whatsapp.movie";
documentInteractionController.delegate = self;
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
// In code Use share GIF and Video for WhatsApp....
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wam"];
savePath = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:_videourl];
_documentInteractionController.UTI = #"net.whatsapp.movie";
_documentInteractionController.delegate = (id)self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
Hi this is my code to open Instagram App directly with an image. As stated in Apple document relative to UIDocumentInteractionController and in Instagram hook page this snippet of code should open instagram directly with the image, but in all testing conditions it open the action sheet with all the app compatible with the extension saved.
I've also whitelisted instagram in LSApplicationQueriesSchemes.
How can i achive this result? It's a bug or a new security feature of IOS 9?
Thanks in advance for your help
NSURL *instagramURL = [NSURL URLWithString:#"instagram:/"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.ig"];
[UIImagePNGRepresentation(capturedImage) writeToFile:getImagePath atomically:YES];
NSURL *imageFileURL =[NSURL fileURLWithPath:getImagePath];
NSLog(#"imag %#",imageFileURL);
self.dic.delegate = self;
self.dic.UTI = #"com.instagram.photo";
self.dic = [self setupControllerWithURL:imageFileURL usingDelegate:self];
[self.dic presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
} else //prompt for app missing
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL
usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController =
[UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}
if you save the image as .igo the only apps i see in the actionsheet are "Instagram" "Notes" and "other.
edit: I have't found a way to open Instagram directly with an image it only seems possible to open it directly with
NSURL *instagramURL = [NSURL URLWithString:#"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
but you can't add an image with that
edit2: I forgot to add that for the .igo format you need to use self.dic.UTI = "com.instagram.exclusivegram"
My app can successfully upload photos to Instagram using UIDocumentInteractionController, but I am unable to successfully upload the photo with a caption.
The Instagram docs tell you to do this:
To include a pre-filled caption with your photo, you can set the
annotation property on the document interaction request to an
NSDictionary containing an NSString under the key "InstagramCaption".
Note: this feature will be available on Instagram 2.1 and later.
I have tried this five times now. I have tried creating an NSDictionary several different ways including using literal syntax but I still cannot get it to work so it has to be something wrong with Instagram.
Here is a sample of my code that sets up the document and opens the photo in the Instagram iOS app:
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.UTI = #"com.instagram.photo";
self.dic.annotation = #{#"InstagramCaption" : #"Here's my caption!"};
self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
[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");
}
}
Notice how I am setting self.dic.annotation properly and follow Instagram's guidelines.
The photo opens up fine in the Instagram iOS app and will successfully upload to the user's profile, but the caption is never included.
It looks like people were having this same problem back in 2013, but then the issue was fixed.
I just figured it out.
All I did was move my statement where I define self.dic.annotation underneath the following statement:
self.dic = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
The above statement is returning a new instance of UIDocumentInteractionController and obviously none of it's properties are set.
EDIT: I would advise not to use the original code I posted unless you are willing to make changes. I just noticed that there are two statements returning a UIDocumentInteractionController instance and they are both right next to each other:
self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.dic = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
This code came from a tutorial so I cannot vouch for it's quality. If you want to use it, make sure to analyze it line for line so you don't run into any of the confusion that I did.
EDIT 2:
Here is the final code sample that works perfectly. You should be able to use this with no errors:
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");
}
I want to share an image via whatsapp.
I'm using UIDocumentInteractionController with the following code.
Can I skip the menu that is opened (and the user selects whatsapp) and just act as if he selected the first option?
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:#"whatsapp://app"]])
{
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = #"net.whatsapp.image";
_documentInteractionController.delegate = self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated: YES];
}
WhatsApp was updated with iOS 8 SDK with ShareKit extension.
So now sharing to WhatsApp is 'default' in UIActivityController and there is no need to implement UIDocumentInteractionController to share the image.
Thanks to a couple other posts here , I've successfully be able to use the Instagram iPhone hooks to open Instagram and present it with a photo successfully from my application.
(I've made my ViewController class a delegate of UIDocumentInteractionController, and alloc/init'ed a nonatomic/retain property of UIDocumentInteractionController...
However, the key that I put into my NSDictionary that I place in the document controller annotation property will not seem to carry over to Instagram - the caption area is just empty.
How do I deal with this?
Here is my method:
- (void) uploadImageToInstagram:(UIImage *)imageToUpload {
NSLog(#"postToInstagramInBackground");
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
// Upscale to 612x612
imageToUpload = [self upscaleImage:imageToUpload];
// Get JPG + IGO Format
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/generate.igo"];
[UIImageJPEGRepresentation(imageToUpload, 1.0) writeToFile:savePath atomically:YES];
// Paths
NSURL *imageURL = [NSURL fileURLWithPath:savePath];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#",savePath]];
// Setup DocController
self.docController.UTI = #"com.instagram.photo";
self.docController = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.docController.annotation = [NSDictionary dictionaryWithObject:#"MyApp" forKey:self.caption.text];
[self.docController setURL:imageURL];
[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES ];
}
else {
NSLog(#"Instagram not installed in this device!\nTo share image please install Instagram.");
}
}
There are a few methods that are called here that I haven't included, one that upscales the image to make sure its 612x612, and one setups the file url for the document controller.
Proper syntax should be
self.docController.annotation = [NSDictionary dictionaryWithObject:self.caption.text forKey:#"InstagramCaption"];