OC JS 相互调用(二)(WKZViewController)

#import "WKZViewController.h"
#import <WebKit/WebKit.h>
@interface WKZViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler,WKScriptMessageHandler>

@property(strong,nonatomic) WKWebView *webView;

@end

@implementation WKZViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     //[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
  
  self.title = @"登录";
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    //注册js方法
    [config.userContentController addScriptMessageHandler:self name:@"jstonative"];
  WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 20,[UIScreen mainScreen].bounds.size.width ,[UIScreen mainScreen].bounds.size.height  ) configuration:config];
 
    //加载本地页面
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wxtest.hsmdb.com/htzq/ht_login.html?cnl=zjzxqz"]]];
    webView.navigationDelegate = self;
    webView.UIDelegate = self;
    [self.view addSubview:webView];
    
}



//实现js调用ios的handle委托
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    //接受传过来的消息从而决定app调用的方法
    NSString *dict = message.body;
  //  {"funcNo":"100001","funName":"loginOut","chnl":"zjzxqz"}

    
    NSData *jsonData = [dict dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    
    
    if ([dic[@"funName"] isEqualToString:@"loginOut"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
  
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容