2015-10-02 34 views
0

我試圖通過WhatsApp共享文本+網址,但沒有在WhatsApp文本框中填充任何內容。無法與whatsapp中的字符串共享網址

我曾嘗試下面的代碼:

NSString *urlString = [NSString stringWithFormat:@"http://www.mouthshut.com/contest?ref=%@&inv_via=whatsapp&act=reg", UserName]; 

//NSURL *url = [NSURL URLWithString:urlString]; 
//urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=Writing a review is better than whatsapp. Win cash prizes daily on MouthShut.com \n Join Now %@", urlString]; 

NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) { 
    [[UIApplication sharedApplication] openURL: whatsappURL]; 
} else { 
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 
+0

我想知道爲什麼這個問題一直在下降投票? – Apple

回答

0
NSString *urlString = [NSString stringWithFormat:@"http://www.mouthshut.com/contest?ref=%@&inv_via=whatsapp&act=reg", UserName];  
     urlString = [urlString stringByReplacingOccurrencesOfString:@":" withString:@"%3A"]; 
     urlString = [urlString stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"]; 
     urlString = [urlString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"]; 
     urlString = [urlString stringByReplacingOccurrencesOfString:@"," withString:@"%2C"]; 
     urlString = [urlString stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"]; 
     urlString = [urlString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"]; 

    //NSURL *url = [NSURL URLWithString:urlString]; 
    //urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=Writing a review is better than whatsapp. Win cash prizes daily on MouthShut.com \n Join Now %@", urlString]; 

    NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) { 
     [[UIApplication sharedApplication] openURL: whatsappURL]; 
    } else { 
     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } 
相關問題