WinkMeter / flutter_socket_io

Socket IO plugin for Flutter
MIT License
49 stars 49 forks source link

Newer Flutter Projects use .swift #3

Open ominibyte opened 5 years ago

ominibyte commented 5 years ago

Hi,

I was trying to follow your instructions for installing on iOS but it appears that newer projects now use Swift because I could not find the AppDelegate.m file under my Runner folder. I could only find AppDelegate.swift

My Swift/ObjC skills are next to none so I would be grateful to receive some help with regards to this.

Thanks.

rknell commented 5 years ago

Ugh.

Ok, so im in the same boat as @ominibyte

I got it working by doing the following:

  1. Delete AppDelegate.swift
  2. Drag over AppDelegate.h, AppDelegate.m and main.m to the Runner folder Note main.m
  3. Add $(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME) to your Build Settings > Library Search Paths (I had to add it to each one)

It would be great to have this work on swift but I am not the person. Also its the only project I have had to edit these files to link it in like this with flutter, and it feels like its going back to the bad old days of react native where everything breaks all the time. Would be great if there was a way to do it automatically. Not sure there is though.

bigearsenal commented 4 years ago

add these lines 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)
        }
    }
RaashVision commented 4 years ago

@bigearsenal what you define for controller? let socketChannel = FlutterMethodChannel(name: "flutter_socket_io", binaryMessenger: controller.binaryMessenger)****

How you import objc SocketObj to the project

richardbonneau commented 4 years ago

@bigearsenal I second @RaashVision , I added your lines of code but it's throwing an error where controller is not defined. What should we do?