From 4dcbec69249b9d83194ed478e356e836dea2a500 Mon Sep 17 00:00:00 2001 From: Chung Tran Date: Wed, 6 May 2020 12:56:48 +0700 Subject: [PATCH 1/2] Support swift language Add instruction for implementing flutter app that uses Swift as iOS development language --- README.md | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4120bb7..5912524 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,81 @@ Flutter Socket IO Plugin, supported Android + iOS (iOS installation guide is com - 1. Copy the folder `example/ios/Runner/SocketObj` to `${PROJECT_ROOT}/ios/Runner/` -- 2. Replace the `example/ios/Runner/AppDelegate.m` line with `${PROJECT_ROOT}/ios/Runner/AppDelegate.m`. +- 2. [If your app's iOs development language is Objective-C] Replace the `example/ios/Runner/AppDelegate.m` line with `${PROJECT_ROOT}/ios/Runner/AppDelegate.m`. (Notice: **You should merge the old one in your project to merge with the new from this plugin if you have some change on that file**) -- 3. Open `${PROJECT_ROOT}/ios/Podfile`, paste this line `pod 'Socket.IO-Client-Swift', '~> 13.3.0'` before the end of `target 'Runner' do` block - -- 4. Run and Enjoy the plugin :) +- 3. [If your app's iOs development language is Swift] Add this line in `${PROJECT_ROOT}/ios/Runner/AppDelegate.swift` AFTER `GeneratedPluginRegistrant.register(with: self)` +~~~ +let socketChannel = FlutterMethodChannel(name: "flutter_socket_io", binaryMessenger: controller.binaryMessenger) + socketChannel.setMethodCallHandler { (call, result) in + guard let dict = call.arguments as? NSDictionary, + let socketNameSpace = dict[SOCKET_NAME_SPACE] as? String, + let socketDomain = dict[SOCKET_DOMAIN] as? String + else { + result(FlutterError(code: "ERROR", message: "Argument is wrong", details: nil)); + return + } + + switch call.method { + case SOCKET_INIT: + let query = dict[SOCKET_QUERY] as? String + let callback = dict[SOCKET_CALLBACK] as? String + var queryStringDictionary = [String: String]() + query?.components(separatedBy: "&") + .forEach { pair in + let components = pair.components(separatedBy: "=") + guard let key = components.first, + let value = components.last else { + return + } + queryStringDictionary[key] = value + } + SocketIOManager.shared()?.initSocket(socketChannel, domain: socketDomain, query: queryStringDictionary, namspace: socketNameSpace, callBackStatus: callback) + case SOCKET_CONNECT: + SocketIOManager.shared()?.connectSocket(socketDomain, namspace: socketNameSpace) + case SOCKET_DISCONNECT: + SocketIOManager.shared()?.disconnectDomain(socketDomain, namspace: socketNameSpace) + case SOCKET_SUBSCRIBES: + guard let data = (dict[SOCKET_DATA] as? String)?.data(using: .utf8), + let json = try? JSONSerialization.jsonObject(with: data, options: []) as? NSMutableDictionary + else { + result(FlutterError(code: "ERROR", message: "Data is wrong", details: nil)); + return + } + SocketIOManager.shared()?.subscribes(socketDomain, namspace: socketNameSpace, subscribes: json) + case SOCKET_UNSUBSCRIBES: + guard let data = (dict[SOCKET_DATA] as? String)?.data(using: .utf8), + let json = try? JSONSerialization.jsonObject(with: data, options: []) as? NSMutableDictionary + else { + result(FlutterError(code: "ERROR", message: "Data is wrong", details: nil)); + return + } + SocketIOManager.shared()?.unSubscribes(socketDomain, namspace: socketNameSpace, subscribes: json) + case SOCKET_UNSUBSCRIBES_ALL: + SocketIOManager.shared()?.unSubscribesAll(socketDomain, namspace: socketNameSpace) + case SOCKET_SEND_MESSAGE: + guard let event = dict[SOCKET_EVENT] as? String, + let message = dict[SOCKET_MESSAGE] as? String, + let callback = dict[SOCKET_CALLBACK] as? String + else { + result(FlutterError(code: "ERROR", message: "Event or message is wrong", details: nil)); + return + } + + SocketIOManager.shared()?.sendMessage(event, message: message, domain: socketDomain, namspace: socketNameSpace, callBackStatus: callback) + case SOCKET_DESTROY: + SocketIOManager.shared()?.destroySocketDomain(socketDomain, namspace: socketNameSpace) + case SOCKET_DESTROY_ALL: + SocketIOManager.shared()?.destroyAllSocket() + default: + result(FlutterMethodNotImplemented) + } + } +~~~ + +- 4. Open `${PROJECT_ROOT}/ios/Podfile`, paste this line `pod 'Socket.IO-Client-Swift', '~> 13.3.0'` before the end of `target 'Runner' do` block + +- 5. Run and Enjoy the plugin :) ## Use the plugin From 23ab2a7542625d750cbcf268bff9e60a2b8ca0e1 Mon Sep 17 00:00:00 2001 From: Chung Tran Date: Wed, 6 May 2020 12:58:25 +0700 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5912524..b93144f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Flutter Socket IO Plugin, supported Android + iOS (iOS installation guide is com - 2. [If your app's iOs development language is Objective-C] Replace the `example/ios/Runner/AppDelegate.m` line with `${PROJECT_ROOT}/ios/Runner/AppDelegate.m`. (Notice: **You should merge the old one in your project to merge with the new from this plugin if you have some change on that file**) -- 3. [If your app's iOs development language is Swift] Add this line in `${PROJECT_ROOT}/ios/Runner/AppDelegate.swift` AFTER `GeneratedPluginRegistrant.register(with: self)` +- 3. [If your app's iOs development language is Swift] Add these lines in `${PROJECT_ROOT}/ios/Runner/AppDelegate.swift` AFTER `GeneratedPluginRegistrant.register(with: self)` ~~~ let socketChannel = FlutterMethodChannel(name: "flutter_socket_io", binaryMessenger: controller.binaryMessenger) socketChannel.setMethodCallHandler { (call, result) in