zoukankan      html  css  js  c++  java
  • 介绍并提高app中WebView的性能

      这周五和老大谈了很长时间的话,感觉颇有收获,在两周做完聊天的任务后,有找到了新的目标,还是那句话,现在还是菜鸟的我正在努力变的不菜,这次我想看着ios developer 文档把我学到的记录下来,这周的目标就是把webView深入学习一下,fighting!!!!

      现在有很多软件都在逐渐加入很多webview元素,其原因是安卓和iOS可以共用一套,成本低,开发速度快,一开我就觉得我去这下好了,活少了,我们app这边根本就不需要做什么直接调用几个方法就搞定,nice,现在想起来还真是天真,要么说菜鸟就是菜鸟,来吧!开始看文档学习(虽然英语只有四级水平,不过文档看起来还算轻松,关键在于你到底是不是从心里就逆反它)。

      说起优化,我发现app端和web端都需要注意的,协调合作才能写出好的程序。

    https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/Introduction/Introduction.html#//apple_ref/doc/uid/TP40002051

    Enhancing the User Experience

      Enhance the user's experience with responsive design to provide different page layouts for varying viewport sizes on handheld and desktop devices. Responsive design can handle CSS and JavaScript detection as well as select the appropriate CSS layout for a device. Responsive design can react to portrait and landscape orientation and multitasking split-screen, automatically switching between layouts with CSS and JavaScript syntax.

      It's easy to add touch and gesture support by adding a few event handlers to your site. Make navigation easier for touchscreen users by making links large enough to reliably hit with a finger, and by surrounding links with enough whitespace to avoid accidentally hitting the wrong link. Leave a blank gutter or border on the page as well, so the user can easily scroll the screen with a finger without touching a link.

      iOS-specific enhancements can turn your website into a web app that behaves like a native iOS app. Optimize websites for iOS by providing an icon for the user's home screen, by making your website into a fullscreen web app, and by including links that dial a phone number, open the Maps app, or open other built-in iOS apps.

      文档中首先是说的是web端的优化,这部分不是我们做,只是简单读读,了解一下就好了,老大这部分你应该让天平去读(别告诉她是我让的)。

      看了半天终于看到根app这边有关的了,而且感觉以后一定会用上的,下面我来介绍一下这个东东

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
        // in this example, the URL from which the user came is http://example.com/profile/?12345
        // determine if the user was viewing a profile
        if ([[url path] isEqualToString:@"/profile"]) {
            // switch to profile view controller
            [self.tabBarController setSelectedViewController:profileViewController];
            // pull the profile id number found in the query string
            NSString *profileID = [url query];
            // pass profileID to profile view controller
            [profileViewController loadProfile:profileID];
        }
        return YES;
    }

    很明了,这就是一个监听在web中点击的链接,我们进行截获,判断如果是我们规定好的地址,我们就可以在这里进行相应的操作。

    Supported File Formats:支持多种文件格式,请看:https://developer.apple.com/library/ios/qa/qa1630/_index.html#//apple_ref/doc/uid/DTS40008749

    -(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
        NSURL *url = [NSURL fileURLWithPath:path];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [webView loadRequest:request];
    }
    
    // Calling -loadDocument:inView:
    [self loadDocument:@"mydocument.rtfd.zip" inView:self.myWebview];

    State Preservation

    In iOS 6 and later, if you assign a value to this view’s restorationIdentifier property, it attempts to preserve its URL history, the scaling and scrolling positions for each page, and information about which page is currently being viewed. During restoration, the view restores these values so that the web content appears just as it did before. For more information about how state preservation and restoration works, see App Programming Guide for iOS.

    For more information about appearance and behavior configuration, see Web Views.

    接下来要介绍一个很重要的知识点:js与webView交互

    在我看来一个属性和一个代理方法方法搞定 

    - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

    script 写你要调的js函数,这块一定要和js函数对应好,如果写错一点,app多不会响应

    NSString *jsCode = [NSString stringWithFormat:@"appBridge.appCallBackHandler('%@', '{"status":%ld }')",[parmers objectForKey:@"callId"],i];
        
        [_web stringByEvaluatingJavaScriptFromString:jsCode];

    jscode 就是web中写好的js我们只需按着那个名称给它传相应的参数,之后调用方法就ok

    说完app调js,现在再来说一下js调app,这个我们没有相应的方法,但是我们也可以利用代理方法做到

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

      NSString *str = [request.URL description];

    //
        if(){
        
            return no;
        }  
        return yes;  
    }    

    if中来用str来判断连接web的重定向链接,截获之后我们就可以在里面进行响应的操作了!!!

    Discussion

    New apps should instead use the evaluateJavaScript:completionHandler: method from the WKWebViewclass. Legacy apps should adopt that method if possible.

    IMPORTANT

    The stringByEvaluatingJavaScriptFromString: method waits synchronously for JavaScript evaluation to complete. If you load web content whose JavaScript code you have not vetted, invoking this method could hang your app. Best practice is to adopt the WKWebView class and use itsevaluateJavaScript:completionHandler: method instead.

    Availability

    Available in iOS 2.0 and later.

    然而看文档会发现,苹果推荐用这个方法

    #import <WebKit/WebKit.h> MKWebView中的 evaluateJavaScript:completionHandler:

    NOTE

    In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView. Additionally, consider setting the WKPreferences property javaScriptEnabled to NO if you render files that are not supposed to run JavaScript.

  • 相关阅读:
    (HDOJ 2034)人见人爱AB
    (UVa 100) The 3n + 1 problem
    (HDOJ 2099)整除的尾数
    (UESTCOJ 1004)8球胜负(eight)
    asp.net中对数据库表插入null空值的问题
    利用模版生成html页
    如何取得IP/用户名等信息
    SQL SERVER的数据类型
    在IIS与.net框架中配置中把扩展名.aspx改成自定义的扩展名.mspx
    c#中什么情况下用(int)什么情况下用Convert.ToInt32?
  • 原文地址:https://www.cnblogs.com/lxgo/p/5041580.html
Copyright © 2011-2022 走看看