我正试图创建一个屏幕时间应用程序与Flutter,应该能够阻止某些应用程序和限制手机使用。苹果有一个API,可以做到这一点。(https://developer.apple.com/videos/play/wwdc2022/110336/)但我就是不能让它与我的flutter应用程序一起工作。我已经能够弄清楚如何将我的main.dart脚本连接到原生iOS端,尽管我仍然无法将来自.swift脚本的打印消息显示到终端。下面是我的AppDelegate.swift文件中的代码:
import UIKit
import SwiftUI
import Flutter
import ManagedSettings
import FamilyControls
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let batteryChannel = FlutterMethodChannel(name: "samples.flutter.dev/nativeAPI", binaryMessenger: controller.binaryMessenger)
let center = AuthorizationCenter.shared
Task {
do {
try await center.requestAuthorization(for: .individual)
} catch {
print("Failed requestAuthorization error: \(error)")
}
}
batteryChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
switch call.method {
case "getDate":
result(self.getDate())
case "getBatteryLevel":
guard let args = call.arguments as? [String: String] else {return}
let name = args["name"]!
var systemVersion = UIDevice.current.systemVersion
result(systemVersion)
default:
result(FlutterMethodNotImplemented)
}
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func getDate() -> String {
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
return String(hour) + ":" + String(minutes) + ":" + String(seconds)
}
private func receiveBatteryLevel() -> Int {
let device = UIDevice.current
device.isBatteryMonitoringEnabled = true
if device.batteryState == UIDevice.BatteryState.unknown {
return -1
} else {
return Int(device.batteryLevel * 100)
}
}
}
当我在Xcode中运行这段代码时,我得到以下错误消息:Failed requestAuthorization error: Error Domain=FamilyControls.FamilyControlsError Code=3 "(null)"
有没有一个正确的方法来做到这一点,或者是不可能配对的两个?或者我必须注册一个苹果开发者帐户并使用该帐户登录才能访问此API?
我不知道从哪里开始,我没有尝试过任何其他的东西。遗憾的是,我无法将我的应用程序加载到苹果设备上,因为我正在使用macincloud进行开发,而且还没有购买开发者帐户,因为我不确定这个概念是否可行。
1条答案
按热度按时间vltsax251#
我创建了一个具有该功能的示例应用程序。https://github.com/ioridev/flutter_screentime