I have a home screen.
On button press. I either fetch information from the server and store it in an object or I create some default information using a class called "Defaults.h/m"
- where I store all my variables. And "Function.h/m"
where I manipulate my default values and call methods.
How can I pass this data globally to any of the future view controllers I create. So that I can access and use the information.
Here is my signin/notsignin code:
You can see that I call [Function fetchInformation]. This method checks if the user has internet connection, then proceeds to set values in the defaults class.
- (IBAction)signIn:(UIButton *)sender {
NSString * username = self.usernameTextField.text;
NSString * password = self.passwordTextField.text;
if([Function isInternetConnectionAvailable]){
if([Function login:username :password]){
[Function fetchInformation];
}
else{
UIAlertView * error = [Function showErrorAlert:@"Login" :@"Username or Password is incorrect" : @"OK"];
[error show];
}
}
else{
UIAlertView * error = [Function showErrorAlert:@"Connection" :@"Please connect to the internet" : @"OK"];
[error show];
}
}
- (IBAction)notSignedIn:(UIButton *)sender {
[Function fetchInformation];
}
Here is my fetchInformation method:
+(void)fetchInformation
{
//instantiate user defaults
NSString * information = @"";
if([Function isInternetConnectionAvailable]){
//set learnfest data from the server using a url request and json response
information = [self getAppInformation];
[self updateStoredLearnfestInformation:information];
}
else{
//set default information
information = [self getLocalLearnfestInformation];
[self updateStoredLearnfestInformation:information];
}
//[self debugOut:@"Information" :information];
[self decodeAppInformation:information];
}
Here is my updateStoredLearnfestInformation method:
+(void)updateStoredLearnfestInformation:(NSString *)information
{
//set default learnfest information
[self updateSavedDataWithString:@"DEFAULT" :@"DEFAULT_INFORMATION" :information];
}
And my updateSavedDataWithString method:
+(void)updateSavedDataWithString:(NSString *)file :(NSString *)key :(NSString * )value
{
[[NSUserDefaults standardUserDefaults] setValue:value forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Update: I've added 3 methods that I use to store my default information about my app. Please review and consider what I have already done.