您好我有一个使用ASIHTTPRequest
登录Web服务器的应用程序。如果用户名和密码都正确,则会调用loginRequestFinished
。但是,当登录凭据不正确时,loginRequestFinished
和loginRequestFailed
都不会被调用。
我尝试通过实施NSTimer
来解决这个问题,以便它会在60秒后发出错误消息并取消请求。然而,到那时看起来请求已经dealloc
(我发现这很奇怪,因为状态栏中的加载指示器仍在旋转)。
这是我收到的错误:
-[__NSCFTimer cancel]: unrecognized selector sent to instance 0x84f0720
(lldb)
这是我的代码:
- (void) login
{
NSString* url = [NSString stringWithFormat:@"https://%@/local_login.html", self.serverIP];
ASIHTTPRequest *theRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[theRequest setDelegate:self];
[theRequest setUsername:username];
[theRequest setPassword:password];
[theRequest setDidFinishSelector:@selector(loginRequestFinished:)];
[theRequest setDidFailSelector:@selector(loginRequestFailed:)];
[theRequest startAsynchronous];
myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0f
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:NO];
}
- (void)timerFired:(ASIHTTPRequest *)request
{
[myTimer invalidate];
UIAlertView *warning = [[[UIAlertView alloc] initWithTitle:@"Error" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]autorelease];
[warning show];
[request cancel];
[request release];
}
任何帮助将不胜感激
更新 我检查了服务器日志。如果我的密码或用户名不正确,我的应用程序每秒都会向服务器发送登录请求,直到我退出iPhone模拟器。
2013-09-11 16:22:02.187 /local_login.html 401 91ms 0kb EC 1.0.1 (iPhone Simulator; iPhone OS 6.1; en_US)
150.101.105.182 - - [11/Sep/2013:16:22:02 -0700] "GET /local_login.html HTTP/1.1" 401 404 - "EC 1.0.1 (iPhone Simulator; iPhone OS 6.1; en_US)" "xyz.appspot.com" ms=91 cpu_ms=0 cpm_usd=0.000045 app_engine_release=1.8.4 instance=00c71b118abd13a90c30bcf64033c95172a494
2013-09-11 16:22:01.754 /local_login.html 401 29ms 0kb EC 1.0.1 (iPhone Simulator; iPhone OS 6.1; en_US)
150.101.105.182 - - [11/Sep/2013:16:22:01 -0700] "GET /local_login.html HTTP/1.1" 401 404 - "EC 1.0.1 (iPhone Simulator; iPhone OS 6.1; en_US)" "xyz.appspot.com" ms=30 cpu_ms=21 cpm_usd=0.000045 app_engine_release=1.8.4 instance=00c71b118abd13a90c30bcf64033c95172a494
(More Similar log events)
谢谢大家。问题已经解决。这是我将ASIHTTPRequest传递给myTimer的方式。我意识到我必须使用userInfo来传递请求。
答案 0 :(得分:0)
您的登录服务器可能会引发身份验证质询,是吗?
为什么不使用[theRequest setTimeOutSeconds:60];
而不是与计时器复合 - 崩溃问题是定时器的问题。
答案 1 :(得分:0)
在login方法中,为计时器设置UserInfo。如下所示
ASIHTTPRequest *theRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0f
target:self
selector:@selector(timerFired:)
userInfo:theRequest
repeats:NO];
为什么在重复时使计时器无效:NO。如果将重复设置为NO,则计时器将自动失效。
- (void)timerFired:(NSTimer *)timer
{
///[myTimer invalidate]; /// coment this. it will work.
ASIHTTPRequest *request = (ASIHTTPRequest *)[timer userInfo];
UIAlertView *warning = [[[UIAlertView alloc] initWithTitle:@"Error" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]autorelease];
[warning show];
[request cancel];
///[request release]; ///request is an autorelease object
}
如果你这样做,它应该可以正常工作。
另请参阅方法中的参数timerFired:timer是传递的参数,只要计时器调用timerFired:方法。