Can I get a Swift example of set and retrieve custom user variable?
Each client will set their own.
1 client, will pull all of these from the user list.
Thanks in advance.
Swift example set and retrieve custom user var
Re: Swift example set and retrieve custom user var
Hi,
you can check the Swift2XConnector example in our iOS Example Pack here:
https://www.smartfoxserver.com/download ... p=examples
This will give you a generic idea on how to setup your code, instantiate the SmartFoxClient class and handle the events. It also shows how to create Room Variables, once logged in and joined into a Room.
As regards setting user variables you can use this, which is very similar to the example code just mentioned:
And handle the relative update event with:
Hope it helps
you can check the Swift2XConnector example in our iOS Example Pack here:
https://www.smartfoxserver.com/download ... p=examples
This will give you a generic idea on how to setup your code, instantiate the SmartFoxClient class and handle the events. It also shows how to create Room Variables, once logged in and joined into a Room.
As regards setting user variables you can use this, which is very similar to the example code just mentioned:
Code: Select all
let var1 = SFSUserVariable(name: "MyName", value: "Kermit the Frog", type: VariableType_STRING);
let var2 = SFSUserVariable(name: "MyColor", value: "Green", type: VariableType_STRING);
let var3 = SFSUserVariable(name: "MyNumber", value: 1, type: VariableType_INT);
sfs!.send(SetUserVariablesRequest(userVariables: [var1!, var2!, var3!]));
And handle the relative update event with:
Code: Select all
func onUserVariablesUpdate(_ evt: SFSEvent!)
{
let user = evt.params["user"] as! SFSUser
let changedVariables = evt.params["changedVars"] as! NSArray
print("Variables updated:")
for key in (changedVariables as! [String])
{
let value = user.getVariable(key)?.getValue() as Any?
print("\tkey:", key, "value:", value!)
}
}
Hope it helps
Re: Swift example set and retrieve custom user var
Can I read these from the room.userlist? Maybe I'm on the wrong track. What I'm looking to do is to setUserVariable "done" to true or false. Then be able to pull this from the user list so I can see in real time which client is done with a task.
onUserVariablesUpdate is across the entire room, right? Or is it only broadcast within the client? Thanks.
onUserVariablesUpdate is across the entire room, right? Or is it only broadcast within the client? Thanks.
Re: Swift example set and retrieve custom user var
The onUserVariablesUpdate event triggers exactly when there is an update, so that's the preferred way of doing it.
Reading from the user list is also possible but it is less efficient as you don't know when a change has occurred so, I guess, what you would need to do is keep reading from the list continuously, with some sort of polling system.
The use case is not very clear, but the polling system seems less efficient.
Cheers
Reading from the user list is also possible but it is less efficient as you don't know when a change has occurred so, I guess, what you would need to do is keep reading from the list continuously, with some sort of polling system.
The use case is not very clear, but the polling system seems less efficient.
Cheers
Return to “SFS2X iPhone / iPad / OSX API”
Who is online
Users browsing this forum: No registered users and 39 guests