In the. h file of the controller
#import <UIKit/UIKit.h>
#import "FirstView.h"
@interface firstviewcontroller:uiviewcontroller<uiwebviewdelegate> {
UIWebView *web;
Uiactivityindicatorview *activityindicatorview;
UIView *opaqueview;
}
@property (nonatomic, strong) Firstview *FV;
@end
In. m, as follows:
#import "FirstViewController.h"
@interface Firstviewcontroller ()
@end
@implementation Firstviewcontroller
-(void) Loadview {
[Super Loadview];
SELF.FV = [[Firstview alloc] Initwithframe:[uiscreen mainscreen].bounds];
Self.view = SELF.FV;
}
-(void) Viewwillappear: (BOOL) Animated {
Self.tabBarController.navigationController.navigationBar.barTintColor = [Uicolor colorwithred:0.890 green:0.471 blue:0.118 alpha:1.000];
Self.tabBarController.navigationController.title = @ "Treasure in the air";
}
-(void) Viewdidload {
[Super Viewdidload];
web = [[UIWebView alloc]initwithframe:cgrectmake (0, 0, Self.view.frame.size.width, self.view.frame.size.height-40)];
[Does web setuserinteractionenabled:yes];//support interactivity
web.delegate=self;
[Web Setopaque:no]; Opaque is a non-transparent meaning
[Web setscalespagetofit:yes];//automatically scaled to fit the screen
[Self.view Addsubview:web];
Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];
[Web Loadrequest:[nsurlrequest Requestwithurl:url]];
2. Loading local File Resources
/* Nsurl *url = [Nsurl Fileurlwithpath:filepath];
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
[WebView loadrequest:request];*/
3. Read in an HTML and write directly to an HTML code
NSString *htmlpath = [[[NSBundle mainbundle]bundlepath]stringbyappendingstring:@ "webapp/loadar.html"];
NSString *htmlstring = [NSString stringwithcontentsofurl:htmlpath encoding:nsutf8stringencoding Error:NULL];
[WebView loadhtmlstring:htmlstring Baseurl:[nsurl Fileurlwithpath:htmlpath];
Opaqueview = [[UIView alloc]initwithframe:[uiscreen mainscreen].bounds];
Activityindicatorview = [[Uiactivityindicatorview alloc]initwithframe:[uiscreen mainscreen].bounds];
[Activityindicatorview SetCenter:opaqueView.center];
[Activityindicatorview Setactivityindicatorviewstyle:uiactivityindicatorviewstylewhite];
[Opaqueview Setbackgroundcolor:[uicolor Blackcolor];
[Opaqueview setalpha:0.6];
[Self.view Addsubview:opaqueview];
[Opaqueview Addsubview:activityindicatorview];
Do any additional setup after loading the view.
}
-(void) Webviewdidstartload: (UIWebView *) webview{
[Activityindicatorview startanimating];
Opaqueview.hidden = NO;
}
-(void) Webviewdidfinishload: (UIWebView *) webview{
[Activityindicatorview startanimating];
Opaqueview.hidden = YES;
}
UIWebView how to determine HTTP 404 and other errors
-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{
Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];
Nshttpurlresponse *httpresponse = (Nshttpurlresponse *) response;
if (([httpresponse statuscode]/100) = = 2)) {
Self.earthquakedata = [Nsmutabledata data];
[UIApplication sharedapplication].networkactivityindicatorvisible = YES;
[Web loadrequest:[nsurlrequest Requestwithurl:url];
Web.delegate = self;
} else {
Nsdictionary *userinfo = [Nsdictionary dictionarywithobject:
Nslocalizedstring (@ "HTTP Error",
@ "error message displayed when receving a connection error.")
Forkey:nslocalizeddescriptionkey];
Nserror *error = [Nserror errorwithdomain:@ "HTTP" Code:[httpresponse StatusCode] userinfo:userinfo];
if ([error code] = = 404) {
NSLog (@ "xx");
Web.hidden = YES;
}
}
Do any additional setup after loading the view.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
/*
#pragma mark-navigation
In a storyboard-based application, you'll often want to do a little preparation before navigation
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {
Get The new view controller using [Segue Destinationviewcontroller].
Pass the selected object to the new view controller.
}
*/
@end
Run it, it's not successful. ~~