jsdw_ios/QuickLocation/AppDelegate.swift

169 lines
5.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AppDelegate.swift
// QuickLocation
//
// Created by 林 on 2026/5/25.
//
import UIKit
import IQKeyboardManagerSwift
#if !targetEnvironment(simulator)
import GeYanSdk
import AMapFoundationKit
#endif
import UMCommon
import SwiftDate
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
private var didSetupPrivacyGrantedSDKs = false
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FontManager.registerCustomFonts()
ApiManager.shared.setup()
AppRouter.shared.setup()
AppUnlockCoordinator.shared.start()
if #available(iOS 16.0, *) {
AppRestrictManager.shared.startPermissionMonitoring()
}
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SwiftDate.defaultRegion = Region(zone: TimeZone.current, locale: Locale.current)
IQKeyboardManager.shared.isEnabled = true
IQKeyboardManager.shared.enableAutoToolbar = false
IQKeyboardManager.shared.resignOnTouchOutside = true
WXApi.registerApp(AppSettings.kAppsWXApiAppId, universalLink: AppSettings.kAppsUniversalLink)
window = UIWindow(frame: UIScreen.main.bounds)
window?.backgroundColor = .white
window?.overrideUserInterfaceStyle = .light
window?.rootViewController = LaunchViewController()
window?.makeKeyAndVisible()
return true
}
/// 用户同意隐私协议后再初始化会采集数据的 SDK。
func setupPrivacyGrantedSDKs() {
guard !didSetupPrivacyGrantedSDKs else { return }
didSetupPrivacyGrantedSDKs = true
#if !targetEnvironment(simulator)
AMapServices.shared().apiKey = AppSettings.kAppsAMapAppId
GeYanSdk.setPreLoginTimeout(10)
GeYanSdk.setEloginTimeout(10)
GeYanSdk.start(withAppId: AppSettings.kAppsGeTuiAppId) { isSuccess, error, gtcid in
print("GeYanSdk startWithAppId gtcid: \(gtcid ?? "")")
}
GeYanSdk.preGetToken { preDic in
print("preGetToken: \(preDic)")
}
#endif
GroupIMService.shared.initSDK()
UMConfigure.initWithAppkey(AppSettings.kAppsUMengAppKey, channel: "App Store")
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print("📱 application open url: \(url.absoluteString) host: \(url.host ?? "")")
// 微信 OAuth 回调:直接从 URL 提取 code,不依赖 SDK 异步 onResp
if let scheme = url.scheme, scheme == "wx\(AppSettings.kAppsWXApiAppId)" || scheme == AppSettings.kAppsWXApiAppId {
if let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
let code = components.queryItems?.first(where: { $0.name == "code" })?.value {
handleWechatLoginCode(code)
return true
}
}
return WXApi.handleOpen(url, delegate: self)
}
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
print("📱 continue userActivity: \(userActivity.webpageURL?.absoluteString ?? "")")
return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
}
}
extension AppDelegate {
/// 重置MainTabbarViewController
func showMainViewController() {
window?.rootViewController = MainTabBarController()
}
}
extension AppDelegate {
static var shared: AppDelegate {
UIApplication.shared.delegate as! AppDelegate
}
static var keyWindow: UIWindow? {
UIApplication.shared.windows.filter { $0.isKeyWindow }.first
}
static var rootViewController: UIViewController? {
getRootViewController(keyWindow?.rootViewController)
}
static func getRootViewController(_ inViewController: UIViewController?) -> UIViewController? {
var rootViewController: UIViewController? = inViewController
while inViewController?.presentedViewController != nil {
rootViewController = rootViewController?.presentedViewController
}
guard let rootVc = rootViewController else { return nil }
if rootVc.isKind(of: UINavigationController.self) {
return getRootViewController((rootVc as? UINavigationController)?.visibleViewController)
} else if rootVc.isKind(of: UITabBarController.self) {
return getRootViewController((rootVc as? UITabBarController)?.selectedViewController)
} else {
return rootVc
}
}
}
extension AppDelegate: WXApiDelegate {
private var handledWechatLoginCode: String? {
get { objc_getAssociatedObject(self, &AssociatedKeys.handledWechatLoginCode) as? String }
set { objc_setAssociatedObject(self, &AssociatedKeys.handledWechatLoginCode, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC) }
}
private struct AssociatedKeys {
static var handledWechatLoginCode: Void?
}
private func handleWechatLoginCode(_ code: String) {
guard code != handledWechatLoginCode else {
return
}
handledWechatLoginCode = code
LoginSessionHandler.login(type: "weixin", data: ["code": code, "code_type": ""])
}
func onReq(_ req: BaseReq) {
print(req)
}
func onResp(_ resp: BaseResp) {
if let authResp = resp as? SendAuthResp, authResp.errCode == 0, let code = authResp.code {
handleWechatLoginCode(code)
return
}
}
}