美文网首页
iOS html 本地加载附加传参数

iOS html 本地加载附加传参数

作者: Jesscia_Liu | 来源:发表于2022-04-06 12:15 被阅读0次
  • 加载本地网页附加参数时不能直接使用[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];

相关文章

网友评论

      本文标题:iOS html 本地加载附加传参数

      本文链接:https://www.haomeiwen.com/subject/ffursrtx.html