Files
yinmeng-ios/xplan-ios/Main/XPWebViewController.m

691 lines
31 KiB
Mathematica
Raw Normal View History

2021-09-16 17:21:52 +08:00
//
// XPWebViewController.m
// xplan-ios
//
// Created by zu on 2021/9/16.
//
#import "XPWebViewController.h"
#import "AccountInfoStorage.h"
#import "ThemeColor.h"
#import "ApiHost.h"
#import "YYUtility.h"
#import "HttpRequestHelper.h"
2021-11-23 19:27:18 +08:00
#import "XPShareView.h"
2021-09-23 18:10:20 +08:00
#import "TTPopup.h"
#import "XPMacro.h"
2021-09-16 17:21:52 +08:00
#import <Masonry/Masonry.h>
#import <MJExtension/MJExtension.h>
2021-09-23 14:52:12 +08:00
#import <RPSDK/RPSDK.h>
2022-01-14 11:38:09 +08:00
#import "XCCurrentVCStackManager.h"
#import "XPMineUserInfoViewController.h"
#import "XPMineRechargeViewController.h"
#import "NSMutableDictionary+Saft.h"
2023-01-03 15:48:07 +08:00
#import "Base64.h"
2022-08-04 11:42:49 +08:00
///vc
#import "XPMineDressUpViewController.h"
#import "XPDressUpShopViewController.h"
#import "XPRoomViewController.h"
2022-08-04 18:22:31 +08:00
#import "RoomHostDelegate.h"
#import "XPMonentTopicContainerViewController.h"
2021-09-16 17:21:52 +08:00
2021-11-24 14:31:24 +08:00
typedef NS_ENUM(NSUInteger, RightNavigationPushType){
///h5
RightNavigationPushType_Web = 1,
///
RightNavigationPushType_Share = 2,
///
RightNavigationPushType_AppPage = 3,
///
RightNavigationPushType_SharePicture = 5
};
@interface WeakWebViewScriptMessageDelegate : NSObject<WKScriptMessageHandler>
2021-09-16 17:21:52 +08:00
//WKScriptMessageHandler JavaScriptOC
@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
@end
@implementation WeakWebViewScriptMessageDelegate
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate {
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
//WKScriptMessageHandler
//JSname
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([self.scriptDelegate respondsToSelector:@selector(userContentController:didReceiveScriptMessage:)]) {
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
}
@end
2021-09-23 18:10:20 +08:00
@interface XPWebViewController () <WKNavigationDelegate, WKScriptMessageHandler, XCShareViewDelegate>
2021-09-16 17:21:52 +08:00
@property (strong, nonatomic) WKWebView *webview;
@property (strong, nonatomic) UIProgressView *progressView;
@property (nonatomic, strong) WKUserContentController *userContentController;
2021-11-24 14:31:24 +08:00
///
@property (nonatomic,copy) NSDictionary *shareDic;
///
@property (nonatomic,assign) BOOL isHiddenNav;
2021-09-16 17:21:52 +08:00
@end
NSString * const kJSOpenPurse = @"openPurse";
NSString * const kJSOpenChargePage = @"openChargePage";
NSString * const kJSOpenSharePage = @"openSharePage";
NSString * const kJSGetUid = @"getUid";
NSString * const kJSGetDeviceId = @"getDeviceId";
NSString * const kJSGetTicket = @"getTicket";
NSString * const kJSGetDeviceInfo = @"getDeviceInfo";
2021-09-23 14:52:12 +08:00
NSString * const kJSOpenFaceLiveness = @"openFaceLiveness";
2021-11-24 14:31:24 +08:00
NSString * const kJSInitNav = @"initNav";
2021-12-23 14:44:08 +08:00
NSString * const kJSGetRoomUid = @"getRoomUid";
2022-01-14 11:38:09 +08:00
NSString * const kOpenPersonPage = @"openPersonPage";
NSString * const kInitShowNav = @"initShowNav";
NSString * const kCloseWebView = @"closeWebView";
2022-08-04 11:42:49 +08:00
NSString * const kJumpAppointPage = @"jumpAppointPage";
NSString * const kJSOpenRoom = @"openRoom";
2022-08-04 16:31:27 +08:00
NSString * const kJSOpenRoomForGiftId = @"openRoomForGiftId";
2022-10-31 15:56:46 +08:00
NSString * const kProhibitRightSlip = @"prohibitRightSlip";
2023-01-03 15:48:07 +08:00
NSString * const kSaveBase64Image = @"saveBase64Image";
2021-09-16 17:21:52 +08:00
@implementation XPWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.isHiddenNav = NO;
2021-09-16 17:21:52 +08:00
[self initView];
}
2022-10-31 15:56:46 +08:00
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.navigationController setNavigationBarHidden:self.isHiddenNav animated:animated];
}
2021-09-16 17:21:52 +08:00
- (void)initView {
if (self.navigationController.viewControllers.count > 1){
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"common_nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
2021-12-17 19:31:26 +08:00
leftBarButtonItem.tintColor = [ThemeColor mainTextColor];
2021-09-16 17:21:52 +08:00
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}
if (@available(iOS 11.0, *)) {
self.webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else{
self.automaticallyAdjustsScrollViewInsets = NO;
}
2021-09-16 17:21:52 +08:00
[self.view addSubview:self.webview];
[self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.bottom.mas_equalTo(self.view);
}];
[self.view addSubview:self.progressView];
}
2021-12-09 20:58:54 +08:00
//
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
//progressView
self.progressView.hidden = YES;
if (self.urlLoadCompleted) {
self.urlLoadCompleted(YES, nil);
}
}
//
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
//progressView
self.progressView.hidden = YES;
if (self.urlLoadCompleted) {
self.urlLoadCompleted(NO, error);
}
}
2022-01-13 19:39:29 +08:00
//
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *URL = navigationAction.request.URL;
NSString *scheme = [URL scheme];
NSURLRequest *request = navigationAction.request;
// Judge is whether to jump to other app.
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:request.URL];
if (canOpen) {
[[UIApplication sharedApplication] openURL:request.URL];
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
2021-09-16 17:21:52 +08:00
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
2021-09-23 18:10:20 +08:00
[self.webview evaluateJavaScript:@"document.location.href" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
NSLog(@"%@", response);
NSString *currentUrl = [NSString stringWithFormat:@"%@", response];
///host,便h5
#ifdef DEBUG
if (currentUrl != nil) {
#else
if (currentUrl != nil) {
#endif
2021-09-23 18:10:20 +08:00
if ([message.name isEqualToString:kJSOpenSharePage]) {
2021-11-24 14:31:24 +08:00
if (message.body && message.body != [NSNull null]) {
NSDictionary *body;
//dic
if ([message.body isKindOfClass:[NSDictionary class]]) {
body = message.body;
} else if ([message.body isKindOfClass:[NSString class]]) {
body = [message.body toJSONObject];
}
self.shareDic = body[@"data"];
[self showSharePanel];
}
2021-09-23 18:10:20 +08:00
} else if ([message.name isEqualToString:kJSGetUid]) {
NSString *uid = [[AccountInfoStorage instance] getUid];
NSString *js = [NSString stringWithFormat:@"getMessage(\"uid\",%@)", uid];
[self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) {
NSLog(@"%@",error);
}];
2021-09-23 18:10:20 +08:00
} else if ([message.name isEqualToString:kJSGetTicket]) {
NSString *ticket = [[AccountInfoStorage instance] getTicket];
NSString *js = [NSString stringWithFormat:@"getMessage(\"ticket\",\"%@\")",ticket];
[self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) {
NSLog(@"%@",error);
}];
} else if ([message.name isEqualToString:kJSGetDeviceId]) {
NSString *js = [NSString stringWithFormat:@"getMessage(\"deviceId\",\"%@\")",[YYUtility deviceUniqueIdentification]];
[self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) {
NSLog(@"%@",error);
}];
} else if ([message.name isEqualToString:kJSGetDeviceInfo]) {
NSDictionary *basicParmars = [HttpRequestHelper configBaseParmars:[[NSDictionary alloc] init]];
NSString *json = [basicParmars mj_JSONString];
NSString *js = [NSString stringWithFormat:@"getMessage(\"deviceInfo\",%@)", json];
[self.webview evaluateJavaScript:js completionHandler:^(id _Nullable ohter, NSError * _Nullable error) {
NSLog(@"%@", error);
}];
} else if ([message.name isEqualToString:kJSOpenPurse]) {
} else if ([message.name isEqualToString:kJSOpenChargePage]) {
[TTPopup dismiss];
UIViewController *controller = [XCCurrentVCStackManager shareManager].getCurrentVC;
if (controller.presentingViewController) {
[controller dismissViewControllerAnimated:NO completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
XPMineRechargeViewController * rechargeVC = [[XPMineRechargeViewController alloc] init];
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:rechargeVC animated:YES];
});
}else {
XPMineRechargeViewController * rechargeVC = [[XPMineRechargeViewController alloc] init];
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:rechargeVC animated:YES];
}
2022-01-14 11:38:09 +08:00
} else if ([message.name isEqualToString:kOpenPersonPage]) {
NSString *uid = [NSString stringWithFormat:@"%@",message.body];
if (uid.integerValue > 0) {
[TTPopup dismiss];
XPMineUserInfoViewController * userInfoVC = [[XPMineUserInfoViewController alloc] init];
userInfoVC.uid = uid.integerValue;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:userInfoVC animated:YES];
}
} else if ([message.name isEqualToString:kJSOpenFaceLiveness]) {
2021-09-23 18:10:20 +08:00
NSString *verifyToken = message.body;
2021-09-23 14:52:12 +08:00
#if TARGET_OS_IPHONE
2021-09-23 18:10:20 +08:00
[RPSDK startWithVerifyToken:verifyToken viewController:self.navigationController completion:^(RPResult * _Nonnull auditState) {
NSString *jsStrUrl;
switch (auditState.state) {
case RPStatePass:
jsStrUrl = [NSString stringWithFormat:@"renderByStatus(\"%d\")", 2];
break;
case RPStateFail:
jsStrUrl = [NSString stringWithFormat:@"renderByStatus(\"%d\")", 1];
break;
case RPStateNotVerify:
jsStrUrl = [NSString stringWithFormat:@"renderByStatus(\"%d\")", -1];
break;
}
[self.webview evaluateJavaScript:jsStrUrl completionHandler:^(id _Nullable other, NSError * _Nullable error) {
NSLog(@"%@",error);
}];
}];
2021-09-23 14:52:12 +08:00
#endif
2021-11-24 14:31:24 +08:00
} else if([message.name isEqualToString:kJSInitNav]) {
[self initNav:message.body];
2021-12-23 14:44:08 +08:00
} else if([message.name isEqualToString:kJSGetRoomUid]) {
NSString *js = [NSString stringWithFormat:@"getMessage(\"roomUid\",\"%@\")",self.roomUid];
[self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) {
NSLog(@"%@",error);
}];
} else if([message.name isEqualToString:kInitShowNav]) {
if (((NSNumber *)message.body).intValue == 0) {
2022-04-07 18:06:38 +08:00
if (self.InitShowNavBlock) {
self.InitShowNavBlock(YES);
return;
}
self.isHiddenNav = YES;
if (self.navigationController) {
[self.navigationController setNavigationBarHidden:YES];
}
}
} else if([message.name isEqualToString:kCloseWebView]) {
2022-04-07 18:06:38 +08:00
if (self.CloseWebViewBlock) {
self.CloseWebViewBlock(YES);
return;
}
if (self.navigationController) {
[self.navigationController popViewControllerAnimated:YES];
}
2022-08-04 11:42:49 +08:00
} else if ([message.name isEqualToString:kJumpAppointPage]) {
// h5
NSDictionary *bodyDict;
if ([message.body isKindOfClass:[NSDictionary class]]) {
bodyDict = message.body;
} else if ([message.body isKindOfClass:[NSString class]]) {
NSString *str = (NSString *)message.body;
bodyDict = [str toJSONObject];
}
NSInteger skyType = [bodyDict[@"routerType"] integerValue];
[self handleRouterType:skyType message:message];
} else if ([message.name isEqualToString:kJSOpenRoom]) {
NSString *uid = [NSString stringWithFormat:@"%@",message.body];
if (uid.length > 0) {
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[XPRoomViewController class]]) {
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController popToRootViewControllerAnimated:NO];
XPRoomViewController<RoomHostDelegate> * rooomVC = obj;
[TTPopup dismiss];
[rooomVC exitRoom];
*stop = YES;
}
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[XPRoomViewController openRoom:uid viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
});
2022-08-04 11:42:49 +08:00
}
2022-10-31 15:56:46 +08:00
} else if ([message.name isEqualToString:kProhibitRightSlip]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
} else if([message.name isEqualToString:kJSOpenRoomForGiftId]) {
2022-08-04 16:31:27 +08:00
NSLog(@"%@", message.body);
NSDictionary *bodyDict;
if ([message.body isKindOfClass:[NSDictionary class]]) {
bodyDict = message.body;
} else if ([message.body isKindOfClass:[NSString class]]) {
NSString *str = (NSString *)message.body;
bodyDict = [str toJSONObject];
}
2022-08-04 18:22:31 +08:00
// uid
NSString *uid = [NSString stringWithFormat:@"%@", bodyDict[@"uid"]];
NSString *giftId = [NSString stringWithFormat:@"%@", bodyDict[@"giftId"]];
if (uid.length) {
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[XPRoomViewController class]]) {
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController popToRootViewControllerAnimated:NO];
XPRoomViewController<RoomHostDelegate> * rooomVC = obj;
[rooomVC exitRoom];
*stop = YES;
}
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[XPRoomViewController openRoom:uid giftId:giftId viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
});
}
2023-01-03 15:48:07 +08:00
} else if([message.name isEqualToString:kSaveBase64Image]) {
NSString * base64 = message.body;
NSArray * array = [base64 componentsSeparatedByString:@","];
if(array.count > 1) {
NSData * data = [Base64 decode:array[1]];
UIImage * image = [UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
}
2021-09-23 18:10:20 +08:00
}
}];
2021-09-16 17:21:52 +08:00
}
2023-01-03 15:48:07 +08:00
#pragma mark - private metho
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (error == nil) {
NSString * jsStrUrl = [NSString stringWithFormat:@"saveBase64ImageCallback(\"%@\")", @"1"];
[self.webview evaluateJavaScript:jsStrUrl completionHandler:^(id _Nullable other, NSError * _Nullable error) {
NSLog(@"%@",error);
}];
} else {
NSString * jsStrUrl = [NSString stringWithFormat:@"saveBase64ImageCallback(\"%@\")", @"0"];
[self.webview evaluateJavaScript:jsStrUrl completionHandler:^(id _Nullable other, NSError * _Nullable error) {
NSLog(@"%@",error);
}];
}
}
2022-08-04 11:42:49 +08:00
- (void)handleRouterType:(NSInteger)skyType message:(WKScriptMessage *)message {
NSDictionary *bodyDict;
if ([message.body isKindOfClass:[NSDictionary class]]) {
bodyDict = message.body;
} else if ([message.body isKindOfClass:[NSString class]]) {
NSString *str = (NSString *)message.body;
bodyDict = [str toJSONObject];
}
2022-08-04 11:42:49 +08:00
switch (skyType) {
case 1:
{
// uid
NSString *uid = [NSString stringWithFormat:@"%@", bodyDict[@"routerVal"]];
if (uid.length) {
[XPRoomViewController openRoom:uid viewController:[XCCurrentVCStackManager shareManager].getCurrentVC];
}
}
break;
case 7:
{
//
XPMineDressUpViewController *vc = [[XPMineDressUpViewController alloc] init];
vc.currentIndex = 1;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:vc animated:YES];
}
break;
case 8:
{
//
XPMineDressUpViewController *vc = [[XPMineDressUpViewController alloc] init];
vc.currentIndex = 0;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:vc animated:YES];
}
break;
case 75:
{//
NSString *topicId = bodyDict[@"routerVal"];
XPMonentTopicContainerViewController * topicVC = [[XPMonentTopicContainerViewController alloc] init];
topicVC.worldId = topicId;
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:topicVC animated:YES];
}
2022-08-04 11:42:49 +08:00
default:
break;
}
}
2021-09-16 17:21:52 +08:00
- (void)setUrl:(NSString *)urlString{
_url = urlString;
if (_url == nil) {
return;
}
if (![_url hasPrefix:@"http"] && ![_url hasPrefix:@"https"]){
_url = [NSString stringWithFormat:@"%@/%@", API_HOST_URL, _url];
}
if (![_url containsString:@"?"]) {
_url = [NSString stringWithFormat:@"%@?platform=%@", _url, [YYUtility appName]];
} else {
_url = [NSString stringWithFormat:@"%@&platform=%@", _url, [YYUtility appName]];
}
// urlString
NSString *noSpaceTextUrl = [_url stringByReplacingOccurrencesOfString:@" " withString:@""];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:noSpaceTextUrl]];
[self.webview loadRequest:request];
}
- (void)backButtonClick {
BOOL canGoBack = YES;
if (self.webview.backForwardList.backList.count <= 1) {
canGoBack = NO;
}
if ([self.webview canGoBack]) {
[self.webview goBack];
} else {
[self.navigationController popViewControllerAnimated:YES];
[self.userContentController removeAllUserScripts];
}
}
2021-11-24 14:31:24 +08:00
#pragma mark -
- (void)initNav:(NSDictionary *)response{
if(!response || ![response isKindOfClass:[NSDictionary class]])return;
self.shareDic = response[@"data"];
if ([response[@"type"] intValue]== RightNavigationPushType_Web) {
[self addNavigationItemWithTitles:@[response[@"data"][@"title"]] titleColor:[ThemeColor alertTitleColor] isLeft:NO target:self action:@selector(gotoWebView) tags:nil];
}else if ([response[@"type"] intValue]== RightNavigationPushType_Share || [response[@"type"] intValue]== RightNavigationPushType_SharePicture){
[self addNavigationItemWithImageNames:@[@"family_person_share"] isLeft:NO
target:self action:@selector(showSharePanel) tags:nil];
}
}
- (void)gotoWebView {
if (self.shareDic[@"link"]) {
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.url = self.shareDic[@"link"];
[self.navigationController pushViewController:webVC animated:YES];
}
}
2021-09-16 17:21:52 +08:00
2021-09-23 18:10:20 +08:00
- (void)showSharePanel {
2021-11-24 14:31:24 +08:00
if (self.shareDic.allKeys.count <= 0) {
return;
}
NSDictionary * dic = self.shareDic;
XPShareInfoModel * shareInfo = [[XPShareInfoModel alloc] init];
shareInfo.shareTitle = self.shareDic[@"title"];
shareInfo.shareContent = dic[@"desc"];
shareInfo.shareImageUrl = dic[@"imgUrl"];
shareInfo.type = ShareType_H5;
NSString *urlStr = ((NSString *)dic[@"url"]).length > 0 ? dic[@"url"] : dic[@"showUrl"];
if (urlStr.length) {
if ([urlStr containsString:@"?"]) {
urlStr = [NSString stringWithFormat:@"%@&shareUid=%@",urlStr,[AccountInfoStorage instance].getUid];
} else {
urlStr = [NSString stringWithFormat:@"%@?shareUid=%@",urlStr,[AccountInfoStorage instance].getUid];
}
}
shareInfo.shareUrl = urlStr;
2021-11-23 19:27:18 +08:00
XPShareItem *cycle = [XPShareItem itemWitTag:XPShareItemTagMoments title:@"朋友圈" imageName:@"share_wechat_circle_normal" disableImageName:@"share_wechat_circle_disable"];
2021-11-24 14:31:24 +08:00
XPShareItem *wechat = [XPShareItem itemWitTag:XPShareItemTagWeChat title:@"微信好友" imageName:@"share_wechat_normal" disableImageName:@"share_wechat_disable"];
XPShareItem *qq = [XPShareItem itemWitTag:XPShareItemTagQQ title:@"QQ好友" imageName:@"share_qq_normal" disableImageName:@"share_qq_disable"];
XPShareItem *qqzone = [XPShareItem itemWitTag:XPShareItemTagQQZone title:@"QQ空间" imageName:@"share_qqzone_normal" disableImageName:@"share_qqzone_disable"];
2021-11-23 19:27:18 +08:00
NSArray * items = @[wechat,cycle, qq, qqzone];
CGFloat margin = 15;
CGSize itemSize = CGSizeMake((KScreenWidth-2*margin)/4, 65);
XPShareView *shareView = [[XPShareView alloc] initWithItems:items itemSize:itemSize shareInfo:shareInfo];
shareView.delegate = self;
[TTPopup popupView:shareView style:TTPopupStyleActionSheet];
2021-09-23 18:10:20 +08:00
}
2021-11-23 19:27:18 +08:00
#pragma mark - XCShareViewDelegate
- (void)shareViewDidClickCancle:(XPShareView *)shareView {
2021-11-24 14:31:24 +08:00
[TTPopup dismiss];
}
- (void)shareView:(XPShareView *)shareView didSuccess:(XPShareInfoModel *)shareInfo {
[TTPopup dismiss];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
NSString *uid = [AccountInfoStorage instance].getUid;
NSString *ticket = [AccountInfoStorage instance].getTicket;
[params safeSetObject:uid forKey:@"uid"];
[params safeSetObject:@(shareInfo.shareType) forKey:@"shareType"];
[params safeSetObject:ticket forKey:@"ticket"];
[params safeSetObject:@(shareInfo.type) forKey:@"sharePageId"];
2021-11-24 14:31:24 +08:00
if (shareInfo.shareUrl.length > 0) {
[params safeSetObject:shareInfo.shareUrl forKey:@"shareUrl"];
2021-11-24 14:31:24 +08:00
}
if (shareInfo.roomUid > 0) {
[params safeSetObject:@(shareInfo.roomUid) forKey:@"targetUid"];
2021-11-24 14:31:24 +08:00
}
[HttpRequestHelper POST:@"usershare/save" params:params success:^(BaseModel * _Nonnull data) {
} failure:^(NSInteger resCode, NSString * _Nonnull message) {
}];
}
- (void)shareView:(XPShareView *)shareView shareFail:(NSString *)message {
[TTPopup dismiss];
[self showErrorToast:message];
2021-09-23 18:10:20 +08:00
}
- (void)shareViewDidClickCancel:(XPShareView *)shareView {
[TTPopup dismiss];
}
2021-09-16 17:21:52 +08:00
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progressView.progress = self.webview.estimatedProgress;
if (self.progressView.progress == 1) {
__weak typeof (self)weakSelf = self;
[UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
} completion:^(BOOL finished) {
weakSelf.progressView.hidden = YES;
}];
}
} else if ([keyPath isEqualToString:@"title"]) {
if (object == self.webview) {
self.navigationItem.title = self.webview.title;
if (self.urlLoadCompleted) {//webview
self.urlLoadCompleted(YES, nil);
}
2021-09-16 17:21:52 +08:00
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc {
[self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
[self.webview removeObserver:self forKeyPath:@"title"];
}
- (WKWebView *)webview {
if (_webview == nil) {
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
if (@available(iOS 10.0, *)) {
configuration.mediaTypesRequiringUserActionForPlayback = NO;
} else {
// Fallback on earlier versions
}
configuration.allowsInlineMediaPlayback = YES;
NSString *uid = [[AccountInfoStorage instance] getUid];
NSString *realCookie = [NSString stringWithFormat:@"%@=%@",@"uid",uid];
WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource: [NSString stringWithFormat:@"document.cookie = '%@';", realCookie] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[self.userContentController addUserScript:cookieScript];
2022-08-08 17:36:45 +08:00
///
NSString *scaleJs = @"$('meta[name=description]').remove(); $('head').append( '<meta name=\"viewport\" content=\"width=device-width, initial-scale=1,user-scalable=no\">' );";
WKUserScript *scaleScript = [[WKUserScript alloc] initWithSource:scaleJs injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
[self.userContentController addUserScript:scaleScript];
2021-09-16 17:21:52 +08:00
//WKUserScriptWKWebViewConfiguration
configuration.preferences.javaScriptEnabled = YES;
configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
configuration.preferences.minimumFontSize = 10;
configuration.selectionGranularity = WKSelectionGranularityCharacter;
configuration.userContentController = self.userContentController;
CGSize size = [UIScreen mainScreen].bounds.size;
_webview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, size.width,size.height) configuration:configuration];
_webview.navigationDelegate = self;
//KVOWKWebViewestimatedProgress
[_webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
//KVOtitle
[_webview addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(backButtonClick)];
[_webview addGestureRecognizer:swipeGesture];
[_webview.scrollView setShowsVerticalScrollIndicator:NO];
[_webview.scrollView setShowsHorizontalScrollIndicator:NO];
//set useragent
__weak typeof(self) weakSelf = self;
[_webview evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
NSString *userAgent = result;
if (![userAgent containsString:@"tutuAppIos erbanAppIos"]){
NSString *newUserAgent = [userAgent stringByAppendingString:@" tutuAppIos erbanAppIos"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];
[weakSelf.webview setCustomUserAgent:newUserAgent];
}
}];
2022-04-19 19:12:26 +08:00
_webview.scrollView.bounces = NO;
2021-09-16 17:21:52 +08:00
}
return _webview;
}
- (UIProgressView *)progressView{
if (!_progressView) {
_progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)];
_progressView.progressTintColor = [ThemeColor appMainColor];
_progressView.trackTintColor = [UIColor clearColor];
_progressView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
}
return _progressView;
}
- (WKUserContentController *)userContentController{
if (!_userContentController) {
//WKScriptMessageHandler
WeakWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[WeakWebViewScriptMessageDelegate alloc] initWithDelegate:self];
_userContentController = [[WKUserContentController alloc] init];
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenSharePage];
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenPurse];
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenChargePage];
// uid
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetUid];
// id
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetDeviceId];
// Ticket
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetTicket];
// info
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetDeviceInfo];
2021-09-23 14:52:12 +08:00
// :
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenFaceLiveness];
2021-11-24 14:31:24 +08:00
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSInitNav];
2021-12-23 14:44:08 +08:00
// uid
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetRoomUid];
2022-01-14 11:38:09 +08:00
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kOpenPersonPage];
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kInitShowNav];
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kCloseWebView];
2022-08-04 11:42:49 +08:00
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJumpAppointPage];
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenRoom];
2022-08-04 16:31:27 +08:00
//
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSOpenRoomForGiftId];
2022-10-31 15:56:46 +08:00
///
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kProhibitRightSlip];
2023-01-03 15:48:07 +08:00
///
[_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kSaveBase64Image];
2021-09-16 17:21:52 +08:00
}
return _userContentController;
}
@end