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];
}
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):
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];
} else {
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.
Add this to your Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
Implement this code to the ViewController where you need to open WhatsApp for sharing. (like for example say a button action) Update for swift 3 version (Xcode 8.x) : Updated for deprecations:
var str = "This is the string which you want to share to WhatsApp"
str=str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))!
let whatsappURL = URL(string: "whatsapp://send?text=\(str)")
if UIApplication.shared.canOpenURL(whatsappURL) {
UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil)
} else {
showAlert(message: "Whatsapp is not installed on this device. Please install Whatsapp and try again.")
}
Here showAlert() is a custom function for showing an alert.
If you use "[[UIApplication sharedApplication] openURL: whatsappURL];" after string replcement it will open the safari browser not the whatsapp,
If you want to open whatsapp don't replace the string
来源:https://stackoverflow.com/questions/34037066/share-link-using-whatsapp