You can add some custom fields (e.g. "age", "gender" and "birthday") too. You can use any field names as long as they are not predefined by the SDK. To learn more about the predefined fields, please read the appledoc .
You will set your custom fields in the UserFields
instance.
Setting and updating custom fields
The following example illustrates how to set the custom fields.
Swift:
// Set custom fields.
let userFields = KiiUserFields ()
userFields . setObject ( NSNumber ( value : 20 as Int32 ), forKey : "age" )
userFields . setObject ( "male" , forKey : "gender" )
userFields . setObject ( NSNumber ( value : 170.5 as Float ), forKey : "height" )
userFields . setObject ( NSNumber ( value : true as Bool ), forKey : "isMember" )
// Remove custom fields.
userFields . removeFromServer ( forKey : "weight" )
userFields . removeFromServer ( forKey : "chest" )
// Get the currently logged-in user.
let user = KiiUser . current () !
do {
// Update the user attributes.
try user . update ( withUserFieldsSynchronous : userFields )
} catch let error as NSError {
// Handle the error.
return
}
// Set custom fields.
let userFields = KiiUserFields ()
userFields . setObject ( NSNumber ( value : 20 as Int32 ), forKey : "age" )
userFields . setObject ( "male" , forKey : "gender" )
userFields . setObject ( NSNumber ( value : 170.5 as Float ), forKey : "height" )
userFields . setObject ( NSNumber ( value : true as Bool ), forKey : "isMember" )
// Remove custom fields.
userFields . removeFromServer ( forKey : "weight" )
userFields . removeFromServer ( forKey : "chest" )
// Get the currently logged-in user.
let user = KiiUser . current () !
// Update the user attributes.
user . update ( with : userFields ) { ( retUser : KiiUser ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
}
Objective-C:
// Set custom fields.
KiiUserFields * userFields = [[ KiiUserFields alloc ] init ];
[ userFields setObject :[ NSNumber numberWithInt : 20 ] forKey : @"age" ];
[ userFields setObject : @"male" forKey : @"gender" ];
[ userFields setObject :[ NSNumber numberWithFloat : 170 . 5 f ] forKey : @"height" ];
[ userFields setObject :[ NSNumber numberWithBool : YES ] forKey : @"isMember" ];
// Remove custom fields.
[ userFields removeFromServerForKey : @"weight" ];
[ userFields removeFromServerForKey : @"chest" ];
NSError * error = nil ;
// Get the currently logged-in user.
KiiUser * user = [ KiiUser currentUser ];
// Update the user attributes.
[ user updateWithUserFieldsSynchronous : userFields error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Set custom fields.
KiiUserFields * userFields = [[ KiiUserFields alloc ] init ];
[ userFields setObject :[ NSNumber numberWithInt : 20 ] forKey : @"age" ];
[ userFields setObject : @"male" forKey : @"gender" ];
[ userFields setObject :[ NSNumber numberWithFloat : 170 . 5 f ] forKey : @"height" ];
[ userFields setObject :[ NSNumber numberWithBool : YES ] forKey : @"isMember" ];
// Remove custom fields.
[ userFields removeFromServerForKey : @"weight" ];
[ userFields removeFromServerForKey : @"chest" ];
// Get the currently logged-in user.
KiiUser * user = [ KiiUser currentUser ];
// Update the user attributes.
[ user updateWithUserFields : userFields block : ^ ( KiiUser * user , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];
If an existing field is not explicitly specified for the update operation, the field remains unchanged on Kii Cloud.
The fields are updated on Kii Cloud and in the KiiUser
instance after the updateWithUserFields:block:
method is successfully completed.
You can update predefiend and custom fields at the same time.
Only users themselves can update their fields. See User Attributes to learn more on who can access the user's fields.
Note: The [KiiUser saveSynchronous:]
and [KiiUser save:withCallback:]
methods are now deprecated.
Please use [KiiUser updateWithIdentityDataSynchronous:userFields:error:]
,
[KiiUser updateWithUserFieldsSynchronous:error]
,
or [KiiUser updateWithIdentityData:userFields:block:]
,
[KiiUser updateWithUserFields:block:]
instead.
Getting custom fields
The following example illustrates how to get the custom fields.
Swift:
// Get the currently logged-in user.
let user = KiiUser . current () !
do {
// Refresh the user to get the latest user info from Kii Cloud.
try user . refreshSynchronous ()
} catch let error as NSError {
// Handle the error.
return
}
// Get custom fields.
let age = ( user . getObjectForKey ( "age" ) as! NSNumber ) . intValue
let gender = user . getObjectForKey ( "gender" ) as! String
let height = ( user . getObjectForKey ( "height" ) as! NSNumber ) . floatValue
let isMember = ( user . getObjectForKey ( "isMember" ) as! NSNumber ) . boolValue
// Get the currently logged-in user.
let user = KiiUser . current () !
// Refresh the user to get the latest user info from Kii Cloud.
user . refresh { ( user : KiiUser ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
// Get custom fields.
let age = ( user !. getObjectForKey ( "age" ) as! NSNumber ) . intValue
let gender = user !. getObjectForKey ( "gender" ) as! String
let height = ( user !. getObjectForKey ( "height" ) as! NSNumber ) . floatValue
let isMember = ( user !. getObjectForKey ( "isMember" ) as! NSNumber ) . boolValue
}
Objective-C:
// Get the currently logged-in user.
KiiUser * user = [ KiiUser currentUser ];
NSError * error = nil ;
// Refresh the user to get the latest user info from Kii Cloud.
[ user refreshSynchronous : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Get custom fields.
int age = [[ user getObjectForKey : @"age" ] integerValue ];
NSString * gender = [ user getObjectForKey : @"gender" ];
double height = [[ user getObjectForKey : @"height" ] doubleValue ];
BOOL isMember = [[ user getObjectForKey : @"isMember" ] boolValue ];
// Get the currently logged-in user.
KiiUser * user = [ KiiUser currentUser ];
// Refresh the user to get the latest user info from Kii Cloud.
[ user refreshWithBlock : ^ ( KiiUser * user , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Get custom fields.
int age = [[ user getObjectForKey : @"age" ] integerValue ];
NSString * gender = [ user getObjectForKey : @"gender" ];
double height = [[ user getObjectForKey : @"height" ] doubleValue ];
BOOL isMember = [[ user getObjectForKey : @"isMember" ] boolValue ];
}];
Please call the getObjectForKey
method to get the field values.
Setting custom fields upon the user registration
The following example illustrates how to set the custom fields upon the user registration.
As shown in the sample code, you can set the custom fields with the setObject:forKey:
method after creating a KiiUser instance.
Swift:
// Create a user.
let username = "user_123456"
let password = "123ABC"
let user = KiiUser ( username : username , andPassword : password )
// Set custom fields.
user . setObject ( NSNumber ( value : 30 as Int32 ), forKey : "age" )
user . setObject ( NSNumber ( value : 0 as Int32 ), forKey : "score" )
do {
// Register the user.
try user . performRegistrationSynchronous ()
} catch let error as NSError {
// Handle the error.
return
}
// Create a user.
let username = "user_123456"
let password = "123ABC"
let user = KiiUser ( username : username , andPassword : password )
// Set custom fields.
user . setObject ( NSNumber ( value : 30 as Int32 ), forKey : "age" )
user . setObject ( NSNumber ( value : 0 as Int32 ), forKey : "score" )
// Register the user.
user . performRegistration { ( retUser : KiiUser ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
}
Objective-C:
// Create a user.
NSString * username = @"user_123456" ;
NSString * password = @"123ABC" ;
NSError * error = nil ;
KiiUser * user = [ KiiUser userWithUsername : username andPassword : password ];
// Set custom fields.
[ user setObject :[ NSNumber numberWithInt : 30 ] forKey : @"age" ];
[ user setObject :[ NSNumber numberWithInt : 0 ] forKey : @"score" ];
// Register the user.
[ user performRegistrationSynchronous : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a user.
NSString * username = @"user_123456" ;
NSString * password = @"123ABC" ;
KiiUser * user = [ KiiUser userWithUsername : username andPassword : password ];
// Set custom fields.
[ user setObject :[ NSNumber numberWithInt : 30 ] forKey : @"age" ];
[ user setObject :[ NSNumber numberWithInt : 0 ] forKey : @"score" ];
// Register the user.
[ user performRegistrationWithBlock : ^ ( KiiUser * user , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];