popcorn/Plugins/iOS/NativeUtils.mm

210 lines
6.4 KiB
Plaintext

//
// NativeUtils.mm
// Unity-iPhone
//
// Created by Suto Atsushi on 2016/06/05.
//
//
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
#import <Social/Social.h>
#import "UnityAppController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "NativeUtils.h"
#include <sys/sysctl.h>
#import <UserNotifications/UserNotifications.h>
@implementation NativeUtils
+(NSString *) charToNSString:(char *)value
{
if (value != NULL)
{
return [NSString stringWithUTF8String: value];
}
else
{
return [NSString stringWithUTF8String: ""];
}
}
static NativeUtils *_sharedInstance = nil;
+ (NativeUtils *) sharedInstance
{
if (!_sharedInstance)
{
_sharedInstance = [[NativeUtils alloc] init];
}
return _sharedInstance;
}
-(void) registerNotification
{
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:
(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert ) completionHandler:^(BOOL granted, NSError *_Nullable error) {
if (granted) {
}
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
-(void) scheduleNotification: (int) triggerInSeconds message: (NSString*) message alarmId:(NSString *)alarmId
{
// インスタンス生成
UNMutableNotificationContent *unMutableNotice = [UNMutableNotificationContent new];
unMutableNotice.body = message;
unMutableNotice.sound = [UNNotificationSound defaultSound];
UNTimeIntervalNotificationTrigger *triger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:triggerInSeconds repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:alarmId content:unMutableNotice trigger:triger];
[UNUserNotificationCenter.currentNotificationCenter addNotificationRequest:request withCompletionHandler:nil];
}
- (void) cancelAllNotifications
{
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
}
- (void) showAlert: (NSString*) title message: (NSString*) message
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// addActionした順に左から右にボタンが配置されます
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}]];
[[[[UIApplication sharedApplication]keyWindow] rootViewController] presentViewController:alertController animated:YES completion:nil];
}
@end
extern "C"{
int _getHeight()
{
CGRect cr = [[UIScreen mainScreen] nativeBounds];
return cr.size.height;
}
int _getWidth()
{
CGRect cr = [[UIScreen mainScreen] nativeBounds];
return cr.size.width;
}
int _getPointHeight()
{
CGRect cr = [[UIScreen mainScreen] bounds];
return cr.size.height;
}
int _getPointWidth()
{
CGRect cr = [[UIScreen mainScreen] bounds];
return cr.size.width;
}
bool _enableReviewWindow()
{
return [SKStoreReviewController class];
}
void _reviewWindow(char* appid)
{
if([SKStoreReviewController class]){
[SKStoreReviewController requestReview] ;
}else{
NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@&action=write-review",[NativeUtils charToNSString:appid]];
NSURL *URL = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:URL];
}
}
long _systemClockTime()
{
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
int rc = sysctl(mib, 2, &boottime, &size, NULL, 0);
if (rc != 0) {
return 0;
}
return (long)(time(NULL) - boottime.tv_sec);
}
bool _canOpenTwitter()
{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"twitter://"]])
{
return true;
}
return false;
}
bool _canOpenFacebook()
{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]])
{
return true;
}
return false;
}
void _registerNotification()
{
[[NativeUtils sharedInstance] registerNotification];
}
void _scheduleNotification (int triggerInSeconds, char* message, char* alarmId)
{
[[NativeUtils sharedInstance] scheduleNotification:triggerInSeconds message:[NativeUtils charToNSString:message] alarmId:[NativeUtils charToNSString:alarmId]];
}
void _cancelAllNotifications()
{
[[NativeUtils sharedInstance] cancelAllNotifications];
}
void _PlayVibration(int id)
{
AudioServicesPlaySystemSound(id);
}
void _PlayVibrationOnce(){
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:(UIImpactFeedbackStyleMedium)];
[generator impactOccurred];
}
void _PlayVibrationOnceStrong(){
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:(UIImpactFeedbackStyleHeavy)];
[generator impactOccurred];
}
void _PlayVibrationOnceWeak(){
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:(UIImpactFeedbackStyleLight)];
[generator impactOccurred];
}
void _PlayVibrationDoubleStrong(){
UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
[generator notificationOccurred: UINotificationFeedbackTypeSuccess];
}
void _PlayVibrationDoubleWeak(){
UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
[generator notificationOccurred: UINotificationFeedbackTypeWarning];
}
void _PlayVibrationTriple(){
UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
[generator notificationOccurred: UINotificationFeedbackTypeError];
}
void _showAlert(char* title, char* message)
{
[[NativeUtils sharedInstance] showAlert:[NativeUtils charToNSString:title] message:[NativeUtils charToNSString:message]];
}
}