MapMapkitIosPlugin.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Flutter
  2. import UIKit
  3. enum MapSupportedPackage: String {
  4. case traceLocation1 = "com.shishi.dingwei"
  5. }
  6. var currentPackage: MapSupportedPackage {
  7. if let bundleId = Bundle.main.bundleIdentifier {
  8. if bundleId.hasPrefix(MapSupportedPackage.traceLocation1.rawValue) {
  9. return .traceLocation1
  10. }
  11. }
  12. return .traceLocation1
  13. }
  14. public class MapMapkitIosPlugin: NSObject, FlutterPlugin {
  15. public static func register(with registrar: FlutterPluginRegistrar) {
  16. let factory = MapFlutterViewFactory(messenger: registrar.messenger())
  17. registrar.register(factory, withId: MapKitConstans.mapViewId, gestureRecognizersBlockingPolicy: FlutterPlatformViewGestureRecognizersBlockingPolicyWaitUntilTouchesEnded)
  18. // 注册通用方法通道
  19. CommonMethodChannelHandler.shared.setBinaryMessenger(binaryMessenger: registrar.messenger())
  20. let commonMethodChannel = FlutterMethodChannel(name: MapKitConstans.commonMethodChannelName, binaryMessenger: registrar.messenger())
  21. commonMethodChannel.setMethodCallHandler { (call, result) in
  22. CommonMethodChannelHandler.shared.handleMethodCall(call: call, result: result)
  23. }
  24. }
  25. }
  26. class CommonMethodChannelHandler: NSObject {
  27. private var binaryMessenger: FlutterBinaryMessenger?
  28. static let shared = CommonMethodChannelHandler()
  29. private override init() {
  30. super.init()
  31. }
  32. func setBinaryMessenger(binaryMessenger: FlutterBinaryMessenger) {
  33. self.binaryMessenger = binaryMessenger
  34. }
  35. func handleMethodCall(call: FlutterMethodCall, result: @escaping FlutterResult) {
  36. handleCommonMethodCall(call: call, result: result)
  37. }
  38. private func handleCommonMethodCall(call: FlutterMethodCall, result: @escaping FlutterResult) {
  39. guard let callMethod = CommonChannelMethod(rawValue: call.method) else {
  40. result(FlutterMethodNotImplemented)
  41. return
  42. }
  43. switch callMethod {
  44. case .`init`:
  45. result(true)
  46. handleStartLocation(result: result)
  47. case .getPlatformMapName:
  48. result("map_mapkit_ios")
  49. case .startLocation:
  50. handleStartLocation(result: result)
  51. }
  52. }
  53. private func handleStartLocation(result: @escaping FlutterResult) {
  54. guard let binaryMessenger else { return }
  55. let isSuccess = LocationManager.shared.start(withMessenger: binaryMessenger)
  56. if !isSuccess {
  57. result(FlutterError(code: "LocationPermissionError", message: "Location permission not granted", details: nil))
  58. return
  59. }
  60. }
  61. }