2014-01-30 56 views
3

也許我在這裏錯過了一些簡單的事情,但我不能讓我的URL使用URL方案出現在WhatsApp應用程序的生活。我有:發送我的應用程序的URL計劃使用WhatsApp的URL計劃

 NSString *stringToSend = [[NSString stringWithFormat:@"whatsapp://send?text=myAppDomain://%@moreChars",specialString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

     NSURL *whatsappURL = [NSURL URLWithString:stringToSend]; 

     [[UIApplication sharedApplication] openURL: whatsappURL]; 

,成功啓動了WhatsApp應用程序,我選了一個接觸後然而,消息框未預先填充,但它仍然是空的。我究竟做錯了什麼?

+0

如果您發送其他文本,它工作嗎? (即不是你的URL) –

+0

是的。它適用於當我硬編碼單詞或當我使用stringWithFormat – JustAnotherCoder

+0

我面臨同樣的問題。你找到解決方案嗎? – Michel

回答

0

你附加字符串UTFEncoded?我的意思是這是你的解決方案。

0

這是不足以stringByAddingPercentEscapesUsingEncoding與WhatsApp。試試下面的代碼對我有用:

NSString *theTempMessage,*theFinalMessage; 
....... 
    /// theTempMessage should contain your stringToSend (in its current state); 
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@":" withString:@"%3A"]; 
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"]; 
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"]; 
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"," withString:@"%2C"]; 
    theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"]; 
    theFinalMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"&" withString:@"%26"]; 
    stringToSend=theFinalMessage; 

在這一點上你的stringToSend應該包含一些工作。 請嘗試一下,讓我知道。

我希望這會有所幫助。

2

這是完整的代碼都在WhatsApp的

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

發送文字和網址最後我得到了解決辦法,分享自己的應用程序iTunes網址到WhatsApp的。

NSString *string = [@"" stringByAppendingFormat:@"Share this on whatsapp \n\n https://itunes.apple.com/us/app/google-search/id284815942?mt=8"]; 

string = [string stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"]; 
    string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"%26"]; 

    NSLog(@">>>Just pass this to Whatspp using their URL scheme %@",string); 

我測試了一下。 :) 祝你好運。