NSURL returning nil for certain cases
我正在创建一个
1 2 3 4 5 6 7 8 9 | NSMutableString *url = [NSMutableString stringWithFormat:@"http://www.private.com/recievedata.php?item=%@&contact=%@&discovery=%@&summary=%@",__item,__contactDetails,__lostFound,__explain]; //The" '" in PHP is a special character, so we have to escape it in the URL //The two slashes are because the"" itself is a special character in Objective C NSString *formattedURL = [url stringByReplacingOccurrencesOfString:@"'" withString:@"\\\'"]; NSURLSession *session = [NSURLSession sharedSession]; NSURL *address = [NSURL URLWithString:formattedURL]; |
但由于某种原因,每当我尝试使以下 url 发生时,地址变量都会保存
1 | http://www.private.com/recievedata.php?item=hell\'&contact=hell&discovery=lost&summary=hell |
从我的代码中可以看出,我必须在撇号前面添加 "\\\\",因为 PHP 需要在其 URL 中对 " \\' " 进行转义。但是这样做,似乎我违反了为
你说:
I had to add"" in front of the apostrophes as is evident from my code because PHP needs to have the" '" escaped in its URLs. But by doing so, it seems like I've violated some requirement set out for NSURL. What do you guys think?
是的,在您的 PHP 代码中,如果您在字符串值周围使用单引号,那么您必须转义出现在字符串文字中的任何
但您不应该使用
如果您因为将这些值插入 SQL 语句而想要转义这些
回到 URL 中保留字符的编码,这由 RFC 3986 管理。谨慎的策略是百分比转义除未保留字符集中的字符之外的任何字符。值得注意的是,如果您的值可能包含
因此,URL查询值中
过去,我们使用