Ios NSTimer';她打断了
摘要:我正在开发一个iOS应用程序,需要登录到web应用程序中托管的帐户。该帐户是在web应用程序中创建和维护的(通过web界面,而不是iOS应用程序),可以在iOS应用程序开始使用该帐户后更改或删除。因此,我需要定期验证该帐户在web应用程序中是否仍然处于活动状态。我使用NSUserDefaults将deviceID(唯一帐号)存储在iOS应用程序中,并希望将此值与硬件唯一ID一起使用,以确认iOS设备仍与活动帐户关联 我的问题:如果我实施verifyDeviceConfiguration方法,则在用户登录时保持数据新鲜的NSTimer将停止工作 更多详细信息: 登录发生在my WelcomeViewController中,它使用故事板Segue打开显示内容的MainViewController。这是my MainViewController的ViewDidLoad中的一个NSTimer示例:Ios NSTimer';她打断了,ios,objective-c,Ios,Objective C,摘要:我正在开发一个iOS应用程序,需要登录到web应用程序中托管的帐户。该帐户是在web应用程序中创建和维护的(通过web界面,而不是iOS应用程序),可以在iOS应用程序开始使用该帐户后更改或删除。因此,我需要定期验证该帐户在web应用程序中是否仍然处于活动状态。我使用NSUserDefaults将deviceID(唯一帐号)存储在iOS应用程序中,并希望将此值与硬件唯一ID一起使用,以确认iOS设备仍与活动帐户关联 我的问题:如果我实施verifyDeviceConfiguration方法
getWeather = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(startFetchingWeather:) userInfo:nil repeats:YES];
-(void)verifyDeviceConfiguration:(NSNotification *)notification
{
[_uManager validateDevice:[[NSUserDefaults standardUserDefaults] valueForKey:@"deviceID"] deviceNumber:[SKFunctions identifierForVendor1]];
}
-(void)didReceiveValidationConfirmation:(NSArray *)message
{
if([message count] == 1)
{
for (NSArray *currentMessage in message)
{
// fails to validate
if([currentMessage valueForKey:@"loginError"] != nil)
{
// empty device and location
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"deviceID"];
// request user pair the device again
NSString *message = @"There was an error validating this device. Please pair the device again.";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Validation Error" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex)
{
[self viewDidAppear:YES];
}];
}
// successfully validates
else
{
// initiate the seque to the device display
[self performSegueWithIdentifier:@"registeredUserSegue" sender:nil];
}
}
}
else
{
NSLog(@"This should never happen");
}
}
-(void)deviceValidationFailedWithError:(NSError *)error
{
NSLog(@"Device validation error");
}
下面是我用来验证iOS设备是否仍然与有效帐户关联的代码。它位于WelcomeViewController中:
getWeather = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(startFetchingWeather:) userInfo:nil repeats:YES];
-(void)verifyDeviceConfiguration:(NSNotification *)notification
{
[_uManager validateDevice:[[NSUserDefaults standardUserDefaults] valueForKey:@"deviceID"] deviceNumber:[SKFunctions identifierForVendor1]];
}
-(void)didReceiveValidationConfirmation:(NSArray *)message
{
if([message count] == 1)
{
for (NSArray *currentMessage in message)
{
// fails to validate
if([currentMessage valueForKey:@"loginError"] != nil)
{
// empty device and location
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"deviceID"];
// request user pair the device again
NSString *message = @"There was an error validating this device. Please pair the device again.";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Validation Error" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex)
{
[self viewDidAppear:YES];
}];
}
// successfully validates
else
{
// initiate the seque to the device display
[self performSegueWithIdentifier:@"registeredUserSegue" sender:nil];
}
}
}
else
{
NSLog(@"This should never happen");
}
}
-(void)deviceValidationFailedWithError:(NSError *)error
{
NSLog(@"Device validation error");
}
还有一些代码使用NSURLConnection从提供帐户确认的服务器获取响应。该守则如下:
-(void)validateDevice:(NSNumber *)deviceID deviceNumber:(NSString *)deviceNumber
{
[self.communicator validateDevice:deviceID deviceNumber:deviceNumber];
}
-(void)receivedValidationConfirmationJSON:(NSData *)objectNotation
{
NSString *decodedString = [[NSString alloc] initWithData:objectNotation encoding:NSUTF8StringEncoding];
NSLog(@"Decoded Validation Confirmation: %@", decodedString);
NSError *error = nil;
NSArray *validationMessage = [SKLoginBuilder deviceValidationConfirmationJSON:objectNotation error:&error];
if(error != nil) {
[self.delegate deviceValidationFailedWithError:error];
} else {
[self.delegate didReceiveValidationConfirmation:validationMessage];
}
}
-(void)validateDevice:(NSNumber *)deviceID deviceNumber:(NSString *)deviceNumber
{
NSString *urlAsString = [NSString stringWithFormat:@"%@/%@&d=%@&n=%@&apiKey=%@", API_URL, @"validation", deviceID, deviceNumber, API_KEY_LOGIN];
NSLog(@"Device Validation URL: %@", urlAsString);
BOOL validUrl = [SKFunctions validateURL:urlAsString];
if(validUrl)
{
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
if([NSURL URLWithString:urlAsString] != nil)
{
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(error) {
[self.delegate deviceValidationFailedWithError:error];
} else {
[self.delegate receivedValidationConfirmationJSON:data];
}
}];
}
}
else
{
//error handing for poorly formatted url
UIAlertView *urlFailure = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was an error validating your tablet. Please logout and login again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
[urlFailure show];
}
}
如果我关闭verifyDeviceConfiguration,一切正常…NSTimer将启动,数据将刷新。如果我打开verifyDeviceConfiguration,MainViewController将加载,它将填充数据一次(直接调用数据的初始加载,不使用NSTimer),并且NSTimer将不会触发和刷新数据
最后,我会处理NSLog语句
想法?谢谢。我在学习和生活!我在以下特定事件中使计时器无效,然后再次尝试运行计时器。实际上,我想要/需要的是暂停计时器……我知道这是不可能做到的。所以,我采取了不同的方法,一切都很好
[getTime invalidate];
谢谢