[iOS开发笔记] webview加载+JS交互

优势:

1:加载进度展示自定义

2:可动态获取web页面设置的title,无限复用

3:js交互全部可在此处设置

(适用于新手,大神直接略过)

话不多说,直接代码撸起---



#import "PostPersionMsgController.h"

#import

#import


@interface PostPersionMsgController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>//WKUIDelegate:wkwebview的代理,WKScriptMessageHandler:JS交互代理



@property (nonatomic,strong)WKWebView *webView;

//进度条

@property (nonatomic,strong)UIProgressView *progressView;

//URL

@property (nonatomic,copy)NSString *requrst_URL;


@end


@implementation PostPersionMsgController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfcreateUI];

}


- (void)viewDidAppear:(BOOL)animated {

    [superviewDidAppear:animated];

}


- (void)createUI {

    

//    _requrst_URL = @"https://www.baidu.com";

    _requrst_URL = [NSStringstringWithFormat:requrst_URL];

        

    WKWebViewConfiguration *config = [[WKWebViewConfigurationalloc] init];

    _webView = [[WKWebViewalloc] initWithFrame:[UIScreenmainScreen].boundsconfiguration:config];

    //设置代理

    _webView.navigationDelegate =self;

    _webView.UIDelegate =self;

    _webView.backgroundColor = [UIColorwhiteColor];

    

    [_webView.configuration.userContentControlleraddScriptMessageHandler:selfname:@"JavaScript"];

    

    NSURL *url = [NSURLURLWithString:_requrst_URL];

    NSURLRequest *requset = [NSURLRequestrequestWithURL:url];

    [_webViewloadRequest:requset];

    

    //监听进度条

    [_webViewaddObserver:selfforKeyPath:@"estimatedProgress"options:NSKeyValueObservingOptionNewcontext:nil];

    

    _progressView = [[UIProgressViewalloc] init];

    _progressView.transform =CGAffineTransformMakeScale(1.0,2.0);

    _progressView.trackTintColor = [UIColorwhiteColor];

    _progressView.progressTintColor = [UIColororangeColor];

    

    [self.viewsd_addSubviews:@[_webView,_progressView]];

    self.view.backgroundColor = [UIColorwhiteColor];

    

    //layout

    _topProgressView.sd_layout

    .centerXEqualToView(self.view)

    .topSpaceToView(self.view,64)

    .widthIs(SCREEN_WIDTH -40)

    .heightIs(100);

    

    _webView.sd_layout

    .centerXEqualToView(self.view)

    .widthIs(SCREEN_WIDTH)

    .topSpaceToView(_topProgressView,10)

    .heightIs(SCREEN_HEIGHT -110 - 64);

    

    _progressView.sd_layout

    .topSpaceToView(self.view,64)

    .widthIs(SCREEN_WIDTH)

    .centerXEqualToView(self.view)

    .heightIs(1);

    

}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

    if ([keyPathisEqualToString:@"estimatedProgress"]) {

        if (_webView.estimatedProgress ==1) {

            _progressView.hidden =YES;

        }

        [_progressViewsetProgress:(CGFloat)_webView.estimatedProgressanimated:YES];

        NSLog(@"%f",(CGFloat)_webView.estimatedProgress);

    }

}


- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {

    NSLog(@"开始加载");

}


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {

    NSLog(@"加载完成");

//自适应标题

    self.navigationItem.title =_webView.title;

 

}


- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {

    NSLog(@"加载失败");

}


#pragma mark - WKScriptMessageHandler

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {

    

    if ([message.nameisEqualToString:@"JavaScript"]) {


        NSLog(@"点击需要交互的操作");

    }

    

}


//移除通知

- (void)dealloc {

    

    [_webViewremoveObserver:selfforKeyPath:@"estimatedProgress"];

    [_progressViewreloadInputViews];

    [_webView.configuration.userContentControllerremoveScriptMessageHandlerForName:@"JavaScript"];

    

}

@end


一直想好好写点博客,奈何程序狗的时间都不是自己的,今天关于webview的博客,希望能帮到刚入门的iosr。


你可能感兴趣的:(webview,ios的js交互)