web-dev-qa-db-fra.com

Partager le lien en utilisant WhatsApp

J'ai utilisé ce code pour le lien de l'application dans ce qui est app mais rien n'est venu dans le champ de texte de WhatsApp. Si vous utilisez un texte simple, son travail Quelqu'un peut-il suggérer le résultat final?.

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];
}
17
CJ IOS Developer

L’erreur suivante s’affiche lors de la vérification de canOpenURL

a échoué pour l'URL: "whatsapp: //" - erreur: cette application n'est pas autorisée à interroger le système whatsapp

Dans iOS 9, vous devez inclure dans la liste blanche les modèles d'URL que votre application souhaite interroger dans Info.plist sous la clé LSApplicationQueriesSchemes (un tableau de chaînes):

 enter image description here

Avec les schémas inclus dans Info.plist, tout fonctionne comme avant. Lorsque vous établissez une liaison avec iOS 9, vous n'êtes pas limité à 50 régimes distincts. Vous devez simplement déclarer ce dont vous avez besoin dans Info.plist. Il semble n'y avoir aucune limite quant au nombre de régimes que vous pouvez inclure, mais je m'attendrais à des questions de la part de l'équipe de révision de l'App Store si elle pense que vous abusez du mécanisme.

Notez que ce mécanisme s'applique uniquement à canOpenURL et non à openURL . Vous n'avez pas besoin d'avoir un schéma répertorié dans Info.plist pour pouvoir ouvrez-le avec 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];
}

Ceci est une vidéo officielle de WWDC 2015 pour la sécurité des applications.

42
Nimit Parekh

Ajoutez ceci à votre Info.plist

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>whatsapp</string>
    </array>

Implémentez ce code dans ViewController où vous devez ouvrir WhatsApp pour le partage. (comme par exemple dire une action sur un bouton) Mise à jour pour la version Swift 3 (Xcode 8.x): Mise à jour pour les obsolètes:

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.")
}

ShowAlert () est une fonction personnalisée permettant d'afficher une alerte.

7
Ankit Kumar Gupta

Si vous utilisez " [[UIApplication sharedApplication] openURL: whatsappURL]; " après le remplacement de la chaîne, le navigateur Safari n'ouvrira pas le navigateur WhatsApp.

Si vous voulez ouvrir WhatsApp, ne remplacez pas la chaîne

1
satheesh