How to share content on WhatsApp from iOS

前端 未结 3 1519
闹比i
闹比i 2021-01-20 02:12

I would like to share one Url link and some text message into WhatsApp from my application. How can i share content?

I got this code for only text

NS         


        
3条回答
  •  离开以前
    2021-01-20 03:02

    I had a problem with this whatsapp api with url strings, especially when they contained a query string with several fields, e.g. http://example.com/foo?bar=foo&foo=bar. When opening the app I found the message text would be empty.

    The solution was to properly percent escape the string using the CFString functions. See the apple documentation here: https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/index.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes

    But for anyone else with this issue here is my solution in full:

    CFStringRef originalURLString = (__bridge CFStringRef)[NSString stringWithFormat:@"%@", @"http://example.com/foo?bar=foo&foo=bar"];
    CFStringRef preprocessedURLString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, originalURLString, CFSTR(""), kCFStringEncodingUTF8);
    NSString *urlString = (__bridge NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, preprocessedURLString, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8);
    NSString *whatsAppURLString = [NSString stringWithFormat:@"whatsapp://send?text=%@", urlString];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:whatsAppURLString]];
    
    • Note the use of the characters to be escaped in the CFURLCreateStringByAddingPercentEscapes function.

提交回复
热议问题