The basics of UIWebView Web loading components in IOS and examples of how to use them _ios

Source: Internet
Author: User

Basic Usage Example

-(void) viewdidload {[Super viewdidload];

  Do no additional setup after loading the view.
  UIWebView * WebView = [[UIWebView alloc]initwithframe:cgrectmake (0, ScreenWidth, ScreenHeight-20)];
  The automatic team page is scaled to fit the screen webview.scalespagetofit = YES;
  webview.userinteractionenabled = YES;

  Webview.opaque = YES;

  [Self.view Addsubview:webview];
  Nsurl * url = [nsurl urlwithstring:@ "http://www.youku.com"];
  Nsurlrequest * request = [nsurlrequest Requestwithurl:url];
[WebView Loadrequest:request];
NSString * Myht = @ "Youku";
[WebView Loadhtmlstring:myht Baseurl:url];    /* [WebView goBack];   return [WebView GoForward];
  Go to [WebView Reload];
 [WebView stoploading];

  * * webview.delegate = self;
  Removes a scrolling outer shadow uiscrollview *scrollview = Webview.scrollview;
    for (int i = 0; i < ScrollView.subviews.count i++) {UIView *view = [Scrollview.subviews objectatindex:i];
    if ([View Iskindofclass:[uiimageview class]]) {View.hidden = YES;
 } } #pragma mark-uiwebviewdelegate-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) req Uest Navigationtype: (uiwebviewnavigationtype) navigationtype{/** * typedef ns_enum (Nsinteger, UIWEBVIEWNAVIGATIONTYP E) {* uiwebviewnavigationtypelinkclicked, * uiwebviewnavigationtypeformsubmitted, * UIWEBVIEWNAVIGATIONTYPEBACKF 
   Orward, * uiwebviewnavigationtypereload, * uiwebviewnavigationtypeformresubmitted, * uiwebviewnavigationtypeother
   };

  * * nslog_function;
return YES;

}//Start loading-(void) Webviewdidstartload: (UIWebView *) webview{nslog_function;

}//Complete loading-(void) Webviewdidfinishload: (UIWebView *) webview{nslog_function; }//Load failed, pop-up error prompt-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (Nserror *) error{Uialertview = [[Uialertview alloc] initwithtitle:@ "" Message:[error Localizeddescription] Delegate:nil cance
  Lbuttontitle:nil otherbuttontitles:@ "OK", nil]; [AlterviEW show];
  [Alterview release];

Nslog_function;

 }


Here are some tips on how to use it:

1. Let the Web page adapt to the width of the phone screen

If you use UIWebView to display some PC station pages, you will find that the page will exceed the screen, it looks very bad, at this time can be in webviewdidfinishload this agent through JS add a meta:

-(void) Webviewdidfinishload: (UIWebView *) webview

{

  nsstring *meta = [NSString stringwithformat:@] Document.getelementsbyname (\ "viewport\") [0].content = \ "Width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\ "", Iphone_width];

  [WebView Stringbyevaluatingjavascriptfromstring:meta];

}

Note: Use this method to set the UIWebView scalespagetofit to No

Webview.scalespagetofit = NO;

2. Add a click event to the picture in the Web page and zoom in when you click on the picture

The idea is to add the onclick event to each IMG tag, encapsulate the img src attribute into a special URL in the event and intercept it.

If you are using loadhtmlstring to load a Web page, you can do the following sentence to replace it:

Copy Code code as follows:

HTML = [HTML stringbyreplacingoccurrencesofstring:@ "

If it is through loadrequest, it is necessary to webviewdidfinishload the following JS:

  NSString *js = @ "var IMGs = document.getElementsByTagName (\" img\ ");"

  " for (Var i=0;i 
 

And then through the WebView agent method to intercept, get the image URL, then you can do a variety of processing

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) navigationtype

{

  nsstring *url = Request. url.absolutestring;

  if ([url hasprefix:@ "http://src."])

  {

    url = [url stringbyreplacingoccurrencesofstring:@ ' http://src. ' withstring:@ '];

    Do something.

    return NO;

  }

  Return YES

}

3. Add a page header to the UIWebView that follows the page scrolling

UIWebView contains a scrollview, you can add a page header to the ScrollView to follow the web scrolling effect

CGFloat headerheight = 36.0f;

Note: The Y coordinate must be a negative number, iphone_width is the screen width

uiview *headerview = [[UIView alloc] Initwithframe:cgrectmake (0,-headerheight, Iphone_width, Headerheight)];

[_webview.scrollview Addsubview:_headerview];

Modify the Contentinset of the WebView ScrollView, leaving a little space at the top of the

uiedgeinsets edgeinset = _webview.scrollview.contentinset;

_webview.scrollview.contentinset = Uiedgeinsetsmake (Headerview.frameheight, Edgeinset.left, EdgeInset.bottom, Edgeinset.right);

Related Article

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.