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

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

最佳答案

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

关于ios - 如何从我的 ios 应用程序向 whatsapp 发送图像或文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650204/

相关文章:

iOS 核心数据数据保护不起作用

ios - ScrollView 中的 SwiftUI 动画/过渡表现奇怪

ios - iOS8 上的新固定位置错误

iphone - @synthesize 干扰继承

iphone - iOS 7 中导航 Controller 及其 Root View Controller 的奇怪行为

iPhone 倾斜 CALayer

iphone - 如何使用 Core Animation 在 iPhone 上的 2 个图像之间交叉淡入淡出

objective-c - 将 int 与存储在字典中的数字进行比较

iphone - [NSDictionary valueForKey :] 内无法识别的选择器崩溃

objective-c - 使用蓝牙 4.0 在 iOS 设备之间交换数据