Combo Game

iOS 对接

在加载游戏H5的WebView中添加Js Bridge以供游戏调用

说明

iOS 需实现两个接口以供游戏调用

webkit.messageHandlers.recharge.postMessage(null); //余额不足,App弹出充值弹窗
webkit.messageHandlers.quit.postMessage(null); //用户退出游戏,App关闭WebView

同时,游戏提供一个接口以供 App 调用

window.rechargeSuccess(); //让用户在App完成充值时,App通知游戏刷新余额

添加 Message Handler

ViewController.m
//仅为样例,可根据情况自行修改

- (void) viewDidLoad
{
    WKUserContentController* wcc = [[WKUserContentController alloc] init];
    //绑定方法名
    [wcc addScriptMessageHandler:self name:@"recharge"];
    [wcc addScriptMessageHandler:self name:@"quit"];
    WKWebViewConfiguration *config = [WKWebViewConfiguration new];
    config.allowsInlineMediaPlayback = YES;
    [config setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
    //音视频的播放不需要用户手势触发, 即为自动播放
    config.mediaTypesRequiringUserActionForPlayback = NO;
    config.preferences = [[WKPreferences alloc]init];
    WKPreferences *preferences = [WKPreferences new];
    preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.preferences = preferences;
    config.preferences.javaScriptEnabled = YES;
    config.userContentController = wcc;

    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];
    [self.webview.scrollView setBackgroundColor:[UIColor clearColor]];
    [self.webview setBackgroundColor:[UIColor clearColor]];
    [self.webview setUIDelegate:self];
    //设置网页透明
    [self.webview setOpaque:NO];
}

- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.name isEqualToString:@"recharge"]) {
        [self showRechargeDialog];
    } else if ([message.name isEqualToString:@"quit"]) {
        [self quitGame];
    }
}

- (void) quitGame
{
    //TODO: 用户退出游戏,关闭游戏页面
}

- (void) showRechargeDialog
{
    //TODO: 弹出充值弹窗
}

//当用户完成充值时调用,通知游戏刷新余额
- (void) notifyRechargeSuccess
{
    [self.webview evaluateJavaScript:@"window.rechargeSuccess()" completionHandler:^(id _Nullable resp, NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"error = %@ , response = %@", error, resp);
        }
    }];
}

加载游戏 Zip 包 (可选)

仅需要通过 zip 方式接入的商户需要进行此项步骤,通过 H5 接入的商户无需进行此部分操作。

#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <SSZipArchive/SSZipArchive.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    WKWebView *webview = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [webview.configuration.preferences setValue:@(YES) forKey:@"allowFileAccessFromFileURLs"];
    [self.view addSubview:webview];

    NSString *filepath = @"/xxx/TeenPatti-1.0.zip"; //此处替换为下载完成的实际zip文件路径
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString *basePath = [NSString stringWithFormat: @"%@/TeenPatti", documentPath]; //此处可修改为其他路径或者其他文件夹命名
    NSError *error;
    [SSZipArchive unzipFileAtPath:filepath toDestination:basePath overwrite:YES password:nil error:&error];
    if (error != nil) {
        NSLog(@"Error %@ %@ %@", filepath, basePath, error);
    } else {
        NSURL *baseUrl = [NSURL fileURLWithPath:basePath isDirectory:YES];
        [webview loadFileURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/web-mobile/index.html%@", basePath, @"?appId=xxx&userId=xxx&token=xxx"]] allowingReadAccessToURL:baseUrl]; //此处query参数需替换为实际参数
    }
}

@end