Swift IOS examples

Post here your questions about the Objective-C API for SFS2X

Moderators: Lapo, Bax

croftie
Posts: 49
Joined: 24 Jul 2013, 08:35
Location: Algarve Portugal
Contact:

Swift IOS examples

Postby croftie » 02 May 2019, 16:02

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
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Swift IOS examples

Postby Lapo » 02 May 2019, 16:50

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
Lapo
--
gotoAndPlay()
...addicted to flash games
croftie
Posts: 49
Joined: 24 Jul 2013, 08:35
Location: Algarve Portugal
Contact:

Re: Swift IOS examples

Postby croftie » 03 May 2019, 07:19

Thanks, that worked perfectly

Return to “SFS2X iPhone / iPad / OSX API”

Who is online

Users browsing this forum: No registered users and 7 guests