I wish the first time anyone loads my application to have it start at the preferences view i have and every other time to start at the main view.
I could not find a way to detect if this is the first time开发者_运维百科 the application is run. any ideas?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
NSUserDefaults *padFactoids;
int launchCount;
padFactoids = [NSUserDefaults standardUserDefaults];
launchCount = [padFactoids integerForKey:@"launchCount" ] + 1;
[padFactoids synchronize];
NSLog(@"this is the number: %i of times this app has been launched", launchCount);
if ( launchCount == 1 )
{
NSLog(@"this is the FIRST LAUNCH of the app");
// do stuff here as you wish
bbb = [[Blue alloc]init];
[window addSubview:bbb.view];
}
if ( launchCount >= 2 )
{
NSLog(@"this is the SECOND launch of the damn app");
// do stuff here as you wish
rrr = [[Red alloc]init];
[window addSubview:rrr.view];
}
[window makeKeyAndVisible];
return YES;
}
here Red & Blue are subclasses of uiviewcontroller in both classes only one difference is that in -(void)viewDidLoad{ self.view.backgroundcolor = [UIColor redColor]; }in case of Red class & in blue class which shows blue backgroundcolor but when i execute app its shows only blue color not show red color where i wrong what i do for when i ran app IInd time it shows red color.....
Here is exactly how to do it. You will be pleased to know it is incredibly easy. It is exactly FOUR lines of code.
Add this code anywhere you want. Perhaps simply in your application:didFinishLaunchingWithOptions: routine in the file AppDelegate.m. Or, wherever you do general setup for your application. (However, be sure it will run once only.)
NSUserDefaults *padFactoids;
int launchCount;
padFactoids = [NSUserDefaults standardUserDefaults];
launchCount = [padFactoids integerForKey:@"launchCount" ] + 1;
[padFactoids setInteger:launchCount forKey:@"launchCount"];
[padFactoids synchronize];
NSLog(@"number of times: %i this app has been launched", launchCount);
if ( launchCount == 1 )
{
NSLog(@"this is the FIRST LAUNCH of the app");
// do stuff here as you wish
}
if ( launchCount == 2 )
{
NSLog(@"this is the SECOND launch of the damn app");
// do stuff here as you wish
}
// enjoy!
Almost every app, other than the simplest, does this. Hope it helps. For the record in theory you do not necessarily have to bother with the "synchronize" call but we have found over huge numbers of real-life user runs it is probably more reliable if you do include it.
PS Do NOT use Booleans in preferences. If you are a new programmer, it is iffy to understand the defaults and hence never maintainable. Stick to integers. (They are always an "integer zero" when first unused, so you have no problems.) Easy Peasy. Hope it helps.
check some flag from the NSUserdefaults, if it is not there you show the preference view, and set that flag after these settings have been set.
BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"];
if (!hasBeenStarted) {
NSLog(@"Show panel");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"];
}
better will be if you create a local file
at app launch. And check if the file exists or not at every launch.
In case of NSUserDefault
, this will be re-initialize every-time if the application was not running in the background.
- (void)checkApplicationFirstLaunch
{
if(![self getFileExistence:@"appinstall"])
{
// this is fresh install of the app
[self createFileFromString:@"install" filename:@"appinstall"];
}
else
{
// app was already installed
}
}
Here are the methods i used above
- (BOOL) getFileExistence: (NSString *) filename
{
BOOL IsFileExists = NO;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *favsFilePath = [documentsDir stringByAppendingPathComponent:filename];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
if ([fileManager fileExistsAtPath:favsFilePath])
{
IsFileExists = YES;
}
return IsFileExists;
}
- (void)createFileFromString:(NSString *)string filename:(NSString *)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSLog(@"%@", documentsDirectory);
NSError *error;
BOOL succeed = [string writeToFile:[documentsDirectory stringByAppendingPathComponent:filename] atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!succeed){
// Handle error here
NSLog(@"Did not save: Error: %@", error);
}
}
BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"];
if (!hasBeenStarted) {
NSLog(@"Show panel");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"];
[[NSUserDefaults standardUserDefaults]synchronize];// save to NSUserDefaults
}
精彩评论