229 lines
9.5 KiB
Objective-C
229 lines
9.5 KiB
Objective-C
//
|
||
// QXPropShopViewController.m
|
||
// QXLive
|
||
//
|
||
// Created by 启星 on 2025/5/9.
|
||
//
|
||
|
||
#import "QXPropShopViewController.h"
|
||
#import <WebKit/WebKit.h>
|
||
#import "QXRechargeViewcController.h"
|
||
|
||
static void *WKWebBrowserContext = &WKWebBrowserContext;
|
||
@interface QXPropShopViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
|
||
@property(nonatomic,strong)WKWebView *contentWebView;
|
||
@property(nonatomic,strong)UIProgressView *progressView;
|
||
//@property (nonatomic,strong)TitleView *titleView;
|
||
@end
|
||
|
||
@implementation QXPropShopViewController
|
||
|
||
- (void)viewDidLoad {
|
||
[super viewDidLoad];
|
||
// Do any additional setup after loading the view.
|
||
// if (@available(iOS 11.0, *)) {
|
||
// self.contentWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollQXPropShopViewControllerViewContentInsetAdjustmentNever;
|
||
// } else {
|
||
// self.automaticallyAdjustsScrollViewInsets = NO;
|
||
// }
|
||
|
||
[self.view addSubview:self.contentWebView];
|
||
[self.view addSubview:self.progressView];
|
||
|
||
[self layoutConstraints];
|
||
[self loadData];
|
||
}
|
||
|
||
-(void)viewWillAppear:(BOOL)animated{
|
||
[super viewWillAppear:animated];
|
||
[self.navigationController setNavigationBarHidden:NO animated:YES];
|
||
}
|
||
-(void)setNavgationItems{
|
||
self.navigationItem.title = @"道具商城";
|
||
UIButton*backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
|
||
[backBtn setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
|
||
backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeading;
|
||
[backBtn addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
|
||
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
|
||
}
|
||
|
||
-(void)backAction{
|
||
if (self.contentWebView.canGoBack) {
|
||
[self.contentWebView goBack];
|
||
}else{
|
||
[self.navigationController popViewControllerAnimated:YES];
|
||
}
|
||
}
|
||
- (void)layoutConstraints {
|
||
[self.contentWebView setFrame:CGRectMake(0, NavContentHeight, SCREEN_WIDTH, SCREEN_HEIGHT-(NavContentHeight))];
|
||
[self.progressView setFrame:CGRectMake(0, NavContentHeight, SCREEN_WIDTH
|
||
, 2)];
|
||
}
|
||
- (void)loadData {
|
||
NSURL* url=[NSURL URLWithString:[NSString stringWithFormat:@"%@web/index.html#/pages/prop/propMall?id=%@&is_show=%@",H5ServerUrl,[QXGlobal shareGlobal].loginModel.token,QXGlobal.shareGlobal.isAppStore?@"0":@"1"]];
|
||
NSURLRequest *request =[NSURLRequest requestWithURL:url];
|
||
[self.contentWebView loadRequest:request];
|
||
}
|
||
|
||
|
||
|
||
#pragma mark - WKNavigationDelegate
|
||
//开始加载
|
||
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
|
||
//开始加载的时候,让加载进度条显示
|
||
self.progressView.hidden = NO;
|
||
}
|
||
|
||
//网页加载完成
|
||
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
|
||
// 获取加载网页的标题
|
||
// if (self.title.length == 0) {
|
||
// self.navigationItem.title = self.contentWebView.title;
|
||
// }
|
||
// if ([self.titleString containsString:@"转账"]) {
|
||
// // 设置字体
|
||
// NSString *fontFamilyStr = @"document.getElementsByTagName('body')[0].style.fontFamily='Arial';";
|
||
// [webView evaluateJavaScript:fontFamilyStr completionHandler:nil];
|
||
// //设置颜色
|
||
// [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#333333'" completionHandler:nil];
|
||
// //修改字体大小
|
||
// [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '150%'"completionHandler:nil];
|
||
// }
|
||
}
|
||
//内容返回时调用
|
||
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
|
||
|
||
}
|
||
|
||
//服务器请求跳转的时候调用
|
||
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
|
||
|
||
}
|
||
|
||
// 内容加载失败时候调用
|
||
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
|
||
|
||
}
|
||
-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
|
||
NSLog(@"message.name====%@ body=%@",message.name,message.body);
|
||
if([message.name isEqualToString:@"nativeHandler"]){
|
||
NSDictionary *dict = message.body;
|
||
if ([dict[@"action"] isEqualToString:@"Recharge"]) {
|
||
QXRechargeViewcController *vc = [[QXRechargeViewcController alloc] init];
|
||
[self.navigationController pushViewController:vc animated:YES];
|
||
}
|
||
}
|
||
}
|
||
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
|
||
//如果是跳转一个新页面
|
||
if (navigationAction.targetFrame == nil) {
|
||
[webView loadRequest:navigationAction.request];
|
||
}
|
||
NSURL *URL = navigationAction.request.URL;
|
||
[self dealSomeThing:URL];
|
||
decisionHandler(WKNavigationActionPolicyAllow);
|
||
}
|
||
|
||
- (void)dealSomeThing:(NSURL *)url{
|
||
NSString *scheme = [url scheme];
|
||
NSString *resourceSpecifier = [url resourceSpecifier];
|
||
if ([scheme isEqualToString:@"tel"]) {
|
||
NSString *callPhone = [NSString stringWithFormat:@"tel://%@", resourceSpecifier];
|
||
/// 防止iOS 10及其之后,拨打电话系统弹出框延迟出现
|
||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
|
||
});
|
||
}
|
||
}
|
||
|
||
//进度条
|
||
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
|
||
}
|
||
|
||
//KVO监听进度条
|
||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
||
|
||
if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.contentWebView) {
|
||
[self.progressView setAlpha:1.0f];
|
||
BOOL animated = self.contentWebView.estimatedProgress > self.progressView.progress;
|
||
[self.progressView setProgress:self.contentWebView.estimatedProgress animated:animated];
|
||
|
||
// Once complete, fade out UIProgressView
|
||
if(self.contentWebView.estimatedProgress >= 1.0f) {
|
||
[UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
|
||
[self.progressView setAlpha:0.0f];
|
||
} completion:^(BOOL finished) {
|
||
[self.progressView setProgress:0.0f animated:NO];
|
||
}];
|
||
}
|
||
}
|
||
else {
|
||
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
||
}
|
||
}
|
||
|
||
#pragma mark - getters and setters
|
||
- (WKWebView *)contentWebView {
|
||
if (!_contentWebView) {
|
||
//设置网页的配置文件
|
||
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
|
||
// 允许可以与网页交互,选择视图
|
||
configuration.selectionGranularity = YES;
|
||
// web内容处理池pr
|
||
configuration.processPool = [[WKProcessPool alloc] init];
|
||
//自定义配置,一般用于 js调用oc方法(OC拦截URL中的数据做自定义操作)
|
||
WKUserContentController * UserContentController = [[WKUserContentController alloc]init];
|
||
// 允许用户更改网页的设置
|
||
[UserContentController addScriptMessageHandler:self name:@"login"];
|
||
[UserContentController addScriptMessageHandler:self name:@"nativeHandler"];
|
||
configuration.preferences.javaScriptEnabled = YES;
|
||
configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
|
||
configuration.userContentController = UserContentController;
|
||
// 此处一定要做判断,因为是iOS9之后才有的方法,否则在iOS8下会崩溃
|
||
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
|
||
//允许视频播放
|
||
configuration.allowsAirPlayForMediaPlayback = YES;
|
||
// 允许在线播放
|
||
configuration.allowsInlineMediaPlayback = YES;
|
||
//开启手势触摸 默认设置就是NO。在ios8系统中会导致手势问题,程序崩溃
|
||
_contentWebView.allowsBackForwardNavigationGestures = YES;
|
||
}
|
||
_contentWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
|
||
_contentWebView.backgroundColor = [UIColor clearColor];
|
||
_contentWebView.opaque = YES;
|
||
//适应你设定的尺寸
|
||
[_contentWebView sizeToFit];
|
||
_contentWebView.scrollView.showsVerticalScrollIndicator = NO;
|
||
// 设置代理
|
||
_contentWebView.navigationDelegate = self;
|
||
//kvo 添加进度监控
|
||
[_contentWebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:WKWebBrowserContext];
|
||
}
|
||
return _contentWebView;
|
||
}
|
||
|
||
- (UIProgressView *)progressView {
|
||
if (!_progressView) {
|
||
_progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
|
||
[_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
|
||
_progressView.progressTintColor = QXConfig.themeColor;
|
||
}
|
||
return _progressView;
|
||
}
|
||
|
||
-(void)setProgressColor:(UIColor *)progressColor{
|
||
_progressView.progressTintColor = progressColor;
|
||
}
|
||
// 记得dealloc
|
||
- (void)dealloc {
|
||
if (self) {
|
||
[self.contentWebView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
|
||
}
|
||
}
|
||
- (void)didReceiveMemoryWarning {
|
||
[super didReceiveMemoryWarning];
|
||
// Dispose of any resources that can be recreated.
|
||
}
|
||
|
||
@end
|