Utilisation de stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding pour encoder une URL NSString n'encode pas le symbole @. Quelle est la bonne façon de résoudre cela? Merci.Comment encoder l'URL du symbole @ dans le SDK iPhone?
3
A
Répondre
10
Ajouter ce après la ligne de code que vous avez déjà (et changer le récepteur, etc. à vos propres variables):
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
Si vous voulez vous assurer que tout est codé, voici le meilleur chemin:
NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];
(de Roger @Iphone SDk : Issue with ampersand in the URL string)
7
La meilleure façon de coder l'URL est avec cela, de mon blog http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/:
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
+1
Le code ci-dessus fonctionne très bien mais il manque quelques caractères qui doivent être encodés. Ajoutez ceci au code .. (CFStringRef) @ "! * '\"();: @ & = + $, /?% # []% " – AlBeebe
Questions connexes
- 1. Aide avec Expression Encoder SDK
- 2. Symbole en double bizarre dans le projet sdk iPhone lors de la compilation?
- 3. Pourquoi NSCalendarDate est-il dans le SDK du simulateur mais pas dans le SDK iPhone?
- 4. CIFilter disponible dans le SDK iPhone?
- 5. iPhone-SDK: Comment rendre le site web au format iPhone?
- 6. Comment faire une animation radar dans le SDK iPhone?
- 7. iPhone SDK Code du signe Erreur
- 8. Comment contrôler le placement du menu coller dans un UITextView dans iPhone SDK
- 9. Coloriage Image dans iPhone SDK
- 10. contournant UIImagePicker dans iphone sdk
- 11. Comment extraire les icônes UIBarButtonItem du SDK iPhone?
- 12. iPhone SDK - liste wifi
- 13. iPhone SDK 3.0 Targetting 2.2.1 SDK
- 14. iPhone SDK: Bienvenue vidéo
- 15. Sauvegarder État iphone sdk
- 16. comment obtenir le chemin du fichier vidéo enregistré de temp dans iphone sdk et jouer?
- 17. iPhone SDK: Optimiser UIModalTransitionStyleFlipHorizontal
- 18. coincé sur l'emplacement actuel - iphone sdk
- 19. ConnectionKit & iPhone SDK
- 20. iPhone sdk 3.0 issue
- 21. personnalisée autorotation iPhone SDK
- 22. SDK iPhone touche événement
- 23. iPhone SDK: IBAction
- 24. iPhone SDK: UIWebView
- 25. §non_lazy_ptr iphone sdk 3.0
- 26. Exportation du symbole CLSQL
- 27. iPhone sdk - nom des propriétaires?
- 28. Iphone SDK comment enregistrer mon score?
- 29. iphone sdk: Création d'une image du contenu de l'écran?
- 30. iPhone SDK: Comment charger une vue différente?
me battre à elle. Maintenant, allez tous voter ma réponse originale :-) –