Introduce and improve the performance of webview in apps

Source: Internet
Author: User
Tags home screen

This Friday and eldest talk for a long time words, feel quite harvest, after two weeks to finish the task of chatting, have found a new goal, or that sentence, now or rookie I am trying to change the dish, this time I want to see the iOS developer document I learned the record down, This week's goal is to take webview in-depth study, fighting!!!!

Now a lot of software is gradually adding a lot of webview elements, the reason is that Android and iOS can share a set, low cost, development speed, a open I think I go to this good, live less, we do not need to do the app here directly call a few methods to get it done, nice, Now think of it is really naïve, or say rookie is rookie, come on! Start looking at document learning (although English is only four levels, but the document looks easy, the key is whether you reverse it from the heart).

Speaking of optimization, I found that both the app side and the web side need to be aware of, coordination and cooperation in order to write good programs.

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 HANDH Eld 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 b Etween 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 hits with a finger, and by Surroundin G links with enough whitespace to avoid accidentally hitting the wrong link. Leave a blank gutter or border on the page as well, the user can easily scroll the screens with a finger without touchin G A link.

Ios-specific enhancements can turn your website into a web app the behaves like a native IOS app. Optimize Websites for I OS by providing an icon for the user's home screen, by making your website to a fullscreen web app, and by including Lin KS that dial a phone number, open the Maps app, or open other built-in IOS apps.

In the first part of the document is the web-side optimization, which is not what we do, but simply read, understand the good, boss this part you should let the balance to read (don't tell her I let it).

Watched for a long time finally see the root of the app here, and the feeling will be used later, let me introduce this stuff

-(BOOL) Application: (UIApplication *) application OpenURL: (nsurl *) URL sourceapplication: (NSString *) sourceapplication Annotation: (ID) annotation{//in this example, the URL from which the user came ishttp://example.com/profile/?12345    //determine if the user is 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 stringNSString *profileid =[url query]; //Pass profileID to Profile view Controller[Profileviewcontroller Loadprofile:profileid]; }    returnYES;}

Very clear, this is a listener in the Web click on the link, we intercept, to determine if we have a good address, we can do the corresponding operation here.

Supported file Formats: supports a variety of files formats, see: Https://developer.apple.com/library/ios/qa/qa1630/_index.html#//apple_ref/doc /uid/dts40008749

-(void) Loaddocument: (nsstring*) DocumentName InView: (uiwebview*) webview{    *path = [[ NSBundle Mainbundle] Pathforresource:documentname Oftype:nil];     *url = [Nsurl Fileurlwithpath:path];     *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 property restorationIdentifier , it attempts to preserve it URL history, the Scal ing and scrolling positions for each page, and information on which page is currently being viewed. During Restoration, the view restores these values so and the Web content appears just as it did before. For more information about what state preservation and restoration works, see Apps Programmingguide for IOS.

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

The next step is to introduce a very important point of knowledge: JS and WebView interaction

It seems to me that a property and a proxy method approach is done

-(NSString *) stringbyevaluatingjavascriptfromstring: (NSString *) script

Script write you want to tune the JS function, this piece must and JS function corresponding good, if write a bit wrong, the app will not respond

NSString *jscode = [NSString stringWithFormat:@ "appbridge.appcallbackhandler ('%@ ', ' {\' ) Status\":%ld} ')", [parmers objectforkey:@ "callid"],i];        [_web Stringbyevaluatingjavascriptfromstring:jscode];

Jscode is the Web to write the JS we just press that name to pass the corresponding parameters, and then call the method OK

After saying the app JS, and now for the JS Tune app, this we do not have the corresponding method, but we can also use the proxy method to do

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) navigationtype{

NSString *STR = [Request. URL description];

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

If in the use of STR to determine the connection Web redirect link, intercepted after we can be in the inside to respond to the operation!!!

Discussion

New apps should instead use the evaluateJavaScript:completionHandler: method from the WKWebView class. 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 that has not vetted, the invoking this method could the hang your app. Best Practice is to adopt the WKWebView class and use its evaluateJavaScript:completionHandler: method instead.

Availability

Available in IOS 2.0 and later.

However, looking at the document will find that Apple recommends this method

#import <WebKit/WebKit.h> in MkwebviewevaluateJavaScript:completionHandler:

NOTE

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

Introduce and improve the performance of webview in apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.