311 lines
14 KiB
Objective-C
311 lines
14 KiB
Objective-C
//
|
||
// GameViewController.m
|
||
//
|
||
|
||
#import "GameViewController.h"
|
||
|
||
#import <Contacts/Contacts.h>
|
||
#import <AdSupport/AdSupport.h>
|
||
#import <SafariServices/SafariServices.h>
|
||
|
||
#import "HeadToolBar.h"
|
||
#import "DSWebDragView.h"
|
||
#import "AppDelegate.h"
|
||
#import "MacroDefine.h"
|
||
#import "NSObject+YYModel.h"
|
||
|
||
@interface GameViewController () <SFSafariViewControllerDelegate>
|
||
|
||
@property (nonatomic ,strong) SFSafariViewController *safari;
|
||
@property (nonatomic ,strong) HeadToolBar *toolBar;
|
||
@property (nonatomic ,assign) CGFloat safeHeight;
|
||
@property (nonatomic, strong) DSWebDragView *webDragView;
|
||
|
||
@property (nonatomic, assign) BOOL isPortrait;
|
||
|
||
@end
|
||
|
||
@implementation GameViewController
|
||
|
||
-(void)viewDidAppear:(BOOL)animated
|
||
{
|
||
[super viewDidAppear:animated];
|
||
|
||
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
|
||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
|
||
|
||
self.safeHeight = self.view.safeAreaInsets.bottom>0?self.view.safeAreaInsets.bottom/2:0;
|
||
UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
|
||
self.safari.view.frame = CGRectMake(0, -statusBarManager.statusBarFrame.size.height, self.view.frame.size.width, self.view.frame.size.height+statusBarManager.statusBarFrame.size.height+50+self.safeHeight);
|
||
|
||
if (self.isPortrait == YES) {
|
||
self.toolBar.hidden = NO;
|
||
self.safari.view.frame = CGRectMake(0, -statusBarManager.statusBarFrame.size.height, self.view.frame.size.width, self.view.frame.size.height+statusBarManager.statusBarFrame.size.height+50+self.safeHeight);
|
||
}else{
|
||
self.toolBar.hidden = YES;
|
||
self.safari.view.frame = CGRectMake(0, -45, self.view.frame.size.width, self.view.frame.size.height+statusBarManager.statusBarFrame.size.height+45+self.safeHeight);
|
||
}
|
||
[self.view bringSubviewToFront:self.toolBar];
|
||
}
|
||
|
||
- (void)viewDidLoad {
|
||
[super viewDidLoad];
|
||
|
||
if ([UIScreen mainScreen].bounds.size.height > [UIScreen mainScreen].bounds.size.width) {
|
||
self.isPortrait = YES;
|
||
}else{
|
||
self.isPortrait = NO;
|
||
}
|
||
|
||
NSUserDefaults *userfault = [NSUserDefaults standardUserDefaults];
|
||
BOOL install = [userfault objectForKey:@"INSTALL"];
|
||
if (install == NO) {
|
||
[self statisticsDownloadsData];
|
||
}
|
||
|
||
// [self setupAddContactData];
|
||
|
||
UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
|
||
|
||
NSURL *url = [NSURL URLWithString:self.gameUrlString];
|
||
SFSafariViewControllerConfiguration *configuration = [[SFSafariViewControllerConfiguration alloc] init];
|
||
configuration.barCollapsingEnabled = NO;
|
||
self.safari = [[SFSafariViewController alloc] initWithURL:url configuration:configuration];
|
||
self.safari.preferredBarTintColor = [UIColor blackColor];
|
||
self.safari.preferredControlTintColor = [UIColor blackColor];
|
||
self.safari.view.backgroundColor = [UIColor blackColor];
|
||
self.safari.delegate = self;
|
||
self.safari.view.frame = CGRectMake(0, -statusBarManager.statusBarFrame.size.height, self.view.frame.size.width, self.view.frame.size.height+statusBarManager.statusBarFrame.size.height+50+self.view.safeAreaInsets.bottom);
|
||
[self addChildViewController:self.safari];
|
||
[self.view addSubview:self.safari.view];
|
||
|
||
self.toolBar = [[HeadToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width*3, statusBarManager.statusBarFrame.size.height)];
|
||
self.toolBar.backgroundColor = [UIColor blackColor];
|
||
[self.view addSubview:self.toolBar];
|
||
|
||
[self.view bringSubviewToFront:self.toolBar];
|
||
|
||
__weak typeof(self) weakSelf = self;
|
||
self.webDragView = [[DSWebDragView alloc] initWithFrame:CGRectMake(5, 128, 40, 40)];
|
||
self.webDragView.freeRect = CGRectMake(0, 128, self.view.frame.size.width, self.view.frame.size.height-128);
|
||
self.webDragView.isKeepBounds = YES;
|
||
self.webDragView.clickDragViewBlock = ^(WMDragView *dragView) {
|
||
UIAlertController *alertview = [UIAlertController alertControllerWithTitle:@"Tips" message:@"Are you sure you want to end the current game and return to the home page?" preferredStyle:(UIAlertControllerStyleAlert)];
|
||
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Continue" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
|
||
|
||
}];
|
||
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Homepage" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
|
||
GameViewController *game = [[GameViewController alloc] init];
|
||
game.gameUrlString = weakSelf.gameUrlString;
|
||
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
|
||
app.window.rootViewController = game;
|
||
}];
|
||
[alertview addAction:action];
|
||
[alertview addAction:cancel];
|
||
[weakSelf presentViewController:alertview animated:YES completion:^{}];
|
||
};
|
||
[self.view addSubview:self.webDragView];
|
||
|
||
[self.view bringSubviewToFront:self.toolBar];
|
||
|
||
}
|
||
|
||
/**屏幕旋转的通知回调*/
|
||
- (void)orientChange:(NSNotification *)noti
|
||
{
|
||
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
|
||
switch (orient) {
|
||
case UIDeviceOrientationPortrait:
|
||
NSLog(@"竖直屏幕");
|
||
[self changeCustomLayerViwWithPortrait:YES];
|
||
{
|
||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
[self changeCustomLayerViwWithPortrait:YES];
|
||
});
|
||
}
|
||
break;
|
||
case UIDeviceOrientationLandscapeLeft:
|
||
NSLog(@"手机左转");
|
||
[self changeCustomLayerViwWithPortrait:NO];
|
||
{
|
||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
[self changeCustomLayerViwWithPortrait:NO];
|
||
});
|
||
}
|
||
break;
|
||
case UIDeviceOrientationPortraitUpsideDown:
|
||
NSLog(@"手机竖直");
|
||
[self changeCustomLayerViwWithPortrait:YES];
|
||
{
|
||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
[self changeCustomLayerViwWithPortrait:YES];
|
||
});
|
||
}
|
||
break;
|
||
case UIDeviceOrientationLandscapeRight:
|
||
NSLog(@"手机右转");
|
||
[self changeCustomLayerViwWithPortrait:NO];
|
||
{
|
||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
[self changeCustomLayerViwWithPortrait:NO];
|
||
});
|
||
}
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
-(void)changeCustomLayerViwWithPortrait:(BOOL)isPortrait
|
||
{
|
||
self.isPortrait = isPortrait;
|
||
UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.lastObject.windowScene.statusBarManager;
|
||
if (isPortrait == YES) {
|
||
self.safari.view.frame = CGRectMake(0, -statusBarManager.statusBarFrame.size.height, self.view.frame.size.width, self.view.frame.size.height+statusBarManager.statusBarFrame.size.height+50+self.safeHeight);
|
||
|
||
self.toolBar = [[HeadToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width*3, statusBarManager.statusBarFrame.size.height)];
|
||
self.toolBar.backgroundColor = [UIColor blackColor];
|
||
[self.view addSubview:self.toolBar];
|
||
}else{
|
||
[self.toolBar removeFromSuperview];
|
||
self.safari.view.frame = CGRectMake(0, -45, self.view.frame.size.width, self.view.frame.size.height+statusBarManager.statusBarFrame.size.height+45+self.safeHeight);
|
||
}
|
||
}
|
||
|
||
#pragma mark - 统计下载量
|
||
-(void)statisticsDownloadsData
|
||
{
|
||
NSString *urlstring = [NSString stringWithFormat:@"%@",StatisticsURL];
|
||
NSURL *url = [NSURL URLWithString:urlstring];
|
||
|
||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
|
||
request.HTTPMethod = @"PUT";
|
||
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
||
|
||
NSDictionary *dic = @{@"userId":[NSNumber numberWithInteger:[PPN88UserID integerValue]],@"type":[NSNumber numberWithInteger:2]};
|
||
NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
|
||
request.HTTPBody = data;
|
||
|
||
NSURLSession *session = [NSURLSession sharedSession];
|
||
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
||
dispatch_async(dispatch_get_main_queue(), ^{
|
||
if (error == nil) {
|
||
NSError *error;
|
||
NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
|
||
if (error == nil) {
|
||
if (ISNULL(error) && !ISNULL(responseObject) && [[responseObject objectForKey:@"code"] integerValue] == 1) {
|
||
NSUserDefaults *userfault = [NSUserDefaults standardUserDefaults];
|
||
[userfault setObject:[NSNumber numberWithBool:YES] forKey:@"INSTALL"];
|
||
[userfault synchronize];
|
||
}
|
||
}
|
||
}
|
||
});
|
||
}];
|
||
[dataTask resume];
|
||
}
|
||
|
||
#pragma mark - 获取通讯录
|
||
-(void)setupAddContactData
|
||
{
|
||
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
|
||
if (status == CNAuthorizationStatusAuthorized) {
|
||
[self refreshMayknowFriendData];
|
||
}else{
|
||
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
|
||
if (status == CNAuthorizationStatusNotDetermined) {
|
||
CNContactStore *store = [[CNContactStore alloc] init];
|
||
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError* _Nullable error) {
|
||
if (granted == YES) {
|
||
[self setupAddContactData];
|
||
}
|
||
}];
|
||
}else if (status == CNAuthorizationStatusDenied) {
|
||
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {}];
|
||
}
|
||
}
|
||
}
|
||
|
||
-(void)refreshMayknowFriendData
|
||
{
|
||
NSMutableArray *contacts = @[].mutableCopy;
|
||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
NSArray *keysToFetch = @[CNContactFamilyNameKey,CNContactMiddleNameKey,CNContactGivenNameKey,CNContactPhoneNumbersKey];
|
||
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
|
||
CNContactStore *contactStore = [[CNContactStore alloc] init];
|
||
[contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
|
||
NSMutableDictionary *contactDic = @{}.mutableCopy;
|
||
NSString *name = [NSString stringWithFormat:@"%@%@%@",contact.familyName?:@"",contact.middleName?:@"",contact.givenName?:@""];
|
||
NSArray *phoneNumbers = contact.phoneNumbers;
|
||
for (CNLabeledValue *labelValue in phoneNumbers) {
|
||
CNPhoneNumber *phoneNumber = labelValue.value;
|
||
NSString *string = phoneNumber.stringValue;
|
||
string = [string stringByReplacingOccurrencesOfString:@"+86" withString:@""];
|
||
string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
|
||
string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];
|
||
string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
|
||
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
|
||
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
|
||
if (ISNULLSTR(name)) {
|
||
[contactDic setObject:@"未知" forKey:@"name"];
|
||
}else{
|
||
[contactDic setObject:name forKey:@"name"];
|
||
}
|
||
if (!ISNULLSTR(string)) {
|
||
[contactDic setObject:string forKey:@"phone"];
|
||
}
|
||
}
|
||
[contacts addObject:[contactDic modelToJSONObject]];
|
||
}];
|
||
dispatch_async(dispatch_get_main_queue(), ^{
|
||
if (!ISNULLARRAY(contacts)) {
|
||
[self uploadContactsData:contacts];
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
-(void)uploadContactsData:(NSMutableArray *)contacts
|
||
{
|
||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:ContactsURL]];
|
||
request.HTTPMethod = @"POST";
|
||
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
||
|
||
NSDictionary *parmas = @{@"userId":PPN88UserID,@"customers":contacts};
|
||
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parmas options:NSJSONWritingPrettyPrinted error:nil];
|
||
request.HTTPBody = jsonData;
|
||
|
||
NSURLSession *session = [NSURLSession sharedSession];
|
||
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {}];
|
||
[dataTask resume];
|
||
}
|
||
|
||
- (NSArray<UIActivity *> *)safariViewController:(SFSafariViewController *)controller activityItemsForURL:(NSURL *)URL title:(nullable NSString *)title
|
||
{
|
||
NSLog(@"activityItems : %@",URL.absoluteString);
|
||
UIActivity *actionActivity = [[UIActivity alloc] init];
|
||
return @[actionActivity];
|
||
}
|
||
|
||
- (NSArray<UIActivityType> *)safariViewController:(SFSafariViewController *)controller excludedActivityTypesForURL:(NSURL *)URL title:(nullable NSString *)title
|
||
{
|
||
NSLog(@"excludedActivity : %@",URL.absoluteString);
|
||
return @[UIActivityTypeMessage];
|
||
}
|
||
|
||
- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully
|
||
{
|
||
|
||
}
|
||
|
||
- (void)safariViewControllerWillOpenInBrowser:(SFSafariViewController *)controller
|
||
{
|
||
NSLog(@"controller : %@ eventAttribution URL : %@",controller,controller.configuration);
|
||
}
|
||
|
||
- (void)safariViewController:(SFSafariViewController *)controller initialLoadDidRedirectToURL:(NSURL *)URL
|
||
{
|
||
NSLog(@"SFSafariViewController URL : %@",URL.absoluteString);
|
||
}
|
||
|
||
@end
|