Unable to create NSUrl from NSString always getting nil

后端 未结 3 789
既然无缘
既然无缘 2021-01-15 19:24
NSMutableString *string = [[NSMutableString alloc]initWithString:
@\"http%3A%2F%2Fsupport24hour.com%2Fworkplace2%2Fbuttler%2Fimage.php%3Fwidth%3D534%26height%3D256%2         


        
相关标签:
3条回答
  • 2021-01-15 20:06

    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)

    0 讨论(0)
  • 2021-01-15 20:11

    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:@"+"];
    }
    
    0 讨论(0)
  • 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?

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