简体   繁体   English

如何使用特殊字符制作NSURL

[英]How to make NSURL with special characters

My application is based in Portuguese - Brazil. 我的应用程序基于葡萄牙语-巴西。 We have some special characters that we use in words like "Açaí". 我们在“Açaí”等词中使用了一些特殊字符。

I have a search method that sends a request to a URL like this: 我有一个搜索方法,可以将请求发送到这样的URL:

 NSURLSession *session = [NSURLSession sharedSession];
    NSString *urlString = [NSString stringWithFormat:@"http://www.mydomain.com.br/webservice/search/%@",_query];
    NSURL *URL = [NSURL URLWithString:urlString];
    [[session dataTaskWithURL:URL
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {
   //ALL THE LOGIC HERE
                });

            }] resume];

Now, if the _query is set "Açaí" (which has ç and í as special characters) when I try NSURL *URL = [NSURL URLWithString:urlString]; 现在,如果在尝试NSURL *URL = [NSURL URLWithString:urlString];时将_query设置为“Açaí”(带有ç和í作为特殊字符) NSURL *URL = [NSURL URLWithString:urlString]; the URL variable is not initialized and breaks all the rest. URL变量未初始化,其余所有中断。

How can I send a request to my web service using special characters? 如何使用特殊字符向我的Web服务发送请求?

I think you need to escape the url string before creating the NSURL . 我认为您需要在创建NSURL之前转义url字符串。

NSString *urlString = [NSString stringWithFormat:@"http://www.mydomain.com.br/webservice/search/%@",_query];
NSURL *URL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM