Custom browser for iOS (using UIWebView)
UIWebView itself comes with methods such as forward, backward, refresh, and stop.
Therefore, we only need to call the existing excuse to complete an embedded browser of the application.
For example, the system provides the following methods:
-(Void) reload;
-(Void) stopLoading;
-(Void) goBack;
-(Void) goForward;
The following attributes are provided to indicate whether these methods are available:
@ Property (nonatomic, readonly, getter = canGoBack) BOOL canGoBack;
@ Property (nonatomic, readonly, getter = canGoForward) BOOL canGoForward;
@ Property (nonatomic, readonly, getter = isLoading) BOOL loading;
To load a request, you need to call the following methods, such as loading html files, loading NSData data, and loading network requests ):
-(Void) loadRequest :( NSURLRequest *) request;
-(Void) loadHTMLString :( NSString *) string baseURL :( NSURL *) baseURL;
-(Void) loadData :( NSData *) data
MIMEType :( NSString *) MIMEType
TextEncodingName :( NSString *) textEncodingName
BaseURL: (NSURL *) baseURL;
A network address (NSUrl *) is required for loading network requests *)
Eg:
NSURL* url = [NSURL URLWithString:@"http://www.google.com.hk"]; NSURLRequest*request = [NSURLRequest requestWithURL:_currenURL]; [webView_ loadRequest:request];
Create a new project and add a UIButton and a UITextField on the home page to execute the event and enter the URL respectively.
Create a new BrowserController controller to load the UIWebView.
Then, the BrowserController controller defines the following variables:
// Browser
UIWebView * webView;
// Function bar
UIView * toolBar;
// Function button
UIButton * stopButton; // stop loading
UIButton * previusbutton; // return
UIButton * nextButton; // forward
UIButton * reloadButton; // refresh
// Current url
NSURL * _ currenURL;
These variables are used to load data and perform operations such as forward, backward, and refresh.
We also need to create webviews of different sizes based on different screen sizes, so the following macro definitions are defined:
// Screen width
# Define SCREEN_WIDTH ([UIScreen mainScreen]. bounds. size. width)
// Screen height
# Define SCREEN_HEIGHT ([UIScreen mainScreen]. bounds. size. height)
// Navigation Bar Height
# Define NAVIGATION_BAR_HEIGHT 44.0f
// Status Bar Height
# Define STATUS_BAR_HEIGHT 20366f
// Toolbar height
# Define TOOL_BAR_HEIGHT 30
You also need to define the following methods:
// Load the request
-(Void) loadUrl :( NSString *) url;
-(Void) loadURLof :( NSURL *) url;
// Initialization function bar
-(Void) loadToolBar;
// Refresh the function bar button
-(Void) reflashButtonState;
// Create a waiting View
-(Void) createLoadingView;
// Refresh the wait View
-(Void) freshLoadingView :( BOOL) B;
Therefore, rewrite the-(void) loadView; method to create the interface.
-(Void) loadView {[super loadView]; if (webView = nil) {webView = [[UIWebView alloc] initWithFrame: CGRectMake (0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT-NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT-30)]; webView. delegate = self; webView. scalesPageToFit = YES; [self. view addSubview: webView];} if (! ToolBar) {[self loadToolBar];}/*** Description: load function bar * Input: * Output: * Return: * Others: */-(void) loadToolBar {float toolY = self. view. frame. size. height-30-44-20; toolBar = [[UIView alloc] initWithFrame: CGRectMake (0.0, toolY, 320.0, 30.0)]; toolBar. backgroundColor = [UIColor orangeColor]; // webView images UIImage * stopImg = [UIImage imageNamed: @ "stopButton.png"]; UIImage * nextImg = [UIImage imageNamed: @ "photo"]; UIImage * previusdimg = [UIImage imageNamed: @ "previusbutton.png"]; UIImage * reloadImg = [UIImage imageNamed: @ "reloadButton.png"]; // function button stopButton = [[UIButton alloc] initWithFrame: CGRectMake (44.0, 3.0, 24.0, 24.0)]; [stopButton setImage: stopImg forState: UIControlStateNormal]; [stopButton addTarget: self action: @ selector (stopWebView :) forControlEvents: Inputs]; previusbutton = [[UIButton alloc] initWithFrame: CGRectMake (112.0, 3.0, 24.0, 24.0)]; [previusbutton setImage: previusdimg forState: Success]; [previusbutton addTarget: self action: @ selector (back :) forControlEvents: Success]; nextButton = [[UIButton alloc] initWithFrame: CGRectMake (180.0, 3.0, 24.0, 24.0)]; [nextButton setImage: nextImg forState: Unknown]; [nextButton addTarget: self action: @ selector (forward :) forControlEvents: Unknown]; reloadButton = [[UIButton alloc] initWithFrame: CGRectMake (248.0, 3.0, 24.0, 24.0)]; [reloadButton setImage: reloadImg forState: UIControlStateNormal]; [reloadButton addTarget: self action: @ selector (reload :) forControlEvents: Success]; [toolBar addSubview: stopButton]; [toolBar addSubview: previusbutton]; [toolBar addSubview: nextButton]; [toolBar addSubview: reloadButton]; [self. view addSubview: toolBar];}
Other methods:
#pragma mark - webView actions- (void)back:(id)sender{ if (webView.canGoBack) { [webView goBack]; }}- (void)reload:(id)sender{ [webView reload]; [self freshLoadingView:YES];}- (void)forward:(id)sender{ if (webView.canGoForward) { [webView goForward]; }}- (void)stopWebView:(id)sender{[webView stopLoading];}- (void)loadUrl:(NSString *)url{ if (webView) { url = [url stringByReplacingOccurrencesOfString:@"%26" withString:@"&"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [webView loadRequest:request]; }}- (void)loadURLof:(NSURL *)url{ self.currenURL = url;}- (void)reflashButtonState{ if (webView.canGoBack) { previousButton.enabled = YES; } else { previousButton.enabled = NO; } if (webView.canGoForward) { nextButton.enabled = YES; } else { nextButton.enabled = NO; }}
Create wait View:
-(Void) createLoadingView {UIView * v = [UIApplication sharedApplication]. keyWindow; hub = [[MBProgressHUD alloc] initWithView: v]; hub. delegate = self; hub. removeFromSuperViewOnHide = YES; hub. labelText = @ "loading... "; [v addSubview: hub]; // note that the view of the frame set for the hub must be the same as the view for the hub to be added. Otherwise, the crash will be a mess .} -(Void) freshLoadingView :( BOOL) B {if (B) {[hub show: YES]; [hub hide: YES afterDelay: 3];} else {[hub hide: YES] ;}}
In the UIWebViewDelegate proxy method, refresh the function bar and wait for the view.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ [self reflashButtonState]; [self freshLoadingView:YES]; NSURL *theUrl = [request URL]; self.currenURL = theUrl; return YES;}- (void)webViewDidFinishLoad:(UIWebView *)webView{ [self reflashButtonState]; [self freshLoadingView:NO];}- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [self reflashButtonState]; [self freshLoadingView:NO];}