Reputation: 103
I have a Web service for creating user session basically its a login web service. This web service basically creates a session key so that for every web service that i need to access is I need to attach this Session ID in my header, my question is where is the good place to store this session ID, I'm basically newbie in mobile development that has session id, not like web applications, browser have cookies and sessions, but how about in mobile application? Is it okay to save it in SQLite database? or there's other way to store this session ID? So that even though the user close the app and reopens it, the session will restore.
by the way, I'm using xamarin to create my mobile applications, I'm actually thinking if there's a storage that I can use to store and restore my session id in both iOS and android.
Thanks
Upvotes: 5
Views: 6994
Reputation: 2934
If you are using android (not Xamarin) then Use SharedPreferences classes in android and set your user session and get user session.I written code as. First Create SharedPreferences class named DataStore as
package com.example.examplesharedpreferenced.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class DataStore {
private static final String PREF_NAME = "com.example.examplesharedpreferenced.pref";
public static final String KEY_SESSION = "key_session";
public static void setUserSession(Context context, String session) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
PREF_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString(KEY_SESSION, session);
editor.commit();
}
public static String getUserSession(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_SESSION, null);
}
}
Then use theses above methods in Main Activity like
package com.example.examplesharedpreferenced;
import com.example.examplesharedpreferenced.utils.DataStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get user session
String userSession = DataStore.getUserSession(MainActivity.this);
Log.d(TAG, "userSession : "+userSession);
//setting user session
DataStore.setUserSession(MainActivity.this, "abc345asd");
//get user session after setting it.
String userSessionAfter = DataStore.getUserSession(MainActivity.this);
Log.d(TAG, "userSessionAfter : "+userSessionAfter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
For Xamarin you should follow link
Upvotes: 0
Reputation: 13922
I would suggest creating a private SharedPreferences for your App, and holding the value there. But then i would also suggest having the session expire every so often so that if the users phone was stolen, they would not be able to log in to your application and get information assuming that the expiration time was reached.
To save to shared preferences:
// create a String for the SharedPreferences
private static final String PREFS = "MyAppsPrivatePrefs";
private static final String SESS_KEY = "Session";
private String session = "";
// then access preferences
SharedPreferences sharedPrefs = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
// Open preferences for editting
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(SESSION, session);
editor.commit();
This should work but again I would suggest adding logic to clear this if some time has passed...
Upvotes: 2
Reputation: 771
You can work with SharedPreferences. http://developer.android.com/reference/android/content/SharedPreferences.html
The preferences will be removed only if the user clear the cache!
Upvotes: 0