nsurlrequest不会加载pdf

时间:2013-11-04 20:49:00

标签: iphone objective-c nsurlrequest

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)

1 个答案:

答案 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];