HTML处理oc代码

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;

@end

@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

测试结果如下:

转载于:https://my.oschina.net/luhoney/blog/702591

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值