【问题标题】:How to share link and text message on whats app from iOS app? [duplicate]如何从 iOS 应用程序在 whatsapp 上共享链接和短信? [复制]
【发布时间】:2015-12-14 15:00:32
【问题描述】:

我正在尝试从我的 iOS 应用程序发送关于 whats 应用程序的短信,但没有成功。我在下面添加了我的代码,请建议我做错了什么。我的设备安装了whats app,但我仍然无法发送任何短信。

- (IBAction)whatAppInvite:(id)sender
    {
        NSString * strTextPost = [@"" stringByAppendingFormat:@"Hey try this app. Its amazing. \n\n https://itunes.apple.com/us/app/google-search/*********2?mt=8"];


        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"." withString:@"%2E"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"'" withString:@"%27"];
        strTextPost = [strTextPost stringByReplacingOccurrencesOfString:@"-" withString:@"%2D"];

        NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",strTextPost];

        NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
        if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]])
        {
            if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
            {
            [[UIApplication sharedApplication] openURL: whatsappURL];
            }
            else
            {
                // don't know what happens
                // calls this
            }
        }
        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 whatsapp


【解决方案1】:

试试这个代码:

NSString * msg = @"Application%20Name%20https://itunes.apple.com/YOUR-URL";

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 not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

【讨论】:

  • 换行怎么办?
  • 如何回到我的应用程序
  • @Chandni 回到上一个应用程序的唯一方法是状态栏左侧的默认方式。
【解决方案2】:

使用 UIActivityViewController 发送消息

【讨论】:

    【解决方案3】:

    看看这个“Share link using whatsapp

    当您尝试通过 whatsapp 发送消息时不要进行字符串替换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 2014-04-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多