LSGGameActivityViewController.m 4.57 KB
//
//  LSGGameActivityViewController.m
//  GameActivitySDK
//
//  Created by yanglele on 2018/6/11.
//  Copyright © 2018年 alexYang. All rights reserved.
//

#import "LSGGameActivityViewController.h"
#import "LetsGameActivityAPI.h"
#import "LSGColor.h"
#import "LSGUrl.h"
#import "LSGMacros.h"

@interface LSGGameActivityViewController ()<UIWebViewDelegate>
@property(nonatomic, strong) UIWebView *webView;
@end

@implementation LSGGameActivityViewController

-(void)loadView{
    [super loadView];
    [self.navigationController setNavigationBarHidden:YES];
    self.view.backgroundColor = [LSGColor colorWithHexValue:0x181818 alpha:0.8];
    [self.view addSubview:self.webView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString *newUserAgent = @"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1";
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent}];
    
    NSString  *webViewUrlPath = [decideIsDebugOrRelease(self.isDebug) stringByAppendingString:kActivityAwardURL];
    NSMutableString *urlWithParams = [NSMutableString stringWithString:webViewUrlPath];
    
    //appId=100&appKey=f899139df5e1059396431415e770c6dd&userId=123456&serverId=111&serverName=aaa&roleId=222&roleName=ccc&diamond=888
    NSMutableString *queryString = [NSMutableString new];
    [queryString appendFormat:@"appId=%@",self.appId];
    [queryString appendFormat:@"&appKey=%@", self.appKey];
    [queryString appendFormat:@"&userId=%@", self.userId];
    [queryString appendFormat:@"&serverId=%@", self.serverId];
    [queryString appendFormat:@"&serverName=%@", self.serverName];
    [queryString appendFormat:@"&roleId=%@", self.roleId];
    [queryString appendFormat:@"&roleName=%@", self.roleName];
    [queryString appendFormat:@"&diamond=%@", self.diamond];
    
    [urlWithParams appendFormat:@"?%@", queryString];
    
    NSString *urlEncode = [urlWithParams stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlEncode];
    NSLog(@"activity Url %@", [url absoluteString]);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

-(UIWebView*)webView{
    if(!_webView){
        _webView = [[UIWebView alloc] init];
        _webView.delegate = self;
    }
    return _webView;
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self layoutSubViews];
}

-(void)layoutSubViews{
    if (iPhoneX) {
        _webView.frame = CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-44-34);
    }else{
        _webView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    }
}

-(void)injectJSObject{
    NSString *jsPath = [[NSBundle mainBundle].resourcePath stringByAppendingFormat:@"/%@/ActJs.txt", kBundleName];
    NSString *jsStr = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:nil];
    [self.webView stringByEvaluatingJavaScriptFromString:jsStr];
    
}

#pragma mark webView Delegate
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [self injectJSObject];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSURL *url = [request URL];
    
    NSString *urlStr = [url absoluteString];
    if([urlStr hasPrefix:@"jscall"]){
        NSArray *components = [urlStr componentsSeparatedByString:@":"];
        
        NSString *function = (NSString*)[components objectAtIndex:1];
        //调用oc方法,忽略警告
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        NSString *funcSig = [NSString stringWithString:function];
        NSLog(@"select funcSig %@", funcSig);
        
        SEL selector = NSSelectorFromString(funcSig);
        if ([self respondsToSelector:selector]) {
            [self performSelector:selector];
        }
        return NO;
    }else{
        return YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma js function
-(void)closeWin{
    //    [[LetsGameAPI instance] hide];
    self.accomplistCallBack();
    //当成功完成时返回是1(包括支付成功和支付失败),其他的情况都是没有值
//    if ([args[0] isEqualToString:@"1"]) {
//        //产生回调
//        self.accomplistCallBack();
//    }
    //其余的关闭窗口
    [[LetsGameActivityAPI instance] hide];
}

@end