Once and for all, iOS page view controller packaging process


With the development of H5, in the iOS development, the use of “view of the rate of increase, in order to increase the code package, to alleviate the burden of development, so usually the general class of web page view package, this paper describes the encapsulation process of” controller general view, hope to help everyone.

This article and part of the “once and for all, iOS guide mask packaging process” for the same series, are generic class packaging process sharing, welcome interested friends to read.

Problems to be solved:
  • Version adaptation (UIWebView& & WKWebView)
  • Navigation button shortcut settings (return & & close)
  • Repair custom navigation buttons sideslip gesture failure problem
  • Hand gesture to return to the upper edge of the page function
  • Page loading progress display
  • Page drop-down refresh
  • Request exception
  • HTTPS authentication
Effect diagram demonstration
Once and for all, iOS page view controller packaging process
Version adaptation problem

In the real development we may love to UIWebView, because of its simple call, easy to use, but it will take a lot of memory program, slow loading speed, experience the effect is not very good, “show view, iOS8 launched WKWebView as compared to UIWebView, the memory request is smaller, more efficient therefore, we strongly recommend the use of WKWebView.

Therefore, in the creation of the first page view version adaptation, for more than iOS8 of the system we use WKWebView, or the use of UIWebView.

- (void) createWebView {self.automaticallyAdjustsScrollViewInsets = NO; if ([[[UIDevice currentDevice]systemVersion]floatValue] > = 8) {[self.view} {[self.view addSubview:self.wk_WebView]; else addSubview:self.webView];}}
Two, navigation button shortcut settings (return & & close)

The realization of the function is relatively simple, we can be judged according to the method of goBack, webView or wk_webView, detection of the current web presence can be returned on a display interface, if there exists a return and close button, if there is no display return button.

- (void) showLeftBarButtonItem if ([_webView canGoBack] || {[_wk_WebView canGoBack] = @[self.backBarButtonItem self.navigationItem.leftBarButtonItems) {self.closeBarButtonItem]}, {self.navigationItem.leftBarButtonItem; else = self.backBarButtonItem;}}

The event that closeBarButtonItem handles is closing the interface

- (void) close: (UIBarButtonItem*) item {[self.navigationController popViewControllerAnimated:YES]};}

BackBarButtonItem should be based on whether the web page can return to the previous interface to trigger the return of the former interface or close operation

- (void) back: (UIBarButtonItem*) item if ([_webView canGoBack] || {[_wk_WebView canGoBack]) {[_webView goBack]; [_wk_WebView goBack];} else {[self.navigationController popViewControllerAnimated:YES];}}

Here we can also set the navigation bar title, the current page title, complete method simply calls JS document.title statements can be “loaded, webView and WkWebView call the JS method to differ, as shown in the following code:

//WebView self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@ "document.title"];
//WkWebView [webView evaluateJavaScript:@ "document.title" completionHandler:^ (ID _Nullable Title NSError * _Nullable error) {self.navigationItem.title = title;}];

For WKWebView we can also be achieved by monitoring the title attribute.

Three, repair custom navigation buttons sideslip gesture failure

In the second step we manually set the left navigation bar button, so we will find that the system comes with the sideslip return function failure, in order to achieve “the sideslip return function, we need to fix the failure problem.

- (void) viewWillAppear: (BOOL) animated if ({[super viewWillAppear:animated]; self.navigationController.viewControllers.count > 1) {self.delegate = self.navigationController.interactivePopGestureRecognizer.delegate; self.navigationController.interactivePopGestureRecognizer.delegate = self;}} - (void) viewWillDisappear: (BOOL) animated {[super viewWillDisappear:animated]; self.navigationController.interactivePopGestureRecognizer.delegate = self.delegate;} - (BOOL) gestureRecognizerShouldBegin: (UIGestureRecognizer * gestureRecognizer) {return self.navigationController.viewControllers.count > 1;} - (BOOL) gestureRecognizer: (UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWi ThGestureRecognizer: (UIGestureRecognizer *) otherGestureRecognizer {return > 1;}; self.navigationController.viewControllers.count
Four, hand gesture to return to the upper edge of the page function

The WKWebView system provides a good result for us, set the WkWebView allowsBackForwardNavigationGestures YES can open the left edge of the window slide right back and left back to the right edge of the screen slide effect. (PS: the landlord had spent a whole day trying to achieve this effect, did not think apple is ready (like ` – like ‘) (-)

_wk_WebView.allowsBackForwardNavigationGestures = YES;
Five, web page loading progress display function

For WKWebView we can use the KVO monitoring property estimatedProgress can get the progress of the load changes, and for UIWebView recommended to use NJKWebViewProgress to add.

[_wk_WebView addObserver:self forKeyPath:@ "estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL]; - (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (ID) object change: (NSDictionary< NSKeyValueChangeKey, ID > change context: (* *) void context if ([keyPath) {isEqualToString:@ "estimatedProgress"]) {_loadingProgressView.progress = [change[@ "new" floatValue] (if]; _loadingProgressView.progress = = 1) {dispatch_after (dispatch_time (DISPATCH_TIME_NOW (int64_t) (.4 * NSEC_PER_SEC)), dispatch_get_main_queue (_loadingProgressView.hidden), ^{= YES;}}}});

Don’t forget to remove the dealloc, since it has been added

- (void) dealloc {[_wk_WebView forKeyPath:@ "estimatedProgress";}; removeObserver:self
Six, web drop-down refresh (iOS10 new features)

IOS10 for the ScrollView to add a drop-down refresh control, and the WebView layer control is ScrollView, so we can add drop-down refresh function for the web page. Choose to use the system native control UIRefreshControl, consider some web pages do not need to pull down the refresh function, so it has been limited to open BOOL, such as the need to manually set to YES.

/ / add the drop-down refresh if ([[[UIDevice currentDevice]systemVersion]floatValue] > = 10; & & _canDownRefresh) {_wk_WebView.scrollView.refreshControl = self.refreshControl;}
Seven, the request for the bitmap problem

In the web site request failure or show a blank page need to show the user. We need to judge when the web page request fails and start loading. Taking WKWebView as an example.

- (void) webView: (WKWebView *) webView didFailProvisionalNavigation: (null_unspecified WKNavigation * navigation) withError: (NSError * error{) webView.hidden = YES;} - (void) webView: (WKWebView *) webView didStartProvisionalNavigation: (null_unspecified WKNavigation * navigation{) webView.hidden = NO; _loadingProgressView.hidden = NO; / / not in the "if [webView.URL.scheme isEqual:@" (blank "about") {webView.hidden = YES;}}
Eight, HTTPS certification

In WKWebView, WKNavigationDelegate provides a proxy method for authentication of rights, which makes authentication more simple.

- (void) webView: (WKWebView * webView) didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge * challenge) (completionHandler: (void ^ (NSURLSessionAuthChallengeDisposition) disposition, NSURLCredential *credential) {if ([challenge.protectionSpace.authenticationMethod) completionHandler isEqualToString:NSURLAuthenticationMethodServerTrust] if ([challenge) {previousFailureCount] = = 0) {NSURLCredential *credential = [NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]; completionHandler (NSURLSessionAuthChallengeUseCredential, credential);} else {completionHandler (NSURLSessionAuthChallengeCancelAuthenticationChallenge, Nil);} else {co} MpletionHandler (NSURLSessionAuthChallengeCancelAuthenticationChallenge, Nil);}}

At this point, a simple web view controller that is initially packaged. Follow up will be added to the commonly used JS detailed operation module.

Demo has uploaded GitHub
once and for all, iOS page view controller generic class package