- 加载本地网页附加参数时不能直接使用[NSURL fileURLWithPath:@""]
- 因为直接使用的话转换出来的url会乱码,有需要可以自己打印看看。比如/会转化为%,?也会转换成%
- 需要使用stringByAddingPercentEncodingWithAllowedCharacters转换,如下:
//获取本地网页路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
//传参数:
NSString *urlStr = filePath;
urlStr = [urlStr stringByAppendingString:[NSString stringWithFormat:@"?id=%@", idNum]];
urlStr = [urlStr stringByAppendingString:[NSString stringWithFormat:@"&name=%@", name]];
//转换url
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];
NSURLRequest *request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15];
//加载本地网页
[self.webView loadRequest:request];
网友评论