Page 1 of 1

Swift IOS examples

Posted: 02 May 2019, 16:02
by croftie
I am just starting to get to grips with ios swift and your objective c api. Are there any simple examples of a swift implementation of the IOS framework showing a connect for example.

I am using Xcode 10 and SFS2X Api Objective C 1.7.8 framework.

I think I have successfully imported the framework to a simple iphone app environment with a bridging header (the code hints for sfs all seem to work) but initiating a connection does not seem to do anything and when I run it, the build is successful but I get errors

Code: Select all

import UIKit

class ViewController: UIViewController, ISFSEvents {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let sfs = SmartFox2XClient()
        print(sfs.version)
        sfs.logger.loggingLevel = LogLevel_DEBUG
        sfs.connect("host", port: 9933)
    }
    
    func onConnection(_ evt: SFSEvent!) {
        print("connected")
    }
}

results in the following in the console
Optional(0.0.0)
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
2019-05-02 16:59:21.304725+0100 testApp[696:176606] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Any pointers would be really helpful to get me started

Re: Swift IOS examples

Posted: 02 May 2019, 16:50
by Lapo
Hi,
here's a simple connection/login example in Swift:

Code: Select all

class AppDelegate: UIResponder, UIApplicationDelegate, ISFSEvents {

    var window: UIWindow?
    var sfs:SmartFox2XClient?
    
    //var someData:Data?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        sfs = SmartFox2XClient(smartFoxWithDebugMode: true, delegate: self)
        NSLog("Version: %@", (sfs?.version)!)

        sfs?.connect("127.0.0.1", port: 9933)
        
        
        return true
    }
    
    
    // -------------------------------------------------------------------------------------------
    // SFS2X Event handlers
    // -------------------------------------------------------------------------------------------
    
    func onConnection(_ evt: SFSEvent!)
    {
        
        let data:NSDictionary = evt.params as NSDictionary
        let success:Bool = data.object(forKey: "success") as! Bool
        
        if (success)
        {
            NSLog("Connection OK")
            
            // Send login
            sfs!.send(LoginRequest(userName: "", password: "", zoneName: "BasicExamples", params: nil))
        }
            
        else
        {
            NSLog("Connection failed")
        }
        
    }
    
    func onLogin(_ evt: SFSEvent!)
    {
        NSLog("Logged in as: %@", (sfs?.mySelf.name())!)
    }
    
    func onLoginError(_ evt: SFSEvent!)
    {
        let params:NSDictionary = evt.params as NSDictionary

        let errMess:String = params.object(forKey: "errorMessage") as! String
        NSLog("Login ERROR:", errMess)
    }
    
     //...
     //...
}
    
I think the main issue is that you're not passing a delegate that responds to the events in your code.

Hope it helps

Re: Swift IOS examples

Posted: 03 May 2019, 07:19
by croftie
Thanks, that worked perfectly