简体   繁体   中英

How to send an image or text to whatsapp from my ios app?

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];
}

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

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;
}

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];
        }
   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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM