Skip to content

Swift Client API

The Swift API can be used to develop client applications for macOS, iOS and tvOS. The API is distributed as a single SPM package to be installed and used locally.

Prerequisites

  • iOS 16 / macOS 13 (Ventura) or higher
  • XCode 15.2 or higher
  • Swift 5.9+

API documentation

Consult the API Javadoc

Adding the API in XCode

After you have created a new Application project proceed with the following steps:

  • Download the API .zip file
  • Uncompress the file in a directory of your choosing
  • Open File > Add Package Dependencies in XCode
  • Click the Add Local button at the bottom of the window and select the folder you have uncompressed
  • Finish by clicking on Add Package

Basic example

The following is a simple Swift client that connects to SmartFoxServer running on the local machine:

import SFS3Api

class SimpleClient
{
    var sfs: SmartFox
    var cfg: ConfigData

    init()
    {
        sfs = SmartFox()
        cfg = ConfigData()
        cfg.host = "127.0.0.1"
        cfg.zone = "Playground"

        sfs.addEventListener(evtType: SFSEvent.CONNECTION, delegate: EventDelegate(onConnection))
        sfs.addEventListener(evtType: SFSEvent.CONNECTION_LOST, delegate: EventDelegate(onConnectionLost))

        sfs.addEventListener(evtType: SFSEvent.LOGIN, delegate: EventDelegate(onLogin))
        sfs.addEventListener(evtType: SFSEvent.LOGIN_ERROR, delegate: EventDelegate(onLoginError))

        sfs.addEventListener(evtType: SFSEvent.ROOM_JOIN, delegate: EventDelegate(onJoinRoom))
        sfs.addEventListener(evtType: SFSEvent.ROOM_JOIN_ERROR, delegate: EventDelegate(onJoinRoomError))

        SFSLog.info("API Version: \(sfs.version)")
        sfs.connect(cfg)
    }

    func onConnection(evt: ApiEvent)
    {
        let success = evt.params[EventParam.Success] as! Bool

        if success {
            SFSLog.info("Connection to \(cfg.host):\(cfg.port)")

            // Login as guest user (user name assigned by the server)
            sfs.send(LoginRequest(userName: ""))

        } else {
            SFSLog.info("Connection attempt failed!")
        }
    }

    func onConnectionLost(evt: ApiEvent)
    {
        let reason = evt.params[EventParam.DisconnectionReason] as! String
        SFSLog.warn("Disconnected, Reason: \(reason)")
    }

    func onLogin(evt: ApiEvent)
    {
        let user = evt.params[EventParam.User] as! BaseUser
        SFSLog.info("Logged in as: \(user.name)")

        // Join the main Lobby
        sfs.send(JoinRoomRequest(room: "Lobby"))
    }

    func onLoginError(evt: ApiEvent)
    {
        let msg = evt.params[EventParam.ErrorMessage] as! String
        SFSLog.warn("Login failed: \(msg)")
    }

    func onJoinRoom(evt: ApiEvent)
    {
        let room = evt.params[EventParam.Room] as! BaseRoom
        SFSLog.info("Joined in: \(room.name)")
    }

    func onJoinRoomError(evt: ApiEvent)
    {
        let msg = evt.params[EventParam.ErrorMessage] as! String
        SFSLog.warn("Join error: \(msg)")
    }
}