UIWebView使用時的問題

來源:互聯網
上載者:User

標籤:

①像普通controller那樣實現跳轉到webview的效果,而不是直接加到當前controller
②隱藏webview的某些元素
③webview跳往原生app
④給webview添加進度條

解決方案如下:
①使用webview的基本步驟

NSURL *url = [NSURL URLWithString:self.urlStr];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;}

但這樣webview是直接加在當前controller的,要實現跳轉效果,應當寫個controller,裡邊再添加webview
跳轉是這樣的

MyWebViewController *web = [[MyWebViewController alloc]initWithUrl:urlGo];[self.navigationController pushViewController:web animated:YES];

MyWebViewController是這樣定義的
.h檔案

@interface MyWebViewController : UIViewController<UIWebViewDelegate>@property(nonatomic,copy) NSString *urlStr;@end

.m內

@interface MyWebViewController ()@property(nonatomic,strong) UIWebView *webView;@end@implementation MyWebViewController- (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

這樣就能實現webview的跳轉和基本使用

 

②隱藏webview的元素
這需要在webview的代理方法中實現,並且用js來操作。先在chrome上用開發人員工具,擷取要隱藏的元素的class,然後用

document.documentElement.getElementsByClassName(‘元素的class‘)[0].style.display = ‘none‘
//網頁載入完成-(void)webViewDidFinishLoad:(UIWebView *)webView{    //去除某個按鈕    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName(‘ibar-toggle‘)[0].style.display = ‘none‘"];        //將controller標題改為webview標題    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];    if (title.length) {        self.title = title;    }}

 

③webview跳往原生app
這同樣需要在webview的代理方法中實現

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

在需要跳往webview的地方返回YES,跳往原生app的地方返回NO,再添加跳原生app的方法。這需要攔截url內的字串判斷比較。如跳往商品詳情頁
http://xxx.com/wap/getItemDetail/146762587051761769.htm

//網頁載入之前執行該方法-(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];                return NO;    }        return YES;}

 

④給webview添加進度條
這需要用到第三方庫NJKWebViewProgress,github地址為https://github.com/ninjinkun/NJKWebViewProgress
上邊寫的已經很清楚
要改變進度條的顏色,需要在NJKWebViewProgressView.m裡configureViews方法修改

UIColor *tintColor = [UIColor colorWithRed:0/255.0 green:224/255.0 blue:94/255.0 alpha:1];

 

UIWebView使用時的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.