0

I use this code for sharing image on Whatsapp from my iOS app, but the Controller always go in else condition. Please give me a solution.

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]])
{
    UIImage *iconImage = [UIImage imageNamed:dict23[@"profileimg"]];
    NSString *savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];
    [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];       
    documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
    documentInteractionController.UTI = @"net.whatsapp.image";            
    documentInteractionController.delegate = self;

    [documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
} 
else 
{        
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
MWiesner
  • 8,868
  • 11
  • 36
  • 70
LC Nagar
  • 1
  • 2

4 Answers4

4

Use this code for sharing only text on whats app .

    NSString * msg = @"Hi! I am using app! download it at https://itunes.apple.com/us/app/google-search/id284815942?mt=8";

   msg = [msg stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
   msg = [msg stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
   msg = [msg stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
   msg = [msg stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
   msg = [msg stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
   msg = [msg stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
   msg = [msg stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

  NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
                NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
   if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
   {
           [[UIApplication sharedApplication] openURL: whatsappURL];
    }
    else
    {
          UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp" message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
    }

Refer this link for sharing image WhatsApp image sharing iOS

Community
  • 1
  • 1
Aarti Oza
  • 1,134
  • 2
  • 14
  • 31
  • I use this code but always give "Your device has no WhatsApp installed." Message... – LC Nagar Jan 07 '16 at 09:06
  • Then you are running it in simulator, you must have to run it in device in which whats app installed. – Aarti Oza Jan 07 '16 at 09:09
  • Be sure to include WhatsApp URL scheme in your application's Info.plist under LSApplicationQueriesSchemes key if you want to query presence of WhatsApp on user's iPhone using -[UIApplication canOpenURL:] – IPv6 Jul 10 '18 at 07:44
0

Check this link and follow the steps mentioned in it: https://ioscoderhub.wordpress.com/2014/05/01/how-can-i-share-image-from-iphone-application-to-whatsapp-line-wechat-programmatically/

Below is the brief code from the link above...

in ViewController.h file:

@interface ViewController : UIViewController
{
}

@property(nonatomic,retain) UIDocumentInteractionController *documentationInteractionController;

in ViewController.m file:

- (IBAction)bocClick:(UIButton *)sender {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; //here i am fetched image path from document directory and convert it in to URL and use bellow

    NSURL *imageFileURL =[NSURL fileURLWithPath:getImagePath];
    NSLog(@"imag %@",imageFileURL);

    self.documentationInteractionController.delegate = self;
    self.documentationInteractionController.UTI = @"net.whatsapp.image";
    self.documentationInteractionController = [self setupControllerWithURL:imageFileURL usingDelegate:self];
    [self.documentationInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL
                                               usingDelegate: (id) interactionDelegate {

    self.documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];

    self.documentationInteractionController.delegate = interactionDelegate;

    return self.documentationInteractionController;
}
Brian
  • 14,610
  • 7
  • 35
  • 43
Avaan
  • 4,709
  • 1
  • 10
  • 13
0

You can share text with WhatsApp as given below:

NSString *msgStr = [NSString stringWithFormat:@"whatsapp://send?text=%@ ,msgString];
        NSURL * whatsappURL = [NSURL URLWithString:[msgStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
            [[UIApplication sharedApplication] openURL: whatsappURL];
        } else {
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
technerd
  • 14,144
  • 10
  • 61
  • 92
  • Using this code i find output is "WhatsApp not installed" please tell me what is doing wrong – LC Nagar Jan 07 '16 at 09:28
  • WhatsApp should be installed in the iPHONE. This will not work in simulator – Milap Kundalia Jan 07 '16 at 10:14
  • Yes Sir , I tested this code in ios device.and this device install whatsApp. – LC Nagar Jan 07 '16 at 10:32
  • Be sure to include WhatsApp URL scheme in your application's Info.plist under LSApplicationQueriesSchemes key if you want to query presence of WhatsApp on user's iPhone using -[UIApplication canOpenURL:]. – IPv6 Jul 10 '18 at 07:45
0
   NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",@"YOUR STRING"];
    NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
    {
        [[UIApplication sharedApplication] openURL: whatsappURL];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

For Details knowledge plz refer below link

https://www.whatsapp.com/faq/en/iphone/23559013

Milap Kundalia
  • 1,566
  • 1
  • 16
  • 24
  • contoller always go in else part – LC Nagar Jan 07 '16 at 09:29
  • WhatsApp should be installed in the iPHONE. This will not work in simulator – Milap Kundalia Jan 07 '16 at 10:15
  • Be sure to include WhatsApp URL scheme in your application's Info.plist under LSApplicationQueriesSchemes key if you want to query presence of WhatsApp on user's iPhone using -[UIApplication canOpenURL:]. – IPv6 Jul 10 '18 at 07:45