How to prepare an NSURL from an NSString continaing international characters?

前端 未结 4 840
感情败类
感情败类 2020-12-11 07:59

I have to access a web server using a GET with international characters (Hebrew in my case but could be anything). So I make an NSString just fine

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 08:42

    + (NSURL*) URLWithStringWithSpecialChars:(NSString *)sURL
    {
        NSURL *url = [NSURL URLWithString:sURL];
        if(url == nil)
            return [NSURL URLWithString:[sURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        else
            return url;
    }
    

    calling this function instead of URLWithString: should ensure that a valid NSURL is always returned (instead of nil when needed)

提交回复
热议问题