如果用户第一次使用我的应用程序,我正在尝试打开一个特殊的 viewController。我以前在其他情况下也可以使用它,但由于某种原因,它现在不起作用。我尝试了两种不同的方法,但都没有奏效。我尝试的第一件事是直接从 appDelegate 检查并执行它...然后我尝试检查它是否是第一次从根视图控制器 viewDidLoad 方法以及是否是第一次调用序列以打开特殊注册页面. Niether 正在工作,我不知道为什么.....这是我正在使用的一些代码:
这是在根视图控制器内部。当我这样做时,它会在日志上打印出“主要是第一次”,所以我知道它正在那里。我仔细检查了情节提要中的segue,它被正确命名并往返于正确的地方......
- (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.
}
在 appDelegate 内部:
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
BOOL hasused = [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];
}
任何想法为什么它不会工作?如果没有人知道处理这种情况的更好方法吗?