HTML->OC
通过url方式:自定义url协议,native原生处理协议,比如:shoppingmall://fruit/hot?id=123456;
首先将html代码编写如下:
<html>
<head>
<meta charset="UTF-8">
<title>oc和html的互调</title>
</head>
<body>
<h1>OC</h1>
<ul><li><a href="http://my.oschina.net/luhoney">我的开源博客</a></li></ul>
<h1>html</h1>
<ul><li><a href="www.baidu.com">百度链接</a></li></ul>
<h1>ios测试代码</h1>
<ul>
<li> <a href = "HTMLDemo://teacher/luohao">罗浩:成都主管</a></li>
</ul>
</body>
</html>
在indexViewController.m中编写代码如下:
//
// IndexViewController.m
// HtmlDemo02
// Created by fandong on 16/6/29.
// Copyright © 2016年 fanjuan. All rights reserved.
#import "IndexViewController.h"
@interface IndexViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@implementation IndexViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *indexUrl = [[NSBundle mainBundle]URLForResource:@"File" withExtension:@"html"];
[self.webView loadRequest:[NSURLRequest requestWithURL :indexUrl]];
self.title = @"我的网站";
self.webView.delegate =self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *url = request.URL.absoluteString;
//判断是否是自定义的协议
if ([url containsString:@"htmldemo"]) {
if ([url containsString:@"teacher"]) {
//处理老师
NSArray *splistArray = [url componentsSeparatedByString:@"/"];
NSString *name = splistArray.lastObject;
NSLog(@"%@",name);
//执行指定id的跳转
[self performSegueWithIdentifier:@"showDetail" sender:name];
}
}
return YES;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//跳转页面
UIViewController *destVC=segue.destinationViewController;
destVC.title = sender;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
测试结果如下: