在控制器viewDidLoad中,建立webview控制器 加载h5页面
-(void)loadView
{
UIWebView *webView = [[UIWebView alloc]init];
self.view = webView;
webView.delegate = self;
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
[super viewDidLoad];
//字符串里有空格 转换成url为空 需要把字符串中的空格去掉 才能转换成正经的url 神坑 神坑 神坑 orzzzzzzz。。。。。
self.nvgUrl = [self.nvgUrl stringByReplacingOccurrencesOfString:@" " withString:@""];
NSURL *url = [NSURL URLWithString:self.nvgUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIWebView *webView = (UIWebView *)self.view;
[webView loadRequest:request];
NSLog(@"%@",url);
}//实现webview的代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSURL * url = [request URL];
NSString * string = url.absoluteString;
//如果字符串中包含这个文字 就会走这个方法
if ([string hasPrefix:@"lalahei"]){
string = string.stringByRemovingPercentEncoding;
NSRange range = [string rangeOfString:@"lalahei:"];
//假设传来的字符串是这串“ lalahei:{"data":"http://192.168.1.000:2222/webapp/lalaDetails/107/0","title":"极速借"}”
string = [string substringFromIndex:range.location + range.length];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
//转换成人类能看懂的格式 不需要再截取字符串了
NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//这时输出结果是这样婶er的 !!!!!@@{
data = "http://192.168.1.000:2222/webapp/lalaDetails/107/0";
title = "\U6781\U901f\U501f"; }
NSLog(@"!!!!!@@%@",object);
CommonWebViewController *commonWebVc = [[CommonWebViewController alloc]init];
//就可以直接从字典里取值了
commonWebVc.nvgTitle = object[@"title"];
commonWebVc.nvgUrl = object[@"data"];
[self.navigationController pushViewController:commonWebVc animated:YES];
return NO;
}
}