162 lines
6.7 KiB
Plaintext
162 lines
6.7 KiB
Plaintext
// UnityiOSBridge.mm
|
|||
|
|
#import <Foundation/Foundation.h>
|
||
|
|
#import <UIKit/UIKit.h>
|
||
|
|
//#import <AppTrackingTransparency/AppTrackingTransparency.h>
|
||
|
|
//#import <AdSupport/ASIdentifierManager.h>
|
||
|
|
#import <CoreTelephony/CTCellularData.h>
|
||
|
|
#import <SystemConfiguration/CaptiveNetwork.h>
|
||
|
|
#import "UnityInterface.h"
|
||
|
|
|
||
|
|
// ==================== IDFA ====================
|
||
|
|
// static NSString *cachedIDFA = @"";
|
||
|
|
|
||
|
|
// #ifdef __cplusplus
|
||
|
|
// extern "C" {
|
||
|
|
// #endif
|
||
|
|
|
||
|
|
// void RequestIDFAFromUnity() {
|
||
|
|
// if (@available(iOS 14, *)) {
|
||
|
|
// [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
|
||
|
|
// if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
|
||
|
|
// cachedIDFA = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
|
||
|
|
// } else {
|
||
|
|
// cachedIDFA = @"Permission Denied";
|
||
|
|
// }
|
||
|
|
// }];
|
||
|
|
// } else {
|
||
|
|
// if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
|
||
|
|
// cachedIDFA = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
|
||
|
|
// } else {
|
||
|
|
// cachedIDFA = @"Tracking Disabled";
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
// const char* GetCachedIDFA() {
|
||
|
|
// const char* utf8Str = [cachedIDFA UTF8String];
|
||
|
|
// if (utf8Str == nil) return NULL;
|
||
|
|
// char* result = (char*)malloc(strlen(utf8Str) + 1);
|
||
|
|
// strcpy(result, utf8Str);
|
||
|
|
// return result;
|
||
|
|
// }
|
||
|
|
|
||
|
|
// #ifdef __cplusplus
|
||
|
|
// }
|
||
|
|
// #endif
|
||
|
|
|
||
|
|
// ==================== iOS Version ====================
|
||
|
|
extern "C" {
|
||
|
|
char* _getVersion() {
|
||
|
|
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
|
||
|
|
NSString *build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
|
||
|
|
NSString *combinedVersion = [NSString stringWithFormat:@"%@.%@", version, build];
|
||
|
|
|
||
|
|
const char *cString = [combinedVersion UTF8String];
|
||
|
|
char *result = (char*)malloc(strlen(cString) + 1);
|
||
|
|
strcpy(result, cString);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
void _freeMemory(void *ptr) {
|
||
|
|
free(ptr);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ==================== Network Permission ====================
|
||
|
|
typedef void (*UnityCallback)(int status);
|
||
|
|
static CTCellularData *cellularData = nil;
|
||
|
|
static UnityCallback currentCallback = nil;
|
||
|
|
// 添加回调函数指针
|
||
|
|
typedef void (*UnityAlertCallback)(void);
|
||
|
|
|
||
|
|
extern "C" {
|
||
|
|
int CheckNetworkPermission() {
|
||
|
|
if (@available(iOS 9.0, *)) {
|
||
|
|
CTCellularDataRestrictedState state = cellularData.restrictedState;
|
||
|
|
switch (state) {
|
||
|
|
case kCTCellularDataRestricted:
|
||
|
|
return 3; // Restricted
|
||
|
|
case kCTCellularDataNotRestricted:
|
||
|
|
return 3; // Authorized
|
||
|
|
default:
|
||
|
|
return 3; // NotDetermined
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 3; // iOS < 9 default authorized
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequestNetworkPermission() {
|
||
|
|
if (@available(iOS 9.0, *)) {
|
||
|
|
if (!cellularData) {
|
||
|
|
cellularData = [[CTCellularData alloc] init];
|
||
|
|
}
|
||
|
|
|
||
|
|
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
|
||
|
|
if (state != kCTCellularDataRestrictedStateUnknown) {
|
||
|
|
int status = (state == kCTCellularDataNotRestricted) ? 3 : 1;
|
||
|
|
if (currentCallback) currentCallback(status);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
// 触发系统权限弹窗(通过发起网络请求)
|
||
|
|
NSURL *url = [NSURL URLWithString:@"https://www.apple.com"];
|
||
|
|
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url];
|
||
|
|
[task resume];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void SetPermissionCallback(UnityCallback callback) {
|
||
|
|
currentCallback = callback;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 显示权限被拒绝的弹窗
|
||
|
|
|
||
|
|
void ShowPermissionDeniedAlertWithCallback(UnityAlertCallback callback) {
|
||
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Network Permission Required"
|
||
|
|
message:@"This game requires a connection to the server for gameplay and display the ads, Please enable network access in Settings"
|
||
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
||
|
|
|
||
|
|
UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:@"Go to Settings"
|
||
|
|
style:UIAlertActionStyleDefault
|
||
|
|
handler:^(UIAlertAction * _Nonnull action) {
|
||
|
|
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
|
||
|
|
if (@available(iOS 10.0, *)) {
|
||
|
|
[[UIApplication sharedApplication] openURL:settingsURL options:@{} completionHandler:nil];
|
||
|
|
} else {
|
||
|
|
[[UIApplication sharedApplication] openURL:settingsURL];
|
||
|
|
}
|
||
|
|
if (callback) callback();
|
||
|
|
}];
|
||
|
|
|
||
|
|
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
|
||
|
|
style:UIAlertActionStyleCancel
|
||
|
|
handler:^(UIAlertAction * _Nonnull action) {
|
||
|
|
if (callback) callback();
|
||
|
|
}];
|
||
|
|
|
||
|
|
[alert addAction:settingsAction];
|
||
|
|
[alert addAction:cancelAction];
|
||
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification
|
||
|
|
object:nil
|
||
|
|
queue:[NSOperationQueue mainQueue]
|
||
|
|
usingBlock:^(NSNotification * _Nonnull note) {
|
||
|
|
[alert dismissViewControllerAnimated:NO completion:nil];
|
||
|
|
}];
|
||
|
|
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ==================== Screen Info ====================
|
||
|
|
extern "C" {
|
||
|
|
void GetScreenResolution(float* width, float* height) {
|
||
|
|
CGRect screenBounds = [UIScreen mainScreen].bounds;
|
||
|
|
*width = screenBounds.size.width;
|
||
|
|
*height = screenBounds.size.height;
|
||
|
|
}
|
||
|
|
|
||
|
|
float GetScaleFactor() {
|
||
|
|
return [UIScreen mainScreen].scale;
|
||
|
|
}
|
||
|
|
}
|