Egret's native packaging calls the local storage function

enter the theme

 

Android side

 

Obtain the command message sent by Egret to the local through gameEngine.setRuntimeInterface for read and write operations. Only sharedprefference is introduced here, and the database storage is similar.

 

 

/**
* 写入
* @param context
* @param key
* @param data
*/
public static void saveData(Context context, String key, Object data){

String type = data.getClass().getSimpleName();
SharedPreferences sharedPreferences = context
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if ("Integer".equals(type)){ editor.putInt(key,

(Integer)data);
}else if ("Boolean".equals(type)){
editor.putBoolean(key, (Boolean)data);
}else if ("String".equals(type)){
editor.putString(key, (String)data);
}else if ("Float".equals(type)){
editor.putFloat(key, (Float)data);
}else if ("Long".equals(type)){
editor.putLong(key, (Long)data);
}

editor.commit();
}

/**
* read
* @param context
* @param key
* @param defValue
* @return
*/
public static Object getData (Context context , String key , Object defValue){

String type = defValue.getClass().getSimpleName() ;
SharedPreferences sharedPreferences = context.getSharedPreferences
( FILE_NAME , Context. MODE_PRIVATE ) ;
//defValue is the default value, if no data is currently available, return it if ( "Integer" .equals(type)){ return


sharedPreferences.getInt(key, (Integer)defValue);
}else if ("Boolean".equals(type)){
return sharedPreferences.getBoolean(key, (Boolean)defValue);
}else if ("String".equals(type)){
return sharedPreferences.getString(key, (String)defValue);
}else if ("Float".equals(type)){
return sharedPreferences.getFloat(key, (Float)defValue);
}else if ("Long".equals(type)){
return sharedPreferences.getLong(key, (Long)defValue);
}

return null;
}

 

 

 

At last

 

IOS side

Obtain the message sent by Egret to native through [ EgretRuntime getInstance ] setRuntimeInterface and do related processing

Only the NSUserDefaults method is introduced here, other methods are similar, and this is enough for lightweight use.

#pragma mark - write

+ (void)setItem:(NSString *)key value:(NSString *)value{

    NSUserDefaults *user = [NSUserDefaultsstandardUserDefaults];

    [user setObject:value forKey:key];

}

 

#pragma mark - read

+ (NSString *)getItem:(NSString *)key{

    NSUserDefaults *user = [NSUserDefaultsstandardUserDefaults];

    NSString *value = [user objectForKey:key];

    return value;

 

}

 

Finish

 

The principle is to communicate with the local through egret to realize the native call.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326215532&siteId=291194637