I am trying to open a special viewController if it is the users first time on my App. I’ve gotten this to work before in other situations but for some reason it isn’t working now. I’ve tried two different approaches and neither one is working. The first thing I tried is checking and doing it straight from the appDelegate…then I tried checking if it was the first time from the root view controllers viewDidLoad method and if it is the first time calling the seque to open a special registration page. Niether is working and I have no idea why…..Here is some of the code I’m using:
This is inside the rootviewcontroller. When I do it it prints out “Main is First Time” on the log so I know it is making it there. I double checked the segue in the storyboard and it is named correctly and goes to and from the right places….
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if([appDelegate firstTime])
{
[self performSegueWithIdentifier:@"mToRegister" sender:self];
NSLog(@"Main is First time");
}
// Do any additional setup after loading the view.
}
Inside the appDelegate:
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
BOOL haveused = [standardUserDefaults boolForKey:@”haveused”];
if(haveused){
//NOT THEIR FIRST TIME
//Handle scenario
firstTime = NO;
NSString* guid = [[NSMutableString alloc]initWithString:[self getMacAddress]];
//interest = [appDelegate interest];
NSString *splitter = [[NSString alloc]initWithString:@"&"];
NSMutableString *post = [[NSMutableString alloc]initWithString: @"user_read=1&guid="];
[post appendString:guid];
NSLog(@"String for Post is %@", post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:@"Basic ==" forHTTPHeaderField:@"Authorization"];
[request setURL:[NSURL URLWithString:@"http://something.com/app/usercrud.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
theConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
else {
NSLog(@"This is the First time");
firstTime = YES;
//THEIR FIRST TIME
//Handle scenario
// UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
// bundle: nil];
//
// UIViewController *controller = (UIViewController*)[mainStoryboard
// instantiateViewControllerWithIdentifier: @"register"];
// UIViewController *root = (UIViewController *)self.window.rootViewController;
// [root presentModalViewController:controller animated:YES];
[self.window.rootViewController performSegueWithIdentifier:@"mToRegister" sender:self];
}
Any ideas why it wouldn’t be working? If not anybody know a better way to handle this situation?
i thing your app delegate is not properly managed, use NSDefaults to store the any identity that is unique and check on next start, if that exists in NSDefaults then this is not for first time, and if it doesn’t exists, it is not the first time. test if, and if still prblm exists, then post your if part from app delegate too