简体   繁体   中英

Add delegate to custom iOS Flutter Plugin

I'm working on integrating a custom iOS plugin into my Flutter app, problem is that I'm not getting delegate callbacks from the custom SDK Protocol.

I have to connect a bluetooth device to my app and I from the delegate calls I should receive the device's ID and pair it.

From the Flutter side, I can call the native functions from the customSdk: sdkInstance.scan() and there are even some internal (inside the sdk) prints with the scan results but my delegate calls are not in place.

I think I'm not correctly adding the delegate to the SDK, I can get this to work in a swift native app but not as a Flutter Plugin.

So here's more or less the code:

iOS Code

AppDelegate.swift

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

SwiftIosPlugin.swift

import Flutter
import UIKit
import CustomSDK

public class SwiftIosPlugin: NSObject, FlutterPlugin {

    let sdkInstance = CustomSDK.shared // This returns an instance of the SDK
    let channel: FlutterMethodChannel


    public static func register(with registrar: FlutterPluginRegistrar) 
      let channel = FlutterMethodChannel(name: "ios_plugin_channel", binaryMessenger: registrar.messenger())
      let instance = SwiftIosPlugin(channel)

      registrar.addMethodCallDelegate(instance, channel: channel)
      registrar.addApplicationDelegate(instance)
    }

    init (_ channel: FlutterMethodChannel) {
      self.channel = channel
      super.init()
      // In Swift, this is done in viewDidLoad()
      // Is this the correct place to do this?
      sdkInstance.addDelegate(self)

    }

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
      switch call.method {
        case "startScan":
         do {
           // This is being called and results printed
           try sdkInstance.scan()
          } catch {
            result(FlutterError(code: "400", message: "\(error)", details: nil))

          }
        case "connect":
            sdkInstance.connect(call, result)
    
        default:
          result(FlutterMethodNotImplemented)
      }
    }
}


// These should be called but are not
extension SwiftIosPlugin: CustomSDKDelegate {
    // Isn't called when scan() is executeed!
    public func onScanDevice(didScan id:String) {
        // do logic
    }

    public func onPairedDevice(didPair id:String) {
        // do logic
    }
}

Update:

Silly thing that I hope nobody else has this trouble...

Two things to consider:

  • The problem was some of the delegate's functions public func onScanDevice(didScan id:String) was missing a parameter (even though there weren't any errors pointed out by Xcode).
  • sdkInstance.addDelegate(self) was called too early in the class "lifecycle".

Be mindful of these things and you won't have any trouble!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM