0

こんにちは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 に渡した方法です。theRequest を渡すには userInfo を使用する必要があることに気付きました。

4

2 に答える 2

0

ログイン サーバーが認証チャレンジをスローする可能性がありますね。

タイマーを複雑にする代わりに使用しないのはなぜですか[theRequest setTimeOutSeconds:60];- クラッシュの問題はタイマーのせいです。

于 2013-09-11T07:02:44.827 に答える
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 タイマーに設定すると、自動的に無効になります。

- (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: メソッドがタイマーによって呼び出されるたびに渡される引数です。

于 2013-09-11T08:33:46.470 に答える