// // AppDelegate.swift // 武极天下 // // Created by zhl on 2021/7/21. // Copyright © 2021 egret. All rights reserved. // import UIKit import Foundation import StoreKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, DYFStoreAppStorePaymentDelegate { var window: UIWindow? var _viewController: UIViewController? var _imageView: UIImageView? var _native: EgretNativeIOS = EgretNativeIOS.init() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let gameUrl = "http://local/game/index.html" _native = EgretNativeIOS.init(); _native.config.showFPS = false; _native.config.fpsLogTime = 30; _native.config.disableNativeRender = false; _native.config.clearCache = false; _native.config.useCutout = false; _native.config.enableGLBatch = true; UIApplication.shared.isIdleTimerDisabled = true; _viewController = ViewController.init(eaglView: _native.createEAGLView()); if (!(_native.initWith(_viewController))) { return false; } self.setExternalInterfaces() // Wether to allow the logs output to console. DYFStore.default.enableLog = true // If more than one transaction observer is attached to the payment queue, no guarantees are made as to the order they will be called in. It is recommended that you use a single observer to process and finish the transaction. DYFStore.default.addPaymentTransactionObserver() // Sets the delegate processes the purchase which was initiated by user from the App Store. DYFStore.default.delegate = self let networkState:String? = _native.getNetworkState(); if (networkState == "NotReachable") { print(">>>>>>>>>>>>>>>>>>>>no network") let native = _native; _native.startGame(gameUrl); self.showLoadingView(); _native.setNetworkStatusChangeCallback({ (state) in if (state != "NotReachable") { DispatchQueue.main.async { native.startGame(gameUrl) self.showLoadingView(); } } }) return true; } _native.startGame(gameUrl); self.showLoadingView(); return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } func setExternalInterfaces() { let support = _native; _native.setExternalInterface("sendToNative", callback: { (message) -> () in var str: String = ""; str.append(message!); support.callExternalInterface("sendToJS", value: str); }) _native.setExternalInterface("@onState", callback: { (message) -> () in print("Get @onStage: %s", message as Any) }) _native.setExternalInterface("@onError", callback: { (message) -> () in print("Get @onError: %s", message as Any) }) _native.setExternalInterface("@onJSError", callback: { (message) -> () in print("Get @onJSError: %s", message as Any) }) _native.setExternalInterface("@egretGameStarted", callback: { (message) -> () in print("Get @egretGameStarted: %s", message as Any) }) _native.setExternalInterface("getUid", callback: { (message) -> () in print("Get @getUid: %s", message as Any) let id = IDUtil.getUid(); let data = String(format:"{\"openid\":\"\", \"token\":\"%@\"}", arguments:[id]); support.callExternalInterface("sendUidToJS", value: data); }) _native.setExternalInterface("removeNativeLoading", callback: { (message) -> () in print("Get @removeNativeLoading: %s", message as Any) self.hideLoadingView(); }) _native.setExternalInterface("hideLoadImg", callback: { (message) -> () in print("Get @hideLoadImg: %s", message as Any) self.hideLoadingView(); }) } func showLoadingView() { let image = UIImage(named: "m_loading.jpg"); _imageView = UIImageView(image: image); _imageView?.contentMode = UIView.ContentMode.scaleAspectFill _imageView?.frame = _viewController!.view.frame; _viewController!.view.addSubview(_imageView!); _viewController!.view.bringSubviewToFront(_imageView!); self.showLoading("加载中") } func hideLoadingView(){ _imageView?.removeFromSuperview(); self.hideLoading() } // 处理用户从应用商店发起的购买 func didReceiveAppStorePurchaseRequest(_ queue: SKPaymentQueue, payment: SKPayment, forProduct product: SKProduct) { if !DYFStore.canMakePayments() { self.showTipsMessage("Your device is not able or allowed to make payments!") return } // Get account name from your own user system. let accountName = "Handsome Jon" // This algorithm is negotiated with server developer. let userIdentifier = Z_SHA256_HashValue(accountName) ?? "" DYFStoreLog("userIdentifier: \(userIdentifier)") ZStoreManager.shared.addPayment(product.productIdentifier, userIdentifier: userIdentifier) } }