Import Header File
#import "NJKWebViewProgressView.h"#import "NJKWebViewProgress.h"
Compliance Agreement
<UIWebViewDelegate, NJKWebViewProgressDelegate>
Implementation code
@implementationviewcontroller{Iboutlet __WeakUIWebView *_webview; Njkwebviewprogressview *_webviewprogressview; Njkwebviewprogress *_webviewprogress;} - (void) viewdidload{[Super Viewdidload]; _webviewprogress = [[Njkwebviewprogress alloc] init]; _webview. Delegate = _webviewprogress; _webviewprogress. webviewproxydelegate =Self _webviewprogress. progressdelegate =SelfCGRect navbounds =Self. Navigationcontroller. Navigationbar. Bounds;CGRect Barframe =CGRectMake (0, Navbounds. Size. Height-2, Navbounds. Size. width,2); _webviewprogressview = [[Njkwebviewprogressview alloc] Initwithframe:barframe];_webviewprogressview. Autoresizingmask =Uiviewautoresizingflexiblewidth |Uiviewautoresizingflexibletopmargin; [_webviewprogressview setprogress:0 Animated:YES]; [Self Loadbaidu]; [Self.navigationcontroller.navigationbar addSubview:_ Webviewprogressview];} -(void) loadbidu{nsurlrequest *req = [[nsurlrequest alloc" Initwithurl:[nsurl urlwithstring:@" http://www.baidu.com/"]; [_webview loadrequest:req];} #pragma mark-njkwebviewprogressdelegate-(void) Webviewprogress: (njkwebviewprogress *) webviewprogress updateprogress: (float) progress{ [_webviewprogressview setprogress:progress animated:yes]; self.title = [_webview Stringbyevaluatingjavascriptfromstring:@ "Document.title"];}
Njkwebviewprogress is how to do, analysis is as follows:
- Webviewdidstartload is the start of a request, all requests go through it, before loading the resource, can get a URL how many resources need to load, use _loadingcount++ way to count.
- webviewdidfinishload, is the end of a request, each request ends _loadingcount--, and re-count progress
- progress using _loadingcount/_maxloadcount the way to calculate
- Every time webviewdidfinishload,Didfailloadwitherror requests are added to the Waitforcompletejs such as JS into the Web view, To detect if the Web page is loaded.
- To get the progress of the logic and display the view of the progress of separate writing, with the agent to connect two classes, the structure is clear, the implementation will be more convenient
Njkwebviewprogress--webview progress bar