【问题标题】:How to send an image or text to whatsapp from my ios app?如何从我的 ios 应用程序向 whatsapp 发送图像或文本?
【发布时间】:2016-04-11 13:14:08
【问题描述】:

我使用此代码从我的 iOS 应用程序在 Whatsapp 上共享图像,但控制器始终处于 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];
}

【问题讨论】:

    标签: ios objective-c iphone ios7


    【解决方案1】:

    使用此代码在 whats 应用上分享仅文本

        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

    【讨论】:

    • 我使用此代码,但总是给出“您的设备没有安装 WhatsApp”。留言...
    • 那你是在模拟器上运行的,你必须在安装了whats app的设备上运行。
    • 如果您想使用 -[UIApplication canOpenURL:] 查询用户 iPhone 上 WhatsApp 的存在,请务必在 LSApplicationQueriesSchemes 键下的应用程序 Info.plist 中包含 WhatsApp URL 方案
    【解决方案2】:

    检查此链接并按照其中提到的步骤操作: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;
    }
    

    【讨论】:

      【解决方案3】:

      您可以与 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];
              }
      

      【讨论】:

      • 使用此代码我发现输出是“WhatsApp 未安装”请告诉我做错了什么
      • WhatsApp 应该安装在 iPHONE 中。这在模拟器中不起作用
      • 是的,先生,我在ios设备中测试了这段代码。这个设备安装了whatsApp。
      • 如果您想使用 -[UIApplication canOpenURL:] 查询用户 iPhone 上是否存在 WhatsApp,请务必在应用程序的 Info.plist 中的 LSApplicationQueriesSchemes 键下包含 WhatsApp URL 方案。
      【解决方案4】:
         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

      【讨论】:

      • 控制器总是进入其他部分
      • WhatsApp 应该安装在 iPHONE 中。这在模拟器中不起作用
      • 如果您想使用 -[UIApplication canOpenURL:] 查询用户 iPhone 上是否存在 WhatsApp,请务必在应用程序的 Info.plist 中的 LSApplicationQueriesSchemes 键下包含 WhatsApp URL 方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 2017-04-08
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 2011-10-18
      • 2020-06-25
      相关资源
      最近更新 更多