ios - Xcode 错误:“'AppDelegate' 类型的值没有成员 'receiveBatteryLevel' Flutter 官方 Flutter 平台频道示例
问题描述
我正在尝试使用 Swift 和 Xcode 为我的 Flutter 项目编写本机代码,对于初学者,我正在关注 Flutter 官方网站上的示例,该示例位于以下站点的 swift/ios 示例下文档的平台集成部分中,
实现的客户端没问题我完全按照代码示例进行操作。但是,在“AppDelegate.swift”文件的本机端,我收到错误消息:“'AppDelegate' 类型的值没有成员'receiveBatteryLevel'”
根据Xcode的问题是在这一行“self?.receiveBatteryLevel(result:result)”
我在 AppDelegate.swift 中的代码如下:
import UIKit
import Flutter
import Firebase
import GoogleMaps
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("myKeyGoesHere(don't mind)")
FirebaseApp.configure()
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let batteryChannel = FlutterMethodChannel(name:"samples.flutter.dev/battery",
binaryMessenger: controller as! FlutterBinaryMessenger)
batteryChannel.setMethodCallHandler({
[weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
// Note: this method is invoked on the UI thread.
// Handle battery messages.
guard call.method == "getBatteryLevel" else {
result(FlutterMethodNotImplemented)
return
}
self?.receiveBatteryLevel(result: result)
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
private func receiveBatteryLevel(result: FlutterResult) {
let device = UIDevice.current
device.isBatteryMonitoringEnabled = true
if device.batteryState == UIDevice.BatteryState.unknown {
result(FlutterError(code: "UNAVAILABLE",message: "Battery info unavailable",details: nil)
)
} else {
result(Int(device.batteryLevel * 100))
}
}
解决方案
推荐阅读
- python - 已安装 CUDA 但未使用 GPU
- java - How to find the first three strings, which start with a lower-case `a` in a sorted order
- python - Python Scikit Image find contours return the contours all in the first location of the list
- unity3d - 如何防止我的 Unity 包中的预构建程序集被剥离?
- json - 如何使用 AWS CLI 获取 NAT 网关公共 IP?
- extjs - Ext.js:如何将按钮向左对齐标题?
- haskell - 是否有一个函数的名称,它采用类型 a,并从 a 到单元返回一个函数?
- android - 无法使用 FragmentManager 将 app.fragment 投射到我的自定义片段 ModelFragment
- python - 尽管在本地工作,但在外部运行服务器的“连接被拒绝”
- r - 在 r 中正确地从剪贴板读取数据