【问题标题】:ASIHTTPRequest setDidFailSelector not called未调用 ASIHTTPRequest setDidFailSelector
【发布时间】:2013-09-11 06:43:01
【问题描述】:

您好,我有一个应用程序使用ASIHTTPRequest 登录到网络服务器。当用户名和密码都正确时,调用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 来传递请求。

【问题讨论】:

    标签: ios login asihttprequest


    【解决方案1】:

    在登录方法中为计时器设置 UserInfo。如下所示

     ASIHTTPRequest *theRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    
    
    
    myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0f
                                                            target:self
                                                 selector:@selector(timerFired:)
                                                          userInfo:theRequest
                                                           repeats: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: 方法被计时器调用时。

    【讨论】:

    • 在评论 [myTimer invalidate]; 行后我仍然遇到同样的错误
    • 谢谢你说得对。这是导致问题的参数尚未传递。我应该使用 userInfo 来传递请求。
    【解决方案2】:

    您的登录服务器可能会抛出身份验证挑战,是吗?

    为什么不使用[theRequest setTimeOutSeconds:60]; 而不是与计时器复杂化 - 崩溃问题是由计时器引起的。

    【讨论】:

    • 谢谢,我尝试设置 setTimeOutSeconds;但是,状态栏中的加载指示器不会停止旋转。
    • 可以同步调用吗? [theRequest startSynchronous];
    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多