app内置开机小游戏

像很多app都是在升级新版本后,一进入首页就有个类似抢红包的小游戏。

之前我们公司为了新版本发布会,希望内置一个开机抢金币的小游戏,现在就记录一下实现的过程。

首先 内置h5网页游戏 需要前端开发将做好的游戏打包成一个包,我们直接将游戏包拖到我们的项目中:
这里写图片描述
接下来就是需要获取网页游戏资源并加载到网页上
比如在首页控制器View将要展示的时候加载网页游戏

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    //游戏资源路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index.html" ofType:nil inDirectory:@"SimpleGame"];
    //根据路径找到请求链接
    NSURL *url = [NSURL fileURLWithPath:filePath];
    //请求链接拼接用户的token
    NSString *urlStr = [url.absoluteString stringByAppendingFormat:@"?token=%@",account.token];
    QCGameVC *simpleGameVC = [[QCGameVC alloc] init];
    simpleGameVC.urlStr = urlStr;
    [self.navigationController pushViewController:simpleGameVC animated:YES];

}

由于产品经理要求,在实际开发中,每个用户一天只能玩一次开机小游戏,所以在viewWillAppear 中要判断用户是否已经登录了,根据请求后台的字段判断该用户是否已经玩过游戏了,如果用户已经登录并没有玩过游戏就让用户进入玩开机小游戏,否则则不会进入玩开机小游戏,玩了小游戏的人 需要记录下来,这样当天下次再进入就不会再进入玩小游戏了。

例子中:QCGameVC控制器代码如下:

#import <UIKit/UIKit.h>

@interface QCGameVC : UIViewController<UIWebViewDelegate>

@property (nonatomic,copy) NSString *urlStr;
@property (nonatomic,strong) UIWebView *webView;

@end

QCGameVC.m实现如下:

#import "QCGameVC.h"

@interface QCGameVC ()<UIAlertViewDelegate>

@end

@implementation QCGameVC

- (void)loadView
{
    [super loadView];
    [self.view addSubview:self.webView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    if (!self.urlStr) {
        NSLog(@"网页请求链接为空");
        return;
    }

    NSURL *url = [[NSURL alloc] initWithString:self.urlStr];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:0 timeoutInterval:10];
    [self.webView loadRequest:request];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIBarButtonItem *backItem = [UIBarButtonItem backItemWithTitle:@"返回" target:self action:@selector(backItemAction)];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)backItemAction
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"退出游戏" message:@"您确定要退出游戏吗?" delegate:self cancelButtonTitle:@"继续玩" otherButtonTitles:@"退出", nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == alertView.firstOtherButtonIndex) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        _webView.delegate = self;
    }
    return _webView;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值