Files
rog_app/ios/Runner/AppDelegate.swift
2024-09-03 22:17:09 +09:00

59 lines
2.1 KiB
Swift

import Flutter
import UIKit
import CoreMotion
@main
@objc class AppDelegate: FlutterAppDelegate {
private let motionManager = CMMotionManager()
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
//GeneratedPluginRegistrant.register(with: self)
//return super.application(application, didFinishLaunchingWithOptions: launchOptions)
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let motionChannel = FlutterMethodChannel(name: "com.yourcompany.app/motion",
binaryMessenger: controller.binaryMessenger)
motionChannel.setMethodCallHandler({
[weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
guard let self = self else { return }
switch call.method {
case "startMotionUpdates":
self.startMotionUpdates(result: result)
case "stopMotionUpdates":
self.stopMotionUpdates(result: result)
default:
result(FlutterMethodNotImplemented)
}
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func startMotionUpdates(result: @escaping FlutterResult) {
if motionManager.isDeviceMotionAvailable {
motionManager.deviceMotionUpdateInterval = 0.1
motionManager.startDeviceMotionUpdates(to: .main) { (motion, error) in
DispatchQueue.main.async {
// UI
let appState = UIApplication.shared.applicationState
//
}
}
result(nil)
} else {
result(FlutterError(code: "UNAVAILABLE",
message: "Device motion is not available.",
details: nil))
}
}
private func stopMotionUpdates(result: @escaping FlutterResult) {
motionManager.stopDeviceMotionUpdates()
result(nil)
}
}