UIWebView -- 未来HTML5 于iOS融合的桥梁

现在有一种新的技术,叫做HTML5。关于这门技术,在编者上大学那会儿,好像是大一那会儿就刚刚开始推出,这个技术为什么到今天还有很多人没有听说过,那是因为我们的浏览器跟不上步伐。。谷歌、opero、IE9等这些浏览器已经支持HTML5,但是中国现在还有大批的用户在使用XP,IE6,IE7等,这就导致了服务器端开发的人员不得不使用HTML4。不过这几年随着智能手机的普及,智能手机端的浏览器,比如UC、还有iOS自带的webView和(安卓的……)都能很好的支持HTML5.早就了很多的手机端的网游,还有比如现在一些3D的网页游戏,都是用到了HTML5这门技术。有服务器开发经验的人员,学习HTML5会很容易上手。。未来HTML5会是主流。。因为器只需要服务器端开发人员。即可实现安卓、iOS、阿里云等跨平台的使用。

     ( 在这里给大家提供一个自学HTML相关的知识的一个免费的网站。www.3cschool.com。)

      iOS的UIkit下有一个强大控件 - - UIWebView。

      它能像浏览器一样,加载网页信息到webView上,而且还能执行脚本语言(JavaScript)。同浏览器一样,也有前进、后退、刷新、停止、转到。还能加载本地的Html,调用js。

  

    //后退
    if (button.tag == 0) {
        [_webView goBack];
    }
    //前进
    if (button.tag == 1) {
        [_webView goForward];
    }
    //停止
    if (button.tag == 2) {
        [_webView stopLoading];
    }
    //刷新
    if (button.tag == 3) {
        [_webView reload];
    }
    //转到
    if (button.tag == 4) {
        //http://www.baidu.com www.baidu.com
        //判断前缀
        if (![_textField.text hasPrefix:@"http"]) {
            _textField.text = [[NSString stringWithFormat:@"http://%@", _textField.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
        //请求
        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:_textField.text]];
        //加载请求
        [_webView loadRequest:request];
        [_textField resignFirstResponder];
    }
    //加载本地html
    if (button.tag == 5) {
        NSString* path = [[NSBundle mainBundle] pathForResource:@"xml2" ofType:@"html"];
        NSString* htmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        //加载本地html字符串
        [_webView loadHTMLString:htmlStr baseURL:nil];
    }

--------------------------

webView的重点:

-------------------------

1:能加载网页,依托于NSURLRequest

    //请求

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:_textField.text]];

   //加载请求

    [_webView loadRequest:request];


2:加载本地html

//加载本地html字符串

    [_webView loadHTMLString:htmlStr baseURL:nil];

     

3:  调用js方法

 
//OC调js方法, func() js函数名

  [_webView stringByEvaluatingJavaScriptFromString:@"func()"];


  这里也可以写直接可以执行的js代码,如:

NSString *height=[webView stringByEvaluatingJavaScriptFromString:@"document.body.clientHeight"];

    NSLog(@"height-- %@ ",height);//打印出了html的高度。



4:js中调用OC方法

    js代码:

    <script type=“javascript”>

    function buttonClick(){

    window.location.href="oc://ocFunc" /*ocFunc -- OC中的方法名,这里前缀可以随便自定义。因为在webView中只要加载一个页面都有回调一个方法。在这个方法中,我们可以监听到连接的地址。*/

    }

    function func(){

    alert("oc来调用了");

    }

    </script>

    <p>金庸射雕三部曲之一。</p>

    <input type="button" onclick="buttonClick"> 观看</input>



在OC代码中:

   在协议方法中 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType


{

//刷新地址栏

    NSString *url = request.URL.absoluteString;

    //js oc

    //分割字符串 :// http oc

    NSArray* array = [url componentsSeparatedByString:@"://"];

   if ([array[0] isEqualToString:@"oc"]) {

        //array[1] 就是要调用的方法名称

//用SEL包装类包装

       SEL sel = NSSelectorFromString(array[1]);

        [self performSelector:sel];

    }

    returnYES;


}


你可能感兴趣的:(UIWebView -- 未来HTML5 于iOS融合的桥梁)