URL encoding iOS NSURL error

前端 未结 2 1356
暗喜
暗喜 2021-01-29 06:42

URL which opens in Firefox,Chrome browsers on desktop, doesn\'t open in WebView on iPhone. This URL is supposedly accessing a GET request. When creating the NSURL without perce

相关标签:
2条回答
  • 2021-01-29 07:07

    You should edit your question showing us an example of your URL and your GET parameters. If you're percent escaping, for example, some reserved character in the domain name or the URL path, that suggests one solution (e.g. stringByAddingPercentEscapesUsingEncoding is fine). If you're percent escaping the broader array of reserved characters in the parameters of a GET request (notably = or +), then stringByAddingPercentEscapesUsingEncoding is simply not up to the job and you'd have to use CFURLCreateStringByAddingPercentEscapes (but only on the parameter keys and their values, not on the full URL string). I use a method like the following on the parameters as I append them to the URL:

    - (NSString *)percentEscapeURLParameter:(NSString *)string
    { 
        return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                         (CFStringRef)string,
                                                                         NULL,
                                                                         (CFStringRef)@":/?@!$&'()*+,;=",
                                                                         kCFStringEncodingUTF8));
    }
    

    If you're saying that CFURLCreateStringByAddingPercentEscapes is not working for you, you'd have to show us how you're using it. Make sure you are doing it just on the GET parameter values, that you're supplying the necessary "legal characters to escape" parameter, that you're not escaping something that shouldn't be, etc.

    0 讨论(0)
  • 2021-01-29 07:21

    Sometimes URL encoded format already except for the é-character which should probably be encoded as %c3%a9. Desktop browser is quite liberal with invalid URLs, thats why it works in Safari etc. So if you have a NSString and you want to convert it into a proper URL encoding then use the below method of NSString class.

    NSURL* url = [NSURL URLWithString:[strURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    0 讨论(0)
提交回复
热议问题