感谢http://www.cocoachina.com/bbs/read.php?tid-93877.html此文作者
这两天做推送跳转到指定的本地html页面,发现用UIWebView加载本地html文件传参数页面加载出错。为了以后查询方便,现记录下来
UIWebView加载本地文件无参数用下面这种方法不会报错:
NSURL *url = [NSURL fileURLWithPath:[self.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
带参数时(如.../index.html?FID=111)加载出错,提示在此服务器上找不到所请求的URL。
调试发现此时用fileURLWithPath的方式会把?转义成%3F导致请求失败。
解决办法参考stackoverflow:http://stackoverflow.com/questions/8286099/how-to-pass-arguments-to-fileurl
NSString *encodedPath = [self.url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSMutableString*urlStr =nil;
if([self.urlParamArrcount] ==0||self.urlParamArr==nil) {
urlStr = [NSMutableStringstringWithFormat:@"file://%@", encodedPath];
}else{
urlStr = [NSMutableStringstringWithFormat:@"file://%@", encodedPath];
for(inti=0; i<[self.urlParamArrcount]; i++) {
NSDictionary* tempDic =self.urlParamArr[i];
if(i==0) {
[urlStrappendFormat:@"?%@=%@",[tempDicobjectForKey:@"key"],[tempDicobjectForKey:@"value"]];
}else{
[urlStrappendFormat:@"&%@=%@",[tempDicobjectForKey:@"key"],[tempDicobjectForKey:@"value"]];
}
}
}
NSURL*url = [NSURLURLWithString:urlStr];
NSURLRequest *request= [NSURLRequest requestWithURL:url];
[_webloadRequest:request];
使用上面方法就可以正常加载了,再次感谢http://www.cocoachina.com/bbs/read.php?tid-93877.html此文作者的分享
网友评论