UIWebView: Error while loading URL

梦想的初衷 提交于 2019-12-19 19:52:11

问题


I am trying to build an RSS reader for the Tom's Hardware website.

I have an error when I try to load an URL into an UIWebview from an RSS link.

Here'is my code:

- (void)viewDidLoad {
 [super viewDidLoad];

 if (self.url != nil) {
  NSURL *requestUrl = [NSURL URLWithString:self.url];
  NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];
  [webView loadRequest:requestObj];
 }
}

The url is setted by the parent controller.

When the URL comes directly from the RSS, I have an error in log:

[2234:207] Error Domain=WebKitErrorDomain Code=101 UserInfo=0x3a6a240 "Operation could not be completed. (WebKitErrorDomain error 101.)"

When I set manually the URL with the same URL like below, it work !

self.url = @"http://www.presence-pc.com/tests/fraude-financiere-paget-macafee-23198/#xtor=RSS-12";

Here is an URL exemple: http://www.presence-pc.com/tests/fraude-financiere-paget-macafee-23198/#xtor=RSS-12. I have no idea about that problem.

I hope you can help me.

Best regards.


回答1:


I think the problem can be the anchor in your URL, a friend got the same problem




回答2:


Thanks guy, that'it, I have split the URL like this:

NSArray *split = [url componentsSeparatedByString:@"#"];

NSURL *requestUrl = [NSURL URLWithString:[[split objectAtIndex:0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

and now it work , thanks for your help !




回答3:


I have this code which also triggers error code 101

 _request = [NSURLRequest requestWithURL:[NSURL URLWithString:redirectUrl]];
            [editorWebView loadRequest:_request];

RedirectURL is an NSString which has this format http%3A%2F%2Fwww.linkedin.com%2Fnhome%2F

I tried removing percent escapes using the method of NSString and it now works.

[redirectUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


来源:https://stackoverflow.com/questions/1528060/uiwebview-error-while-loading-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!