Wkwebview simple use and common use scenarios

Source: Internet
Author: User

Transfer from HTTP://WWW.JIANSHU.COM/P/8F6D527F13BC

features of Wkwebview:
    • High performance, good stability, the use of memory is relatively small,
    • Support JS Interaction
    • Support HTML5 new features
    • You can add a progress bar (then egg, not good, or get used to a third party).
    • Supports built-in gestures,
    • It is said that up to 60fps refresh rate (not card)
It's simple when it's created
    1. Import Wbkit this class library (WKWEBVEIW contained inside)

    2. Abide by the agreement (usually the first two on the line, the third is mainly related to JS, this protocol contains a must be implemented method, this method is to improve the application and web-side interaction, it can directly convert the received JS script to OC or Swift Object-online Big God said, I didn't use. )

@interface ViewController : UIViewController<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>

3. Create a WebView object

    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]]; [self.view addSubview:webView];

Here is a note, the URL must be complete, plus https:// I just started lazy, no add, how to try in the House can not be loaded, showing a white screen!! The lesson of blood!!

Wkwebview has two delegates

Wkwebview agent has two, is WKNavigationDelegate and WKUIDelegate , can decide according to the need to use those methods (of course, don't forget to abide by the agreement before).

他们两个的代理方法有好多个,不过有的不常用就不写了,需要的自己点进去看一下就行啦。。。

1. Wknavigationdelegate to track the loading process

//when the page starts loading-(void) webview: (Wkwebview *) WebView didstartprovisionalnavigation: ( Wknavigation *) navigation; //called when content starts to return-(void) webView: ( Wkwebview *) WebView didcommitnavigation: (wknavigation *) navigation; //the page is loaded after the completion of the call-(void) webView: ( Wkwebview *) WebView didfinishnavigation: (wknavigation *) navigation; //page load failed when called-(void) webView: ( Wkwebview *) WebView didfailprovisionalnavigation: (wknavigation *) navigation;   

Wknavigtiondelegate to page jump

Receive a server jump request before executing-(void)WebView:(Wkwebview *)WebViewDidreceiveserverredirectforprovisionalnavigation:(wknavigation *)NavigationAfter receiving the response, decide whether to jump-(void)WebView:(Wkwebview *)WebViewDecidepolicyfornavigationresponse:(Wknavigationresponse *) navigationresponse decisionHandler:(void (^) (wknavigationresponsepolicy)) decisionhandler;< Span class= "Hljs-comment" >//determines whether to jump -(void) before sending a request webview:(Wkwebview *) webview decidepolicyfornavigationaction:(wknavigationaction *) navigationaction decisionhandler:(void (^) (wknavigationactionpolicy)) Span class= "Hljs-selector-tag" >decisionhandler;           

2. Wkuidelegate

1. Create a new WEBVEIW-(Nullable Wkwebview *)WebView:(Wkwebview *)WebViewCreatewebviewwithconfiguration:(wkwebviewconfiguration *)ConfigurationFornavigationaction:(wknavigationaction *)NavigationactionWindowfeatures:(Wkwindowfeatures *)Windowfeatures;2.WebVeiw off (new method in 9.0)-(void)Webviewdidclose:(Wkwebview *)WebViewNs_available (10_11,9_0);3. Display a JS alert (interacting with JS)-(void)WebView:(Wkwebview *)WebViewRunjavascriptalertpanelwithmessage:(NSString *)MessageInitiatedbyframe:(Wkframeinfo *)FrameCompletionhandler:(void (^) (void))Completionhandler;4. Popup An input box (interacting with JS)-(void)WebView:(Wkwebview *)WebViewRunjavascripttextinputpanelwithprompt:(NSString *)PromptDefaultText:(NullableNSString *)DefaultTextInitiatedbyframe:(Wkframeinfo *)Framecompletionhandler:(void (^) (NSString * __) Nullable result) completionhandler; 5. Display a confirmation box (JS) -(void) webview:(Wkwebview *) webview  Runjavascriptconfirmpanelwithmessage:(NSString *) message initiatedbyframe:(Wkframeinfo *) frame  Completionhandler:(void (^) (BOOL result))  Completionhandler               

(Ps:wkuidelegate is primarily about interacting with Web pages, which can show some of the alert or action of JavaScript, and it looks just like you do.)

For the time being used so much, this demo is purely for beginners to test, the specific complex use of the method and so on later encountered AH!!!!

This article is written in very detailed, we can also refer to the following

2016.11.26 Supplement

issues that automatically close after loading a page

Problem description, I load a Web page, do a variety of operations, than say I recharge, what, and then want to top off this web page after the deposit has been successful to go back to the previous layer or return to an interface, the following method, the General Judgment URL contains the string is given in the background, It's all just a judgment here!

**wknavigationdelegate** inside the proxy method (above)-(void) WebView: (Wkwebview *) WebView Decidepolicyfornavigationresponse: (Wknavigationresponse *) Navigationresponse Decisionhandler: (void (^) (Wknavigationresponsepolicy)) decisionhandler{//gets the URL path of the request. nsstring *requeststring = navigationResponse.response.URL.absoluteString; wklog (@" requeststring:%@ ", requeststring); //encounters a string to make changes nsstring *substr =  @ "www.baidu.com"; if ([requeststring rangeofstring:substr].location! =  Nsnotfound) {wklog (@ "This string has substr"); //callback URL if it contains Baidu, directly return, that is, closed the WebView interface [yes]; } decisionhandler (wknavigationresponsepolicyallow);      

 

Wkwebview simple use and common use scenarios

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.