WebView, hey, lazy people dedicated, direct copy and paste can be used, frame adjustable

Source: Internet
Author: User
Tags error code uikit

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. ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.