123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- //
- // SPSBHtmlViewController.m
- // 我的社保
- //
- // Created by shanp on 2021/4/26.
- //
- #import "SPSBHtmlViewController.h"
- #import <WebKit/WebKit.h>
- #import "SPSBUIGeneralHeader.h"
- #import "SPSBBaseNetworkModel.h"
- #import "UIViewController+SPSBNavigationBar.h"
- #import "UIViewController+SPSBInitialization.h"
- #import "SPSBConfirmAlertViewController.h"
- #import "SPSBShareViewController.h"
- @interface SPSBShareWebViewModel : SPSBBaseNetworkModel
- @property (nonatomic, copy) NSString *spsb_title;
- @property (nonatomic, copy) NSString *spsb_content;
- @property (nonatomic, copy) NSString *spsb_url;
- @property (nonatomic, copy) NSString *spsb_imageUrl;
- @end
- @implementation SPSBShareWebViewModel
- @end
- @interface SPSBHtmlViewController ()<WKUIDelegate, WKNavigationDelegate> {
- UIButton *_shareButton;
- SPSBShareWebViewModel *_shareWebViewModel;
- UIActivityIndicatorView *_activity;
- NSString *_alert;
- UIButton *_backButton;
- UIButton *_directBackButton;
- }
- @end
- @implementation SPSBHtmlViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self setupUI];
- }
- #pragma mark - JS Action
- - (void)webexit:(NSString *)sender {
- if (self.navigationController.viewControllers.count == 1) {
- [self directCancelAction];
- } else {
- [self directBackAction];
- }
- if (_spsb_callBack) {
- NSDictionary *dic = jxh_jsonToDictionary([sender stringByRemovingPercentEncoding]);
- _spsb_callBack(dic);
- }
- }
- - (void)setData:(NSString *)data {
- NSDictionary *dic = jxh_jsonToDictionary(data);
- if (dic[@"exitHint"]) {
- _alert = dic[@"exitHint"];
- }
- }
- - (void)setHtmlTitle:(NSString *)data {
- NSString *data1 = [data stringByRemovingPercentEncoding];
- self.spsb_titleLabel.text = data1;
- }
- #pragma mark - Action
- - (void)shareAction {
- if (!_shareWebViewModel) return;
- id image;
- if (![_shareWebViewModel.spsb_imageUrl isEqualToString:@""]) {
- image = _shareWebViewModel.spsb_imageUrl;
- } else {
- image = [UIImage imageNamed:SPSBLogo];
- }
- SPSBShareModel *model = [[SPSBShareModel alloc] initWithTypeArray:@[SPSBShareCircleOfFriends, SPSBShareWechat, SPSBShareMiniProgram, SPSBShareQQ, SPSBShareQzone, SPSBShareSinaWeibo] content:_shareWebViewModel.spsb_content image:image title:_shareWebViewModel.spsb_title url:_shareWebViewModel.spsb_url mediaType:SSDKContentTypeWebPage eventId:nil finish:nil];
- SPSBShareViewController *vc = SPSBShareViewController.new;
- vc.spsb_data = model;
- [self presentViewController:vc animated:false completion:nil];
- }
- - (void)backAction {
- if ([_spsb_webView canGoBack]) {
- _directBackButton.hidden = false;
- [_spsb_webView goBack];
- return;
- }
- [self directBackAction];
- }
- - (void)directBackAction {
- if (_alert) {
- SPSBConfirmAlertViewController *vc = [SPSBConfirmAlertViewController alertWithTitle:_alert content:nil];
- [vc setCancelButtonTitle:nil titleColor:nil action:^{
- }];
- @weakify(self)
- [vc setConfirmButtonTitle:nil titleColor:nil action:^{
- @strongify(self)
- [self.navigationController popViewControllerAnimated:true];
- }];
- [self presentViewController:vc animated:false completion:nil];
- return;
- }
- [self.navigationController popViewControllerAnimated:true];
- }
- - (void)cancelAction {
- if ([_spsb_webView canGoBack]) {
- _directBackButton.hidden = false;
- [_spsb_webView goBack];
- return;
- }
- [self directCancelAction];
- }
- - (void)directCancelAction {
- if (_alert) {
- SPSBConfirmAlertViewController *vc = [SPSBConfirmAlertViewController alertWithTitle:_alert content:nil];
- [vc setCancelButtonTitle:nil titleColor:nil action:^{
- }];
- @weakify(self)
- [vc setConfirmButtonTitle:nil titleColor:nil action:^{
- @strongify(self)
- [self dismissViewControllerAnimated:true completion:nil];
- }];
- [self presentViewController:vc animated:false completion:nil];
- return;
- }
- [self dismissViewControllerAnimated:true completion:nil];
- }
- - (void)setTitle {
- @weakify(self)
- [_spsb_webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable title, NSError * _Nullable error) {
- @strongify(self)
- if (title) {
- self.spsb_titleLabel.text = title;
- }
- }];
- }
- #pragma mark - WKUIDelegate
- - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
- completionHandler();
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
- [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- }]];
- [self presentViewController:alertController animated:true completion:nil];
- }
- - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
- [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- completionHandler(false);
- }]];
- [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- completionHandler(true);
- }]];
- [self presentViewController:alertController animated:true completion:nil];
- }
- - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];
- [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
- textField.text = defaultText;
- }];
- [alertController addAction:[UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- completionHandler(alertController.textFields[0].text?:@"");
- }]];
- [self presentViewController:alertController animated:true completion:nil];
- }
- #pragma mark - WKWebViewDelegate
- - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
- _activity.hidden = false;
- [_activity startAnimating];
- }
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
- _activity.hidden = true;
- [_activity stopAnimating];
- @weakify(self)
- [webView evaluateJavaScript:@"spIosShare()" completionHandler:^(id _Nullable data, NSError * _Nullable error) {
- @strongify(self)
- if ([data isKindOfClass:[NSString class]]) {
- NSString *shareString = data;
- NSData *jsonData = [shareString dataUsingEncoding:NSUTF8StringEncoding];
- NSError *err;
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
- options:NSJSONReadingMutableContainers
- error:&err];
- if (!err) {
- self->_shareButton.hidden = false;
- self->_shareWebViewModel = SPSBShareWebViewModel.new;
- [self->_shareWebViewModel handleDataWithDictionary:dic];
- }
- }
- }];
- [self setTitle];
- }
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
- debugLog(@"asss");
- bool isAllow = [self handleRequest:navigationAction.request];
- if (isAllow) {
- //处理电话
- NSURL *URL = navigationAction.request.URL;
- NSString *scheme = [URL scheme];
- if ([scheme isEqualToString:@"tel"]) {
- NSString *resourceSpecifier = [URL resourceSpecifier];
- NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", resourceSpecifier];
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
- decisionHandler(WKNavigationActionPolicyCancel);
- return;
- }
- //如果是跳转一个新页面
- if (navigationAction.targetFrame == nil) {
- [webView loadRequest:navigationAction.request];
- }
- decisionHandler(WKNavigationActionPolicyAllow);
- [self setTitle];
- } else {
- decisionHandler(WKNavigationActionPolicyCancel);
- }
- }
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
- decisionHandler(WKNavigationResponsePolicyAllow);
- }
- #pragma mark - Request
- - (bool)handleRequest:(NSURLRequest *)request {
- NSMutableString *requestString = [[[request URL] absoluteString] mutableCopy];
- debugLog(@"--------%@", requestString);
- if ([requestString hasPrefix:@"alipay"]) {
- [jxh_application() openURL:[NSURL URLWithString:requestString]];
- return false;
- }
- if ([requestString hasPrefix:@"weixin://wap/pay?"]) {
- [jxh_application() openURL:[NSURL URLWithString:requestString]];
- return false;
- }
- if ([requestString containsString:@"//itunes.apple.com/"] || [requestString containsString:@"//apps.apple.com"]) {
- [jxh_application() openURL:[NSURL URLWithString:requestString]];
- return false;
- }
- if (_spsb_isJsMethod) {
- NSArray *array = [requestString componentsSeparatedByString:@":"];
- debugLog(@"++++++%@", array);
- if (array.count > 1 && [array[0] isEqualToString:@"iosvsjs"]) {
- if (array.count > 2) {
- [requestString deleteCharactersInRange:NSMakeRange(0, ((NSString *)array[0]).length + ((NSString *)array[1]).length + 2)];
- SEL action = NSSelectorFromString([NSString stringWithFormat:@"%@:", array[1]]);
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- [self performSelector:action withObject:requestString];
- #pragma clang diagnostic pop
- }
- else {
- SEL action = NSSelectorFromString(array[1]);
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- [self performSelector:action];
- #pragma clang diagnostic pop
- }
-
- return false;
- }
- }
- return true;
- }
- #pragma mark - Network Action
- #pragma mark - UI
- - (void)setupUI {
- self.spsb_isHiddenNavigationBar = true;
- [self createNavigationBar];
- [self addWebView];
- [self addActivity];
- [self addDirectBackButton];
- [self addShareButton];
- }
- - (void)addWebView {
- NSURLCache * cache = [NSURLCache sharedURLCache];
- [cache removeAllCachedResponses];
- [cache setDiskCapacity:0];
- [cache setMemoryCapacity:0];
- _spsb_webView = [[WKWebView alloc] init];
- _spsb_webView.UIDelegate = self;
- _spsb_webView.navigationDelegate = self;
- _spsb_webView.scrollView.bounces = false;
- _spsb_webView.allowsLinkPreview = false;
- _spsb_webView.scrollView.showsVerticalScrollIndicator = false;
-
- if (_spsb_jsString) {
- WKUserScript *script = [[WKUserScript alloc] initWithSource:_spsb_jsString injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
- [_spsb_webView.configuration.userContentController addUserScript:script];
- }
-
-
- if (@available(iOS 11.0, *)) {
- _spsb_webView.scrollView.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
- }
- [self.view addSubview:_spsb_webView];
- [_spsb_webView makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.and.trailing.and.bottom.equalTo(0);
- make.top.equalTo(self.spsb_navigationBar.bottom);
- }];
- if (![_spsb_url hasPrefix:@"http"]) {
- _spsb_url = [NSString stringWithFormat:@"http://%@", _spsb_url];
- }
- NSDate *date = [NSDate new];
- NSTimeInterval time = [date timeIntervalSince1970] * 1000;
- NSURL *url;
- if ([_spsb_url containsString:@"?"]) {
- url = [NSURL URLWithString:[NSString stringWithFormat:@"%@&time=%lf", _spsb_url, time]];
- } else {
- url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?time=%lf", _spsb_url, time]];
- }
- [_spsb_webView loadRequest:[NSURLRequest requestWithURL:url]];
-
-
- }
- - (void)addActivity {
- _activity = [[UIActivityIndicatorView alloc] init];
- [_activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
- [self.view addSubview:_activity];
- [_activity makeConstraints:^(JXHConstraintMaker *make) {
- make.center.equalTo(0);
- }];
- }
- - (void)addDirectBackButton {
- [self.spsb_titleLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.update(120);
- make.trailing.update(-120);
- }];
-
- [self.spsb_barBackButton removeFromSuperview];
- _backButton = [UIButton convenienceWithTarget:nil action:nil];
- [_backButton setImage:jxh_getImage(arrow_back_blue) state:JXHButtonControlStateNormal];
- [_backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
- [self.spsb_navigationBar addSubview:_backButton];
- [_backButton makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(SPSBFixMargin);
- make.bottom.equalTo(0);
- make.size.equalTo((CGSize){50, jxh_navigationViewHeight()});
- }];
-
- _directBackButton = [UIButton convenienceWithTarget:nil action:nil];
- [_directBackButton setImage:jxh_getImage(nav_del) state:JXHButtonControlStateNormal];
- [_directBackButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
- [self.spsb_navigationBar addSubview:_directBackButton];
- [_directBackButton makeConstraints:^(JXHConstraintMaker *make) {
- make.leading.equalTo(self->_backButton.trailing);
- make.centerY.equalTo(self->_backButton);
- make.size.equalTo((CGSize){50, jxh_navigationViewHeight()});
- }];
- _directBackButton.hidden = true;
-
- if (self.navigationController.viewControllers.count == 1) {
- [_directBackButton addTarget:self action:@selector(directCancelAction) forControlEvents:UIControlEventTouchUpInside];
- [_backButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
- } else {
- [_directBackButton addTarget:self action:@selector(directBackAction) forControlEvents:UIControlEventTouchUpInside];
- [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
- }
-
-
- }
- - (void)addShareButton {
- _shareButton = [UIButton convenienceWithTarget:self action:@selector(shareAction)];
- [_shareButton setImage:jxh_getImage(share_btn) state:JXHButtonControlStateNormal];
- [_shareButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
- [self.spsb_navigationBar addSubview:_shareButton];
- [_shareButton makeConstraints:^(JXHConstraintMaker *make) {
- make.trailing.equalTo(-SPSBFixMargin);
- make.centerY.equalTo(self->_backButton);
- make.size.equalTo((CGSize){64, jxh_navigationViewHeight()});
- }];
- _shareButton.hidden = true;
- }
- @end
|