iOS WebView

作者: Bob林 | 来源:发表于2015-11-15 21:45 被阅读3385次

    WKWeb​View

    WKWeb​View 可以参考 http://nshipster.cn/wkwebkit/

    UIWeb​View

    UIWeb​View 缓存

    1.这是官方原版的,根据测试有网络直接读网络,没网络才会读本地

    https://github.com/rnapier/RNCachingURLProtocol
    

    2.下面这个是根据我们的需求来修改了一下,我们的需求是有缓存的话,不用再次联网更新的,节约玩家的流量,
    因为游戏基本是不会修改的,换了连接以后,缓存才会更新,这样就可以实现离线也可以很愉快的玩耍,下次再打开,就秒开啦

    https://github.com/BaiCanLin/RNCachingURLProtocol
    

    UIWeb​View 的一些注意点

    1.当时我们还需要兼容iOS6版本的系统,在iOS6上面,比如分别有AB两个控制器,
    分别加载2个UIWeb​View,只有一个会正常运行,相同的测试在iOS7以上的版本不会有问题。

    2.如果打着断点,给WebView调试的时候突然间奔溃了,把断点去掉即可,当初被这个给坑死了,谷歌后才知道

    UIWeb​View利用CocoaHTTPServer开启本地服务器,加载本地资源

    1.官方介绍

    CocoaHTTPServer is a small, lightweight, embeddable HTTP server for Mac OS X or iOS applications.

    Sometimes developers need an embedded HTTP server in their app.
    Perhaps it's a server application with remote monitoring.
    Or perhaps it's a desktop application using HTTP for the communication backend.
    Or perhaps it's an iOS app providing over-the-air access to documents.
    Whatever your reason, CocoaHTTPServer can get the job done. It provides:

    Built in support for bonjour broadcasting
    IPv4 and IPv6 support
    Asynchronous networking using GCD and standard sockets
    Password protection support
    SSL/TLS encryption support
    Extremely FAST and memory efficient
    Extremely scalable (built entirely upon GCD)
    Heavily commented code
    Very easily extensible
    WebDAV is supported too!

    2.使用非常简单,照着官方的Demo试一下就行,建议使用cocopods安装

    https://github.com/robbiehanson/CocoaHTTPServer
    

    UIWeb​View实现自动登陆

    参考 :
    http://www.jianshu.com/p/072bbc1e4c33?utm_campaign=hugo&utm_medium=reader_share&utm_content=note
    

    UIWeb​View 有类似于微信的加载进度条

    https://github.com/ninjinkun/NJKWebViewProgress
    

    UIWeb​View Javascript进行交互

    1.objective-c调用 javascript :

    [webView stringByEvaluatingJavaScriptFromString:@"alert('done')"];
    

    2.可以自定义协议,比如一个网页,你想调起原生的应用,可以自定义一个协议,通过UIWebView中的委托 shouldStartLoadWithRequest 方法。

    NSString *requestString = [[request URL] absoluteString];
    NSArray *components = [requestString componentsSeparatedByString:@"://"];
    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"这里一般写公司域名"]) {
        if ([[components lastObject] isEqualToString:@"share"]){
              DebugLog(@"监听到 webView 分享");
          }else if ([[components lastObject] isEqualToString:@"gameover"]) {
              DebugLog(@"监听到 webView 游戏结束");
        }
    };
    

    3.WebViewJavascriptBridge是一个Objective-CJavaScript进行消息互通的三方库

    https://github.com/marcuswestin/WebViewJavascriptBridge
    

    试试这个库很不错,根据版本号切换iOS7 <-> iOS8

    pod 'KINWebBrowser'
    

    相关文章

      网友评论

      • 缺舟:求教 如何在加载网页的时候 进行post传值,我使用:
        NSString *url=[dic valueForKey:@"url"];
        // 设置请求路径和头信息
        NSMutableURLRequest* requestShare = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
        // 请求方式
        [requestShare setHTTPMethod: @"POST"];
        // 设参数
        NSString *bodyShare = [NSString stringWithFormat: @"orgCode=%@&currTime=%@", @"q123",@"2016-08-01"];
        // 添加参数
        [requestShare setHTTPBody: [bodyShare dataUsingEncoding: NSUTF8StringEncoding]];
        // 、加载网页
        [self.webView loadRequest:requestShare];

        这种方法不能将参数传递到后台,后台得到的值是null

      本文标题:iOS WebView

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