Storing a Variable When the App Is First Installed

Storing a variable when the app is first installed

Hey @Curtis Cowan try with this

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let firstLaunch = NSUserDefaults.standardUserDefaults().boolForKey("FirstLaunchTime")
if firstLaunch {
println("Not First launch")
}
else {
println("First launch")
NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey:"FirstLaunchTime")
}

return true
}

Storing a variable when the app is first installed

Hey @Curtis Cowan try with this

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let firstLaunch = NSUserDefaults.standardUserDefaults().boolForKey("FirstLaunchTime")
if firstLaunch {
println("Not First launch")
}
else {
println("First launch")
NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey:"FirstLaunchTime")
}

return true
}

How to set variable only on first launch of activity

Short answer: Save boolean activityWasLauched on SharedPreferences

When the app launches always put the variable as false. Inside of the onCreate of the Activity put it true, then you can use:

if (activityWasLauched == false){
//use the default
}
else{
//use a bundle and get the value from an intent
}

To simplify the much as possible the use of SharedPreferences:
To store values in shared preferences:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sp.edit();
if(score > high_score)
{
editor.putInt("high_score", score);
editor.apply(); /* Edit the value here*/
}

To retrieve values from shared preferences:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String high_score= sp.getInt("high_score", "");

Ways to store variables throughout the life time of android app in phone

You can use SharedPreferences to store your variable inside shared preferences. For example, set it like:

SharedPreferences sharedPreferences = context.getSharedPreferences("DATA",Context.MODE_PRIVATE);
sharedPreferences.edit().putString("STRENGTH",yourVar).apply();

Then get it out using:

SharedPreferences sharedPreferences = context.getSharedPreferences("DATA",Context.MODE_PRIVATE);
strength = sharedPreferences.getString("STRENGTH",null);

react native : There is way to detect if it is the first time launch of the app or not?

As per the other Answer you will have to use the Asyn storage to check the value on Componentdidmount and set the value if its not there.

Also the rest of the logic will have to be changed a bit as well.
Here we show the ActivityIndicator until the check is done and you can do whatever you want after that.
I couldnt test this code as it has dependencies but I've put some inline comments so that you can change appropriately.

import { ActivityIndicator } from "react-native";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
azureLoginObject: {},
loginSuccess: false,
loading: true
};

}

async componentDidMount() {
const firstTime = await AsyncStorage.getItem("isFirstTime")
if (firstTime != null) {
this.setState({ loading: false });

//Place this based on your logic
this.azureInstance = new AzureInstance(credentials);
this._onLoginSuccess = this._onLoginSuccess.bind(this);

} else {
// It is first time
await AsyncStorage.setItem("isFirstTime", 'true');
this.setState({ loading: false })
}
}

_onLoginSuccess() {
this.azureInstance
.getUserInfo()
.then((result) => {
this.setState({
loginSuccess: true,
azureLoginObject: result,
});
console.log(result);
//HERE EXAMPLE FOR STORE SOME VARIABLE INTO MY REDUX STORE
store.dispatch(userPrincipalName(result.userPrincipalName));
store.dispatch(givenName(result.mobilePhone));
})
.catch((err) => {
console.log(err);
});
}

render() {
//Show activity indicator until async storage check is done
if (this.state.loading)
return <ActivityIndicator />;

if (!this.state.loginSuccess) {
return (
<Provider store={store}>
<AzureLoginView
azureInstance={this.azureInstance}
loadingMessage="Requesting access token"
onSuccess={this._onLoginSuccess}
/>
</Provider>
);
}

const { userPrincipalName, givenName } = this.state.azureLoginObject;

return (
<Provider store={store}>
<AppNavigator />
</Provider>
);
}
}

Storing variable value for lifetime in .Net?

Application is a key value dictionary of objects. Application state is stored in memory on the server and will be lost when application shutdown or crash. See this for more info.

If you want to store a value lifetime, I mean even when the Application crash or shutdown, you must use a database(it could be a xml file or structured file...).



Related Topics



Leave a reply



Submit