@@ -53,6 +53,8 @@ NSString * const kMSGameLoaded = @"gameLoaded";
@ property ( nonatomic , weak ) id < RoomHostDelegate > hostDelegate ;
@ property ( nonatomic , strong ) ActivityInfoModel * gameModel ;
@ property ( nonatomic , strong ) UIButton * backBtn ;
@ property ( nonatomic , assign ) BOOL isBaiShun ;
@ property ( nonatomic , assign ) BOOL isCharing ;
@ end
@ implementation MSRoomGameWebVC
@@ -63,6 +65,7 @@ NSString * const kMSGameLoaded = @"gameLoaded";
if ( self ) {
self . hostDelegate = delegate ;
self . gameModel = gameModel ;
self . isBaiShun = [ gameModel . code . uppercaseString isEqualToString : @ "BAISHUN" ] ;
}
return self ;
}
@@ -77,12 +80,19 @@ NSString * const kMSGameLoaded = @"gameLoaded";
[ self showLoading ] ;
}
- ( void ) viewWillAppear : ( BOOL ) animated {
[ super viewWillAppear : animated ] ;
if ( ! self . isBaiShun &&
self . isCharing ) {
self . isCharing = NO ;
[ self updateCoin ] ;
}
}
- ( void ) setupBackButton {
self . backBtn = [ UIButton new ] ;
[ self . view addSubview : self . backBtn ] ;
[ self . backBtn mas_makeConstraints : ^ ( MASConstraintMaker * make ) {
make . edges . equalTo ( self . view ) ;
} ] ;
self . backBtn . frame = CGRectMake ( 0 , 0 , KScreenWidth , KScreenHeight ) ;
[ self . backBtn addTarget : self action : @ selector ( backBtnAction ) forControlEvents : UIControlEventTouchUpInside ] ;
}
@@ -95,25 +105,39 @@ NSString * const kMSGameLoaded = @"gameLoaded";
[ _ms _userContentController addScriptMessageHandler : weakScriptMessageDelegate name : kMSGameRecharge ] ;
[ _ms _userContentController addScriptMessageHandler : weakScriptMessageDelegate name : kMSGameLoaded ] ;
// / LEADERCC
[ _ms _userContentController addScriptMessageHandler : weakScriptMessageDelegate name : @ "closeGame" ] ;
[ _ms _userContentController addScriptMessageHandler : weakScriptMessageDelegate name : @ "pay" ] ;
WKWebViewConfiguration * config = [ WKWebViewConfiguration new ] ;
config . allowsInlineMediaPlayback = YES ;
[ config setValue : @ YES forKey : @ "allowUniversalAccessFromFileURLs" ] ;
// ⾳ 视 频 的 播 放 不 需 要 ⽤ ⼾ ⼿ 势 触 发 , 即 为 ⾃ 动 播 放
config . mediaTypesRequiringUserActionForPlayback = NO ;
config . mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone ;
config . preferences = [ [ WKPreferences alloc ] init ] ;
WKPreferences * preferences = [ WKPreferences new ] ;
preferences . javaScriptCanOpenWindowsAutomatically = YES ;
config . preferences = preferences ;
config . preferences . javaScriptEnabled = YES ;
config . userContentController = _ms _userContentController ;
// 获 取 当 前 屏 幕 的 宽 ⾼
int ScreenHeight = [ [ UIScreen mainScreen ] bounds ] . size . height ;
int ScreenWidth = [ [ UIScreen mainScreen ] bounds ] . size . width ;
self . webView = [ [ WKWebView alloc ] initWithFrame : CGRectMake ( 0 , 0 ,
ScreenWidth , ScreenHeight ) configuration : config ] ;
CGRect frame = CGRectZero ;
if ( self . isBaiShun ) {
frame = CGRectMake ( 0 , 0 ,
KScreenWidth , KScreenHeight ) ;
} else {
if ( self . gameModel . showType = = ActivityShowType_Half ) {
frame = CGRectMake ( 0 , KScreenHeight * 0.3 ,
KScreenWidth , KScreenHeight * 0.7 ) ;
} else {
frame = CGRectMake ( 0 , 0 ,
KScreenWidth , KScreenHeight ) ;
}
}
self . webView = [ [ WKWebView alloc ] initWithFrame : frame
configuration : config ] ;
[ self . webView . scrollView setBackgroundColor : [ UIColor clearColor ] ] ;
[ self . webView setBackgroundColor : [ UIColor clearColor ] ] ;
[ self . webView setUIDelegate : self ] ;
@@ -122,19 +146,39 @@ NSString * const kMSGameLoaded = @"gameLoaded";
[ self . webView setOpaque : NO ] ;
// 设 置 ⽹ ⻚ 全 屏
if ( @ available ( iOS 11.0 , * ) ) {
self . webView . scrollView . contentInsetAdjustmentBehavior =
UIScrollViewContentInsetAdjustmentNever ;
self . webView . scrollView . contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever ;
}
[ self . view addSubview : self . webView ] ;
NSString * h5Url = self . gameModel . skipContent ;
NSURL * url = [ NSURL URLWithString : h5Url ] ;
NSURL * url = [ self handleGameURL : h5Url ] ;
NSURLRequest * request = [ [ NSURLRequest alloc ] initWithURL : url ] ;
[ self . webView loadRequest : request ] ;
}
- ( NSURL * ) handleGameURL : ( NSString * ) url {
if ( self . isBaiShun ) {
return [ NSURL URLWithString : url ] ;
} else {
url = [ url stringByAppendingFormat : @ "&uid=%@" , [ AccountInfoStorage instance ] . getUid ] ;
url = [ url stringByAppendingFormat : @ "&token=%@" , self . gameModel . gameModel . code ] ;
url = [ url stringByAppendingFormat : @ "&lang=%@" , @ "zh-CN" ] ;
url = [ url stringByAppendingFormat : @ "&roomid=%ld" , ( long ) self . hostDelegate . getRoomInfo . uid ] ;
return [ NSURL URLWithString : url ] ;
}
return nil ;
}
- ( void ) webView : ( WKWebView * ) webView didFinishNavigation : ( WKNavigation * ) navigation {
[ self hideHUD ] ;
if ( self . isBaiShun ) {
} else {
self . backBtn . frame = CGRectMake ( 0 , 0 , KScreenWidth , KScreenHeight * 0.3 ) ;
}
# if DEBUG
NSString * fileName = @ "vconsole.min.js" ; // 你 要 查 找 的 文 件 名
NSString * directory = [ [ NSBundle mainBundle ] resourcePath ] ;
@@ -208,7 +252,6 @@ NSString * const kMSGameLoaded = @"gameLoaded";
NSData * jsonData = [ jsonString dataUsingEncoding : NSUTF8StringEncoding ] ;
NSError * err ;
NSDictionary * dic = [ NSJSONSerialization JSONObjectWithData : jsonData
options : NSJSONReadingMutableContainers
error : & err ] ;
if ( err )
@@ -219,25 +262,81 @@ NSString * const kMSGameLoaded = @"gameLoaded";
return dic ;
}
// 绑 定 协 议 ⽅ 法
- ( void ) userContentController : ( WKUserContentController
* ) userContentController
- ( void ) userContentController : ( WKUserContentController * ) userContentController
didReceiveScriptMessage : ( WKScriptMessage * ) message
{
// NSString * method = [ NSString stringWithFormat : @ "%@:" , message . name ] ;
NSDictionary * dicBody = [ self dictionaryWithJsonString : message . body ] ;
if ( [ message . name isEqualToString : kMSGetConfig ] ) {
[ self getConfig : dicBody ] ;
} else if ( [ message . name isEqualToString : kMSDestroy ] ) {
[ self destroy : dicBody ] ;
} else if ( [ message . name isEqualToString : kMSGameLoaded ] ) {
[ self gameLoaded : dicBody ] ;
} else if ( [ message . name isEqualToString : kMSGameRecharge ] ) {
[ self gameRecharge : dicBody ] ;
// / BaiShun
if ( self . isBaiShun ) {
NSDictionary * dicBody = [ self dictionaryWithJsonString : message . body ] ;
if ( [ message . name isEqualToString : kMSGetConfig ] ) {
[ self getConfig : dicBody ] ;
} else if ( [ message . name isEqualToString : kMSDestroy ] ) {
[ self destroy : dicBody ] ;
} else if ( [ message . name isEqualToString : kMSGameLoaded ] ) {
[ self gameLoaded : dicBody ] ;
} else if ( [ message . name isEqualToString : kMSGameRecharge ] ) {
[ self gameRecharge : dicBody ] ;
} else {
NSLog ( @ "未实现⽅法 : %@ --> %@" , message . name , message . body ) ;
}
} else {
NSLog ( @ "未实现⽅法 : %@ --> %@" , message . name , message . body ) ;
// / LeaderCC
NSString * method = [ NSString stringWithFormat : @ "%@:" , message . name ] ;
SEL selector = NSSelectorFromString ( method ) ;
if ( [ self respondsToSelector : selector ] ) {
[ self performSelector : selector withObject : message . body ] ;
} else {
NSLog ( @ "未實現方法 : %@ --> %@" , message . name , message . body ) ;
}
}
}
# pragma mark - LeaderCC Delegate
// 关 闭 游 戏
- ( void ) closeGame : ( NSDictionary * ) args {
// NSLog ( @ "1234" ) ;
TTAlertConfig * config = [ [ TTAlertConfig alloc ] init ] ;
config . message = YMLocalizedString ( @ "MSRoomGameWebVC0" ) ;
config . actionStyle = TTAlertActionBothStyle ;
[ TTPopup alertWithConfig : config showBorder : NO confirmHandler : ^ {
[ self willMoveToParentViewController : nil ] ; // 1
[ self . view removeFromSuperview ] ; // 2
[ self removeFromParentViewController ] ; // 3
} cancelHandler : ^ {
} ] ;
}
// 显 示 充 值 界 面
- ( void ) pay : ( NSDictionary * ) args {
// 拉 起 充 值 商 城 TODO 客 ⼾ 端
TTAlertConfig * config = [ [ TTAlertConfig alloc ] init ] ;
config . message = YMLocalizedString ( @ "XPNobleCenterViewController3" ) ;
config . actionStyle = TTAlertActionBothStyle ;
@ kWeakify ( self ) ;
[ TTPopup alertWithConfig : config showBorder : NO confirmHandler : ^ {
@ kStrongify ( self ) ;
XPIAPRechargeViewController * webVC = [ [ XPIAPRechargeViewController alloc ] init ] ;
webVC . type = @ "4" ;
[ self . navigationController pushViewController : webVC animated : YES ] ;
self . isCharing = YES ;
} cancelHandler : ^ {
} ] ;
}
- ( void ) updateCoin {
// 平 时 游 戏 币 更 新 不 需 要 调 用 该 方 法 , 以 防 止 影 响 游 戏 开 奖 动 画 。
[ self . webView evaluateJavaScript : @ "updateCoin()" completionHandler : ^ ( id _Nullable result , NSError * _Nullable error ) {
if ( error ) {
NSLog ( @ "Error calling JS function: %@" , error . localizedDescription ) ;
} else {
NSLog ( @ "JS function called successfully, result: %@" , result ) ;
}
} ] ;
}
# pragma mark - BaiShun Delegate
// 获 取 信 息 配 置
- ( void ) getConfig : ( NSDictionary * ) args
{
@@ -312,10 +411,13 @@ NSString * const kMSGameLoaded = @"gameLoaded";
TTAlertConfig * config = [ [ TTAlertConfig alloc ] init ] ;
config . message = YMLocalizedString ( @ "XPNobleCenterViewController3" ) ;
config . actionStyle = TTAlertActionBothStyle ;
@ kWeakify ( self ) ;
[ TTPopup alertWithConfig : config showBorder : NO confirmHandler : ^ {
@ kStrongify ( self ) ;
XPIAPRechargeViewController * webVC = [ [ XPIAPRechargeViewController alloc ] init ] ;
webVC . type = @ "4" ;
[ self . navigationController pushViewController : webVC animated : YES ] ;
self . isCharing = YES ;
} cancelHandler : ^ {
} ] ;