UIWebView problems when using the

Source: Internet
Author: User

① the effect of jumping to webview like a regular controller, rather than adding it directly to the current controller
② hide some elements of webview
③webview jump to native app
④ adding a progress bar to WebView

Here's how to fix it:
① basic steps for using WebView

Nsurl *url =*request = [Nsurlrequest requestwithurl:url];[ Self.webview Loadrequest:request]; -(UIWebView *) webview{    if (!  _webview) {        = [[UIWebView alloc]initwithframe:[uiscreen mainscreen].bounds];        _webview. delegate = self ;        [Self.view Addsubview:_webview];    }     return _webview;}

But this webview is directly added to the current controller, to achieve jump effect, you should write a controller, add webview inside
The jump is like this

Mywebviewcontroller *web = [[Mywebviewcontroller alloc]initwithurl:urlgo];[ Self.navigationcontroller Pushviewcontroller:web Animated:yes];

Mywebviewcontroller is defined like this.
. h file

@interface Mywebviewcontroller:uiviewcontroller<uiwebviewdelegate>*urlstr; @end

. m inside

@interfaceMywebviewcontroller () @property (nonatomic,strong) UIWebView*WebView;@end@implementationMywebviewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor= [Uicolor colorwithhexstring:@"#f2f2f2"]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; [Self.webview loadrequest:request];}-(UIWebView *) webview{if(!_webview) {_webview=[[UIWebView alloc]initwithframe:[uiscreen mainscreen].bounds]; _webview.Delegate=Self ;    [Self.view Addsubview:_webview]; }    return_webview;}@end

This will enable the WebView to jump and basic use

② hidden elements of WebView
This needs to be implemented in the proxy method of WebView, and is manipulated with JS. First, use the developer tool on Chrome to get the class of the element you want to hide, and then use

Document.documentElement.getElementsByClassName (' class of element') [0' None '
//Web page loading complete-(void) Webviewdidfinishload: (UIWebView *) webview{//Remove a button[WebView stringbyevaluatingjavascriptfromstring:@"document.documentElement.getElementsByClassName (' Ibar-toggle ') [0].style.display = ' None '"]; //Change controller title to WebView titleNSString *title = [WebView stringbyevaluatingjavascriptfromstring:@"Document.title"]; if(title.length) {Self.title=title; }}

③webview jump to native app
This also needs to be implemented in the proxy method of the WebView

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

Return yes to the place where you need to jump to WebView, jump to the native app and return to No, and then add a way to skip the native app. This requires blocking the comparison of string judgments within the URL. Skip to Product Details page
Http://xxx.com/wap/getItemDetail/146762587051761769.htm

//execute this method before the page loads-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (uiwebviewnavigationtype) navigationtype{nsstring*resueststr =request.        url.absolutestring; if([Resueststr rangeofstring:@"Getitemdetail"].location! =nsnotfound) {Nsarray*TEMPARR1 = [Resueststr componentsseparatedbystring:@"getitemdetail/"]; NSString*TEMPSTR1 =[TempArr1 Lastobject]; NSString*itemcode = [TempStr1 stringbyreplacingoccurrencesofstring:@". htm"Withstring:@""]; Productdetailviewcontroller*VC =[[Productdetailviewcontroller Alloc]initwithitemcode:itemcode];                [Self.navigationcontroller PUSHVIEWCONTROLLER:VC Animated:yes]; returnNO; }        returnYES;}

④ adding a progress bar to WebView
This requires a third-party library Njkwebviewprogress,github address of https://github.com/ninjinkun/NJKWebViewProgress
It's clearly written on the top.
To change the color of the progress bar, you need to modify the Configureviews method in the njkwebviewprogressview.m.

Uicolor *tintcolor = [uicolor colorwithred:0/255.0 Green:224/255.0 Blue:94 /255.0 Alpha:1];

UIWebView problems when using the

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.