URLWithString: returns nil

后端 未结 8 1914
遥遥无期
遥遥无期 2020-12-02 13:12

it may be very easy, but I don\'t seems to find out why is URLWithString: returning nil here.

//localisationName is a arbitrary string here
NSSt         


        
8条回答
  •  萌比男神i
    2020-12-02 13:24

    You need to escape the non-ASCII characters in your hardcoded URL as well:

    //localisationName is a arbitrary string here
    NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
    NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL* url = [NSURL URLWithString:webStringURL];
    

    You can probably remove the escaping of the localisationName since it will be handled by the escaping of the whole string.

提交回复
热议问题