iOS使用UIWebView遇到Error Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKi

如今在接触iOS开发,今天在调试一个界面加载web页面的问题,发现死活没法加载,浏览器里能正常打开,加上相应代码以后获得了错误信息为:web

2013-04-18 15:05:06.446 Client_DEMO[22889:1a303] webview didFailLoadWithError <UIWebView: 0x18ff2160; frame = (18 70; 315 408); autoresize = W+H; layer = <CALayer: 0xc1140a0>> , and err is Error Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKitErrorDomain error 101.)”浏览器

加日志的办法为:ui

WebviewDelegat.h 代码以下:url

#import <UIKit/UIKit.h>debug

@interface WebviewDelegate : NSObject <UIWebViewDelegate>调试

@end日志

WebviewDelegat.mm 代码以下:code

#import “WebviewDelegate.h”component

@implementation WebviewDelegate
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString * str = [[request URL] absoluteString];
NSString *urlStr = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];ip

NSArray * strArray = [urlStr componentsSeparatedByString:@":"];
if([strArray count]>2)
{
NSLog(@”webview for %@”,urlStr);
if ([[strArray objectAtIndex:0] isEqualToString:@”jscall”] ) {
//这里拦截作些本地的事情
}
return NO;
}else{
return YES;
}

}
- (void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@”webview webViewDidStartLoad %@”,webView.debugDescription);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@”webview webViewDidFinishLoad %@”,webView.debugDescription);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

//把出错信息打出来
NSLog(@”webview didFailLoadWithError %@ , and err is %@”,webView.debugDescription, error.debugDescription);
}

@end

经过UIWebView的setDelegate方法将其绑定。

 

网上搜索以后得知是urlencode有问题,仔细核实了一下url,发现url里有个参数值有竖线,因而知道了是竖线未转义致使。

好比: http://lizongbo.com/?chid=0|0|618119, 应该为:http://lizongbo.com/?chid=0%7C0%7C618119

解决办法很简单,对url的字符串作了个替换:

sUrl=replaceStr(sUrl,”|”,”%7C”);//不转义的话会报错,nd err is Error Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKitErrorDomain error 101.)”

参考:http://stackoverflow.com/questions/2028379/uiwebkit-error-101

相关文章
相关标签/搜索