| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- //
- // WebViewController.m
- // 千模
- //
- // Created by Drew on 2018/12/19.
- // Copyright © 2018 MUMEI. All rights reserved.
- //
- #import "WebViewController.h"
- #import "Masonry.h"
- #import <WebKit/WebKit.h>
- #import <WXApi.h>
- #import "SharePopViewController.h"
- #import "WeakScriptMessageDelegate.h"
- #import "UploadVideoViewController.h"
- #import "UIImage+Common.h"
- #import "ModelFansViewController.h"
- @interface WebViewController () <ShareDelegate, WKUIDelegate, WKScriptMessageHandler, WKNavigationDelegate>
- @property(nonatomic, strong) WKWebView *webView;
- @end
- @implementation WebViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
- self.navigationItem.leftBarButtonItem = leftItem;
- UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_share"] style:UIBarButtonItemStylePlain target:self action:@selector(share)];
- self.navigationItem.rightBarButtonItem = rightItem;
- self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
- self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
- self.navigationController.navigationBar.translucent = NO;
- WKWebView *webView = [[WKWebView alloc] init];
- [self.view addSubview:webView];
- [webView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.view.mas_left);
- make.right.equalTo(self.view.mas_right);
- make.top.equalTo(self.mas_topLayoutGuide);
- make.bottom.equalTo(self.mas_bottomLayoutGuide);
- }];
- webView.UIDelegate = self;
- webView.navigationDelegate = self;
- [[webView configuration].userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"chmo"];
- [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
- if ([self.url containsString:@"?"]) {
- [NSString stringWithFormat:@"%@&memberpk=%@&activitypk=%@", self.url, [ModelUser user].pk, self.activityPK];
- } else {
- self.url = [NSString stringWithFormat:@"%@?memberpk=%@&activitypk=%@", self.url, [ModelUser user].pk, self.activityPK];
- }
- if (self.modelPK) {
- self.url = [self.url stringByAppendingFormat:@"&modelpk=%@", self.modelPK];
- }
- [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
- self.webView = webView;
- }
- - (void)backClick {
- if ([self.webView canGoBack]) {
- [self.webView goBack];
- } else {
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
- self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
- [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:1.0 green:64 / 255.f blue:149 / 255.f alpha:1.0]] forBarMetrics:UIBarMetricsDefault];
- [self.navigationController.navigationBar setShadowImage:[UIImage new]];
- self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
- self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
- [self.navigationController.navigationBar setShadowImage:nil];
- self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
- self.navigationController.navigationBar.titleTextAttributes = nil;
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- if ([keyPath isEqualToString:@"title"]) {
- if (object == self.webView) {
- self.title = self.webView.title;
- } else {
- [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
- }
- } else {
- [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
- }
- }
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
- if ([@"tel" isEqualToString:navigationAction.request.URL.scheme]) {
- if (@available(iOS 10.0, *)) {
- [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{} completionHandler:nil];
- } else {
- if (![[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
- [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
- }
- }
- decisionHandler(WKNavigationActionPolicyCancel);
- } else {
- decisionHandler(WKNavigationActionPolicyAllow);
- }
- }
- - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
- NSLog(@"name = %@, body = %@", message.name, message.body);
- NSString *action = message.body[@"action"];
- if ([@"signUp" isEqualToString:action]) {
- NSString *activitypk = message.body[@"activitypk"];
- UploadVideoViewController *vc = [[UploadVideoViewController alloc] init];
- vc.activitypk = activitypk;
- [self.navigationController pushViewController:vc animated:YES];
- } else if ([@"fansList" isEqualToString:action]) {
- NSString *modelpk = [message.body[@"modelpk"] stringValue];
- ModelFansViewController *modelFansVC = [[ModelFansViewController alloc] init];
- modelFansVC.modelPK = modelpk;
- modelFansVC.hidesBottomBarWhenPushed = YES;
- [self.navigationController pushViewController:modelFansVC animated:YES];
- }
- }
- - (void)share {
- SharePopViewController *shareVC = [[SharePopViewController alloc] init];
- shareVC.delegate = self;
- [self presentViewController:shareVC animated:NO completion:nil];
- }
- - (void)shareWxSession {
- [self shareToWechat:WXSceneSession withTitle:@"千模通告" description:self.title url:self.url];
- }
- - (void)shareWxTimeline {
- [self shareToWechat:WXSceneTimeline withTitle:self.title description:self.title url:self.url];
- }
- - (void)shareToWechat:(enum WXScene)scene withTitle:(NSString *)title description:(NSString *)description url:(NSString *)url {
- SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
- req.bText = NO;
- req.scene = scene;
- WXMediaMessage *urlMessage = [WXMediaMessage message];
- urlMessage.title = title;
- urlMessage.description = description;
- [urlMessage setThumbImage:[UIImage imageNamed:@"share_icon"]];
- WXWebpageObject *webObj = [WXWebpageObject object];
- webObj.webpageUrl = url;
- urlMessage.mediaObject = webObj;
- req.message = urlMessage;
- [WXApi sendReq:req];
- }
- - (void)dealloc {
- [[self.webView configuration].userContentController removeScriptMessageHandlerForName:@"chmo"];
- [self.webView removeObserver:self forKeyPath:@"title"];
- }
- @end
|