Add ios implementation of platform_message_relay

This commit is contained in:
Ming Ming 2023-09-07 00:26:02 +08:00
parent 904b898a44
commit 865f22a682
8 changed files with 163 additions and 0 deletions

View file

@ -18,6 +18,9 @@ migration:
- platform: android
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
- platform: ios
create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
# User provided section

View file

@ -0,0 +1,38 @@
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
profile
DerivedData/
build/
GeneratedPluginRegistrant.h
GeneratedPluginRegistrant.m
.generated/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/Generated.xcconfig
/Flutter/ephemeral/
/Flutter/flutter_export_environment.sh

View file

@ -0,0 +1,5 @@
import Foundation
struct K {
static let libId = "com.nkming.nc_photos.np_platform_message_relay"
}

View file

@ -0,0 +1,73 @@
import Flutter
import Foundation
import NpIosCore
class MessageRelayChannelHandler: NSObject, FlutterStreamHandler {
override init() {
do {
Self.idLock.lock()
defer { Self.idLock.unlock() }
id = Self.nextId
Self.nextId += 1
}
super.init()
}
func onMethodCall(call: FlutterMethodCall, result: FlutterResult) {
let args = call.arguments as? Dictionary<String, Any>
do {
switch call.method {
case "broadcast":
try broadcast(
event: (args?["event"] as? String).unwrap(),
data: args?["data"] as? String,
result: result
)
default:
result(FlutterMethodNotImplemented)
}
} catch let error as AppError {
result(FlutterError(code: "systemException", message: error.message, details: "\(error.stackTrace)"))
} catch {
result(FlutterError(code: "systemException", message: "\(error)", details: nil))
}
}
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
Self.evLock.lock()
defer { Self.evLock.unlock() }
Self.eventSinks[id] = events
return nil
}
func onCancel(withArguments arguments: Any?) -> FlutterError? {
Self.evLock.lock()
defer { Self.evLock.unlock() }
Self.eventSinks.removeValue(forKey: id)
return nil
}
private func broadcast(event: String, data: String?, result: FlutterResult) {
Self.evLock.lock()
defer { Self.evLock.unlock() }
for s in Self.eventSinks.values {
var map = ["event": event]
if data != nil {
map["data"] = data
}
s(data)
}
result(nil)
}
static let eventChannel = "\(K.libId)/message_relay_event"
static let methodChannel = "\(K.libId)/message_relay_method"
private static var eventSinks: [Int:FlutterEventSink] = [:]
private static var evLock = NSRecursiveLock()
private static var nextId = 0
private static var idLock = NSRecursiveLock()
private let id: Int
}

View file

@ -0,0 +1,18 @@
import Flutter
import UIKit
public class NpPlatformMessageRelayPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let handler = MessageRelayChannelHandler()
let eventChannel = FlutterEventChannel(
name: MessageRelayChannelHandler.eventChannel,
binaryMessenger: registrar.messenger()
)
eventChannel.setStreamHandler(handler)
let methodChannel = FlutterMethodChannel(
name: MessageRelayChannelHandler.methodChannel,
binaryMessenger: registrar.messenger()
)
methodChannel.setMethodCallHandler(handler.onMethodCall)
}
}

View file

@ -0,0 +1,24 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint np_platform_message_relay.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'np_platform_message_relay'
s.version = '0.0.1'
s.summary = 'A new Flutter plugin project.'
s.description = <<-DESC
A new Flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :type => 'GPLv3' }
s.author = { 'Your Company' => 'email@example.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.dependency 'NpIosCore'
s.platform = :ios, '12.0'
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
s.swift_version = '5.0'
end

View file

@ -28,3 +28,5 @@ flutter:
android:
package: com.nkming.nc_photos.np_platform_message_relay
pluginClass: NpPlatformMessageRelayPlugin
ios:
pluginClass: NpPlatformMessageRelayPlugin