57 lines
1.9 KiB
Swift
57 lines
1.9 KiB
Swift
import UIKit
|
|
import Flutter
|
|
import CoreMotion
|
|
|
|
@UIApplicationMain
|
|
@objc class AppDelegate: FlutterAppDelegate {
|
|
private let motionManager = CMMotionManager()
|
|
|
|
override func application(
|
|
_ application: UIApplication,
|
|
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
|
) -> Bool {
|
|
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)
|
|
}
|
|
}
|