NSString* urlText = @"http://www2.htk.dk/Socialpsykiatrien/menuplan%20værestedet.pdf";
NSString* urlTextEscaped = [urlText stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
这是我的代码。并且它每次都停在NSURLRequest ...看起来它正确地加载了url 但我似乎无法找到错误
所有控制台都说:(lldb)
答案 0 :(得分:2)
如果您避免百分比转义已经转义百分比的内容,那么它可以正常工作:
NSString* urlText = @"http://www2.htk.dk/Socialpsykiatrien/menuplan værestedet.pdf";
NSString* urlTextEscaped = [urlText stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
或者,如果此字符串的来源已经隐藏了%20
,您可以将其取消并重新转义:
NSString* urlText = @"http://www2.htk.dk/Socialpsykiatrien/menuplan%20værestedet.pdf";
NSString* urlTextUnescaped = [urlText stringByReplacingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSString* urlTextEscaped = [urlTextUnescaped stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSURLRequest *request = [NSURLRequest requestWithURL:url];