QinGang_interested/ios/Runner/AppDelegate.swift

95 lines
3.6 KiB
Swift
Raw Normal View History

2025-12-12 09:11:30 +08:00
import UIKit
import Flutter
import AVFoundation
@main
@objc class AppDelegate: FlutterAppDelegate {
//
static var orientationMask: UIInterfaceOrientationMask = .portrait
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller = window?.rootViewController as! FlutterViewController
// 1) orientation channel ()
let orientationChannel = FlutterMethodChannel(name: "app.orientation",
binaryMessenger: controller.binaryMessenger)
orientationChannel.setMethodCallHandler { [weak self] call, result in
guard let self = self else { return }
if call.method == "setOrientation" {
guard let arg = call.arguments as? String else {
result(FlutterError(code: "BAD_ARGS", message: "need 'landscape' | 'portrait'", details: nil))
return
}
//
if arg == "landscape" {
AppDelegate.orientationMask = .landscape
} else if arg == "portrait" {
AppDelegate.orientationMask = .portrait
} else {
result(FlutterError(code: "BAD_ARGS", message: "unknown arg", details: nil))
return
}
//
if #available(iOS 16.0, *) {
// VC supportedInterfaceOrientations
self.window?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
if let scene = self.window?.windowScene {
let orientations: UIInterfaceOrientationMask =
(arg == "landscape") ? .landscape : .portrait
do {
try scene.requestGeometryUpdate(.iOS(interfaceOrientations: orientations))
} catch {
result(FlutterError(code: "GEOMETRY_UPDATE_FAILED",
message: error.localizedDescription, details: nil))
return
}
}
} else {
let target: UIInterfaceOrientation =
(arg == "landscape") ? .landscapeLeft : .portrait
UIDevice.current.setValue(target.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
result(true)
} else {
result(FlutterMethodNotImplemented)
}
}
// 2) permissions channel (): iOS 访
let permissionChannel = FlutterMethodChannel(name: "qhd_prevention/permissions",
binaryMessenger: controller.binaryMessenger)
permissionChannel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in
if call.method == "requestCameraAccess" {
// AVCaptureDevice.requestAccess
AVCaptureDevice.requestAccess(for: .video) { granted in
DispatchQueue.main.async {
result(granted)
}
}
} else {
result(FlutterMethodNotImplemented)
}
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
//
override func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?)
-> UIInterfaceOrientationMask {
return AppDelegate.orientationMask
}
}