IOS Custom Browser (using UIWebView)
UIWebView itself with the way forward, back, refresh, stop and so on.
So we just need to invoke the existing excuses to complete a browser embedded in the app.
For example, the system provides the following methods:
-(void) reload;
-(void) stoploading;
-(void) GoBack;
-(void) GoForward;
Several properties 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;
The load request needs to call several methods, such as (loading the HTML file, loading the NSData data, loading the network request):
-(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 is required to load a network request (nsurl*)
eg
nsurl* url = [Nsurl urlwithstring:@ "http://www.google.com.hk"]; Nsurlrequest*request = [Nsurlrequest requestwithurl:_currenurl]; [Webview_ Loadrequest:request];
Create a new project, add a UIButton and a uitextfield on the home page to execute the event and enter the URL respectively.
Then create a new Browsercontroller controller to load the UIWebView
Then The following variables are defined in the Browsercontroller controller:
// browser
UIWebView *webview;
// function bar
UIView *toolbar;
// function button
UIButton *stopbutton; // Stop loading
UIButton *previousbutton; // back
UIButton *nextbutton; // forward
UIButton *reloadbutton; // Refresh
// current URL
Nsurl *_currenurl;
These variables will be used to load data and perform actions such as forward and backward and flush.
We also need to create different sizes of webview based on different screen sizes, so we define a few macro definitions as follows:
// 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 20.0f
// toolbar height
#define Tool_bar_height
There are several ways to define the following:
// Load Request
-(void) Loadurl: (nsstring *) URL;
-(void) Loadurlof: (nsurl *) URL;
// Initialize function bar
-(void) LoadToolBar;
// Refresh function Bar button
-(void) reflashbuttonstate;
// Create a wait view
-(void) Createloadingview;
// Refresh wait View
-(void) Freshloadingview: (BOOL) b;
You should not use Xib, so rewrite-(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_heigh T-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:@ "Nextbuttonweb.png"]; UIImage *previousdimg =[uiimage imagenamed:@ "Previousbutton.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:uicontroleventtouchupinside]; Previousbutton = [[UIButton alloc]initwithframe:cgrectmake (112.0, 3.0, 24.0, 24.0)]; [Previousbutton setimage:previousdimg Forstate:uicontrolstatenormal]; [Previousbutton addtarget:self Action: @selector (back:) forcontrolevents:uicontroleventtouchupinside]; Nextbutton = [[UIButton alloc]initwithframe:cgrectmake (180.0, 3.0, 24.0, 24.0)]; [Nextbutton setimage:nextimg Forstate:uicontrolstatenormal]; [Nextbutton addtarget:self Action: @selector (Forward:) forcontrolevents:uicontroleventtouchupinside]; 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:uicontroleventtouchupinside]; [ToolBar AddSubview:stopbutton]; [ToolBar Addsubview:previousbutton]; [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" Withst ring:@ "&"]; 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; }}
To create a 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]; This place has to be noted that the frame of the hub must have a view that is consistent with the view that you want to add the hub to, or a mess of fucking crashes. }-(void) Freshloadingview: (BOOL) b{ if (b) { [hub show:yes]; [Hub Hide:yes Afterdelay:3]; } else{ [Hub Hide:yes]; }}
InThe uiwebviewdelegate Agent method handles the refresh of the function bar, and waits 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];}