// // TestViewController.m // QYSDKDemo // // Created by zhongzhida on 2023/4/19. // Copyright © 2023 Netease. All rights reserved. // #import "TestViewController.h" #import #import @interface TestViewController () @property (nonatomic, strong) WKWebView *webView; @end @implementation TestViewController - (void)viewDidLoad { [super viewDidLoad]; CGRect rect = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)); WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.preferences = [[WKPreferences alloc] init]; config.preferences.minimumFontSize = 10; config.preferences.javaScriptEnabled = YES; config.preferences.javaScriptCanOpenWindowsAutomatically = NO; config.userContentController = [[WKUserContentController alloc] init]; config.processPool = [[WKProcessPool alloc] init]; config.userContentController = [WKUserContentController new]; //在创建wkWebView时,需要将被js调用的方法注册进去,oc与js端对应实现 [config.userContentController addScriptMessageHandler:self name:@"receiveYSFMessage"]; WKWebView *wkWebView = [[WKWebView alloc]initWithFrame:rect configuration:config]; self.webView = wkWebView; wkWebView.navigationDelegate = self; wkWebView.UIDelegate = self; NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://10.221.149.18:8002/src/html/demo-debug.html"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10]; [wkWebView loadRequest:request]; [self.view addSubview:wkWebView]; } - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { if ([message.name isEqualToString:@"receiveYSFMessage"]) { NSLog(@"JS调用原生,参数:%@",message.body); NSString *body = message.body; NSData *jsonData = [message.body dataUsingEncoding:NSUTF8StringEncoding]; NSError *error = nil; NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error]; NSString *type = jsonDictionary[@"type"]; NSInteger result = 0; if ([type isEqualToString:@"imagePermission"] || [type isEqualToString:@"videoPermission"]) { // 获取相册权限状态 PHAuthorizationStatus photoLibraryAuthorizationStatus = [PHPhotoLibrary authorizationStatus]; [self nativeCallJS:@{@"seq":jsonDictionary[@"seq"],@"type":type,@"result":@(photoLibraryAuthorizationStatus)}]; } else if ([type isEqualToString:@"filePermission"]) { [self nativeCallJS:@{@"seq":jsonDictionary[@"seq"],@"type":type,@"result":@(1)}]; } else if ([type isEqualToString:@"requestImagePermission"] || [type isEqualToString:@"requestVideoPermission"]) { // 获取相册权限状态 PHAuthorizationStatus photoLibraryAuthorizationStatus = [PHPhotoLibrary authorizationStatus]; if (photoLibraryAuthorizationStatus == PHAuthorizationStatusDenied ) { [[[UIAlertView alloc] initWithTitle:@"没有相册权限" message:@"请在iPhone的“设置-隐私-相册”选项中,允许访问你的相册。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]; } else if (photoLibraryAuthorizationStatus == PHAuthorizationStatusNotDetermined) { // 请求访问相册的权限 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { dispatch_async(dispatch_get_main_queue(), ^{ [self nativeCallJS:@{@"seq":jsonDictionary[@"seq"],@"type":type,@"result":@(status)}]; }); }]; } } } } - (void)nativeCallJS:(NSDictionary *)param { NSData *data = [NSJSONSerialization dataWithJSONObject:param options:0 error:nil]; NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSString *js = [NSString stringWithFormat:@"ysfAppBridge(%@)",str]; [self.webView evaluateJavaScript:js completionHandler:^(id _Nullable response, NSError * _Nullable error) {   //TODO NSLog(@""); }]; } @end