CJ IOS Developer CJ IOS Developer - 1 year ago 68
iOS Question

Share link using whatsapp

I used these code for share app link in what's app but nothing is come in the textfield of whatsapp. If using simple text then its work. Can anyone suggest the final outcome.

NSString *theTempMessage = @"whatsapp://send?text=https://itunes.apple.com/in/app/myapp/id1054375332?ls=1&mt=8";
NSString *theFinalMessage;

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"];

NSString * stringToSend=theFinalMessage;
NSURL *whatsappURL = [NSURL URLWithString:stringToSend];

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])

[[UIApplication sharedApplication] openURL: whatsappURL];

Answer Source

Following error shows when checking canOpenURL

failed for URL: "whatsapp://" - error: This app is not allowed to query for scheme whatsapp

In iOS 9 you must whitelist any URL schemes your App wants to query in Info.plist under the LSApplicationQueriesSchemes key (an array of strings):

enter image description here

With the schemes included in Info.plist everything works as before. When you link against iOS 9 you are not limited to 50 distinct schemes you just need to declare what you need in Info.plist. There seems to be no limit for how many schemes you can include but I would expect questions from the App Store review team if they think you are abusing the mechanism.

Note that this mechanism only applies to canOpenURL and not openURL. You do not need to have a scheme listed in Info.plist to be able to open it with openURL.

 NSString * msg = @"Application%20Name%20https://itunes.apple.com/in/app/myapp/id1054375332?ls=1&mt=8";

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

This is official video of WWDC 2015 for app security.