如何从我的ios应用程序发送图像或文本到whatsapp?

时间:2016-01-07 08:15:05

标签: ios objective-c iphone ios7

我使用此代码在我的iOS应用程序上分享Whatsapp上的图像,但Controller始终处于else状态。请给我一个解决方案。

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

4 个答案:

答案 0 :(得分:4)

  

使用此代码在最新应用上分享仅文字

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

请参阅此链接以共享图像   WhatsApp image sharing iOS

答案 1 :(得分:0)

点击此链接并按照其中提及的步骤进行操作:https://ioscoderhub.wordpress.com/2014/05/01/how-can-i-share-image-from-iphone-application-to-whatsapp-line-wechat-programmatically/

以下是上面链接的简要代码......

ViewController.h文件中的

@interface ViewController : UIViewController
{
}

@property(nonatomic,retain) UIDocumentInteractionController *documentationInteractionController;
ViewController.m文件中的

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

答案 2 :(得分:0)

您可以与WhatsApp共享文字,如下所示:

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

答案 3 :(得分: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];
    }

有关详细信息,请参阅以下链接

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