Unable to create NSUrl from NSString always gettin

2019-02-20 03:19发布

问题:

NSMutableString *string = [[NSMutableString alloc]initWithString:
@"http%3A%2F%2Fsupport24hour.com%2Fworkplace2%2Fbuttler%2Fimage.php%3Fwidth%3D534%26height%3D256%26image%3Duploads%2Fdeals%2FdealImage%2Fdeal_1383005121_IGA+Logo.JPG"];

[string replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [string length])];

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

回答1:

one of my colleague faced the same problem , try below line of code . Hope your problem will be resolved

NSMutableString *string = [[NSMutableString alloc]initWithString:
                           @"http%3A%2F%2Fsupport24hour.com%2Fworkplace2%2Fbuttler%2Fimage.php%3Fwidth%3D534%26height%3D256%26image%3Duploads%2Fdeals%2FdealImage%2Fdeal_1383005121_IGA+Logo.JPG"];

string = [[NSMutableString alloc] initWithString:[[string stringByURLDecode] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

NSURL * url =  [NSURL URLWithString:string];

NSLog(@"url = %@",url);

Where stringByURLDecode is a category method used to decode URL and it goes like this

- (NSString *)stringByURLDecode {

NSMutableString *tempStr = [NSMutableString stringWithString:self];
[tempStr replaceOccurrencesOfString:@"+" withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])];

return [[NSString stringWithFormat:@"%@",tempStr] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

}

Best of Luck (y)



回答2:

You must not percent encode the whole string. Instead, only the data octet in a component must be possibly encoded.

In other words, a URI consists of components and subcomponents which are delimited by certain characters. These characters are called "reserved characters". The separators for the different components must certainly not be included in the percent encoding.

Note that each component of a URL may require a slightly different percent encoding.

See also RFC 3986.


Edit:

The general structure of a URL is given below:

  URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

The following two examples are straight copied from RFC which show their component parts:

     foo://example.com:8042/over/there?name=ferret#nose
     \_/   \______________/\_________/ \_________/ \__/
      |           |            |            |        |
   scheme     authority       path        query   fragment
      |   _____________________|__
     / \ /                        \
     urn:example:animal:ferret:nose

The "query component" consists of a list of "key/value" parameters whose key and value is separated by "=", and whose parameters are separated by "&".

The query component is everything after the question mark '?' up until a hash character '#'.

The following code encodes the name or the value of a parameter. Note that parameters must be separated by a '&', and the name and value must be separated by a '='.

static NSString* form_urlencode_rfc3986(NSString* s) {
    CFStringRef charactersToLeaveUnescaped = CFSTR(" ");
    //CFStringRef legalURLCharactersToBeEscaped = CFSTR("!$&'()+,/:;=?@~");
    // Modified for urls (excluding '~'):
    CFStringRef legalURLCharactersToBeEscaped = CFSTR("!$&'()+,/:;=?@");

    NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                             kCFAllocatorDefault,
                             (__bridge CFStringRef)s,
                             charactersToLeaveUnescaped,
                             legalURLCharactersToBeEscaped,
                             kCFStringEncodingUTF8));
    return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}


回答3:

Hmmm... how about printing out string before you turn it into an NSURL? Does it look as you'd expect it?

Also: replacing "+" with a Space OR do you actually want to remove it?