排版樣式可能不是太好 你可以把以下代碼全部復制到一個新的UIViewController里面就行
實現(xiàn)功能弯囊,獲取webview的title和進度值
//? WebCheckViewController.m
//? FamilySchoolPro
//
//? Created by yzq on 15/11/23.
//? Copyright ? 2015年 renxiaoxu. All rights reserved.
//
#import "WebCheckViewController.h"
#import
@interface WebCheckViewController ()
@property (nonatomic, strong) WKWebView *webView;
@property(nonatomic,strong)UIProgressView *progressView;
@end
@implementationWebCheckViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.title=self.mytitle;
? ? if ([self.mytitle isEqualToString:@"聯(lián)系客戶經(jīng)理"]) {
? ? ? ? //? ? ? ? self.webView.dataDetectorTypes = UIDataDetectorTypeNone;
? ? }
? ? UIBarButtonItem *_addressBookItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_bt_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backBookAction)];
? ? _addressBookItem.imageInsets=UIEdgeInsetsMake(0,0,0,0);
? ? [_addressBookItemsetTitle:@"返回"];
? ? [_addressBookItemsetTintColor:RGB_MD(120,209,97)];
? ? [self.navigationController.navigationBar setTintColor:RGB_MD(120, 209, 97)];
? ? UIBarButtonItem *closeBookItem = [[UIBarButtonItem alloc] initWithTitle:@"關(guān)閉" style:UIBarButtonItemStylePlain target:self action:@selector(closeBookAction)];
? ? NSArray*buttonArray = [[NSArrayalloc]initWithObjects:_addressBookItem,closeBookItem,nil];
? ? self.navigationItem.leftBarButtonItems = buttonArray;
? ? self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
? ? [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
? ? [self.webView setNavigationDelegate:self];
? ? [self.webView setUIDelegate:self];
? ? //? ? [_webView setMultipleTouchEnabled:YES];
? ? //? ? [_webView setAutoresizesSubviews:YES];
? ? [self.webView.scrollView setAlwaysBounceVertical:YES];
? ? // 這行代碼可以是側(cè)滑返回webView的上一級忠蝗,而不是根控制器(*只針對側(cè)滑有效)
? ? //? ? [_webView setAllowsBackForwardNavigationGestures:true];
? ? [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
? ? [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
? ? [self.view insertSubview:self.webView belowSubview:self.progressView];
? ? [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.link]]];
}
-(BOOL)hidesBottomBarWhenPushed{
? ? return YES;
}
//- (WKWebView *)webView{
//? ? if (_webView == nil) {
//? ? ? ? _webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
//? ? ? ? [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
////? ? ? ? [_webView setNavigationDelegate:self];
//? ? ? ? [_webView setUIDelegate:self];
//? ? ? ? [_webView setMultipleTouchEnabled:YES];
//? ? ? ? [_webView setAutoresizesSubviews:YES];
//? ? ? ? [_webView.scrollView setAlwaysBounceVertical:YES];
//? ? ? ? // 這行代碼可以是側(cè)滑返回webView的上一級,而不是根控制器(*只針對側(cè)滑有效)
//? ? ? ? [_webView setAllowsBackForwardNavigationGestures:true];
//
////? ? ? ? [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
//? ? ? ? [self.view insertSubview:_webView belowSubview:self.progressView];
//? ? }
//? ? return _webView;
//}
- (UIProgressView*)progressView
{
? ? if(!_progressView)
? ? {
? ? ? ? _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 5)];
? ? ? ? self.progressView.tintColor= [UIColorgreenColor];
? ? ? ? self.progressView.trackTintColor = [UIColor clearColor];
? ? ? ? [self.viewaddSubview:self.progressView];
? ? }
? ? return _progressView;
}
-(void)backBookAction{
? ? if([self.webViewcanGoBack]) {
? ? ? ? [self.webViewgoBack];
? ? }else{
? ? ? ? [self.navigationController popViewControllerAnimated:YES];
? ? }
}
-(void)closeBookAction{
? ? [self.navigationController popViewControllerAnimated:YES];
}
// 記得取消監(jiān)聽
- (void)dealloc {
? ? [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
? ? [self.webView removeObserver:self forKeyPath:@"title"];
}
#pragma mark - WKNavigationDelegate
// 頁面開始加載時調(diào)用
- (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*)navigation{
}
// 當內(nèi)容開始返回時調(diào)用
- (void)webView:(WKWebView*)webView didCommitNavigation:(WKNavigation*)navigation{
}
// 頁面加載完成之后調(diào)用
- (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation{
}
// 頁面加載失敗時調(diào)用
- (void)webView:(WKWebView*)webView didFailProvisionalNavigation:(WKNavigation*)navigation{
? ? [[LoadingAnimation sharedInstance] hideLoadingAnimation];
? ? [PromptAction showErrorPrompt:@"網(wǎng)頁加載失敗..."];
}
// 接收到服務(wù)器跳轉(zhuǎn)請求之后調(diào)用
- (void)webView:(WKWebView*)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation*)navigation{
}
// 在收到響應(yīng)后呀潭,決定是否跳轉(zhuǎn)
- (void)webView:(WKWebView*)webView decidePolicyForNavigationResponse:(WKNavigationResponse*)navigationResponse decisionHandler:(void(^)(WKNavigationResponsePolicy))decisionHandler{
? ? NSLog(@"%@",navigationResponse.response.URL.absoluteString);
? ? //允許跳轉(zhuǎn)
? ? decisionHandler(WKNavigationResponsePolicyAllow);
? ? //不允許跳轉(zhuǎn)
? ? //decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在發(fā)送請求之前颖医,決定是否跳轉(zhuǎn)
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{
? ? NSLog(@"%@",navigationAction.request.URL.absoluteString);
? ? /**
?? ? *? 攔截app跳轉(zhuǎn)
?? ? */
? ? NSString*string = navigationAction.request.URL.absoluteString;
? ? if(string.length>24) {
? ? ? ? string = [stringsubstringToIndex:23];//截取掉下標23之后的字符串
? ? }
? ? if ([string isEqualToString:@"http://itunes.apple.com"]) {
? ? ? ? //不允許跳轉(zhuǎn)
? ? ? ? decisionHandler(WKNavigationActionPolicyCancel);
? ? }
? ? //允許跳轉(zhuǎn)
? ? decisionHandler(WKNavigationActionPolicyAllow);
}
// 計算wkWebView進度條
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
? ? if(object ==self.webView&& [keyPathisEqualToString:@"estimatedProgress"]) {
? ? ? ? CGFloatnewprogress = [[changeobjectForKey:NSKeyValueChangeNewKey]doubleValue];
? ? ? ? if(newprogress ==1) {
? ? ? ? ? ? self.progressView.hidden=YES;
? ? ? ? ? ? [self.progressViewsetProgress:0animated:NO];
? ? ? ? }else{
? ? ? ? ? ? self.progressView.hidden=NO;
? ? ? ? ? ? [self.progressViewsetProgress:newprogressanimated:YES];
? ? ? ? }
? ? }
? ? //網(wǎng)頁title
? ? elseif([keyPathisEqualToString:@"title"])
? ? {
? ? ? ? if(object ==self.webView)
? ? ? ? {
? ? ? ? ? ? self.title=self.webView.title;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? [superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];
? ? ? ? }
? ? }
? ? else
? ? {
? ? ? ? [superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];
? ? }
}
-(WKWebView*)webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration forNavigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures
{
? ? if(!navigationAction.targetFrame.isMainFrame) {
? ? ? ? [webViewloadRequest:navigationAction.request];
? ? }
? ? return nil;
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
/*
?#pragma mark - Navigation
?// In a storyboard-based application, you will 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