<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 插件開發:iOS端API實現 本節我們接著之前"獲取電池電量"插件的示例,來完成iOS端API的實現。以下步驟使用Objective-C,如果您更喜歡Swift,可以直接跳到后面Swift部分。 首先打開Xcode中Flutter應用程序的iOS部分: 1. 啟動 Xcode 2. 選擇 File > Open… 3. 定位到您 Flutter app目錄, 然后選擇里面的 `iOS`文件夾,點擊 OK 4. 確保Xcode項目的構建沒有錯誤。 5. 選擇 Runner > Runner ,打開`AppDelegate.m` 接下來,在`application didFinishLaunchingWithOptions:`方法內部創建一個`FlutterMethodChannel`,并添加一個處理方法。 確保與在Flutter客戶端使用的通道名稱相同。 ``` #import <Flutter/Flutter.h> @implementation AppDelegate - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController; FlutterMethodChannel* batteryChannel = [FlutterMethodChannel methodChannelWithName:@"samples.flutter.io/battery" binaryMessenger:controller]; [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { // TODO }]; return [super application:application didFinishLaunchingWithOptions:launchOptions]; } ``` 接下來,我們添加Objective-C代碼,使用iOS電池API來獲取電池電量,這和原生是相同的。 在`AppDelegate`類中添加以下新的方法: ``` - (int)getBatteryLevel { UIDevice* device = UIDevice.currentDevice; device.batteryMonitoringEnabled = YES; if (device.batteryState == UIDeviceBatteryStateUnknown) { return -1; } else { return (int)(device.batteryLevel * 100); } } ``` 最后,我們完成之前添加的`setMethodCallHandler`方法。我們需要處理的平臺方法名為`getBatteryLevel`,所以我們在call參數中需要先判斷是否為`getBatteryLevel`。 這個平臺方法的實現只需調用我們在前一步中編寫的iOS代碼,并使用result參數返回成功或錯誤的響應。如果調用了未定義的API,我們也會通知返回: ``` [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { if ([@"getBatteryLevel" isEqualToString:call.method]) { int batteryLevel = [self getBatteryLevel]; if (batteryLevel == -1) { result([FlutterError errorWithCode:@"UNAVAILABLE" message:@"電池信息不可用" details:nil]); } else { result(@(batteryLevel)); } } else { result(FlutterMethodNotImplemented); } }]; ``` 現在可以在iOS上運行該應用程序了,如果使用的是iOS模擬器,請注意,它不支持電池API,因此應用程序將顯示“電池信息不可用”。 ### 使用Swift實現iOS API 以下步驟與上面使用Objective-C相似,首先打開Xcode中Flutter應用程序的iOS部分: 1. 啟動 Xcode 2. 選擇 File > Open… 3. 定位到您 Flutter app目錄, 然后選擇里面的 `ios`文件夾,點擊 OK 4. 確保Xcode項目的構建沒有錯誤。 5. 選擇 Runner > Runner ,然后打開`AppDelegate.swift` 接下來,覆蓋application方法并創建一個`FlutterMethodChannel`綁定通道名稱`samples.flutter.io/battery`: ``` @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { GeneratedPluginRegistrant.register(with: self); let controller : FlutterViewController = window?.rootViewController as! FlutterViewController; let batteryChannel = FlutterMethodChannel.init(name: "samples.flutter.io/battery", binaryMessenger: controller); batteryChannel.setMethodCallHandler({ (call: FlutterMethodCall, result: FlutterResult) -> Void in // Handle battery messages. }); return super.application(application, didFinishLaunchingWithOptions: launchOptions); } } ``` 接下來,我們添加Swift代碼,使用iOS電池API來獲取電池電量,這和原生開發是相同的。 將以下新方法添加到`AppDelegate.swift`底部: ``` private func receiveBatteryLevel(result: FlutterResult) { let device = UIDevice.current; device.isBatteryMonitoringEnabled = true; if (device.batteryState == UIDeviceBatteryState.unknown) { result(FlutterError.init(code: "UNAVAILABLE", message: "電池信息不可用", details: nil)); } else { result(Int(device.batteryLevel * 100)); } } ``` 最后,我們完成之前添加的`setMethodCallHandler`方法。我們需要處理的平臺方法名為`getBatteryLevel`,所以我們在call參數中需要先判斷是否為`getBatteryLevel`。 這個平臺方法的實現只需調用我們在前一步中編寫的iOS代碼,并使用result參數返回成功或錯誤的響應。如果調用了未定義的API,我們也會通知返回: ``` batteryChannel.setMethodCallHandler({ (call: FlutterMethodCall, result: FlutterResult) -> Void in if ("getBatteryLevel" == call.method) { receiveBatteryLevel(result: result); } else { result(FlutterMethodNotImplemented); } }); ``` 現在可以在iOS上運行應用程序,如果使用的是iOS模擬器,請注意,它不支持電池API,因此應用程序將顯示“電池信息不可用”。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看