2

Web サイトが認証を必要としない場合、私のコードは正常に動作します。必要な場合は、「資格情報が作成されました」を出力した直後に EXC_BAD_ACCESS でエラーが発生します。私は何もリリースしていません。このコードはドキュメントから直接コピーされています。何が問題なのですか?

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
 if ([challenge previousFailureCount] == 0)
 {
  NSLog(@"received authentication challenge");

  NSURLCredential *newCredential;
  newCredential=[NSURLCredential credentialWithUser:@"root"
             password:@"rootpassword"
             persistence:NSURLCredentialPersistenceForSession];


  NSLog(@"credential created");

  [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

  NSLog(@"responded to authentication challenge");

 }
 else
 {
  NSLog(@"previous authentication failure");

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Failure" 
              message:@"Website did not accept the username and password."
                delegate:nil
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil];
  [alert show];
  [alert release];
 }
}
4

2 に答える 2

1

それはその特定のサイト認証方法と関係があることがわかりました-ログインしたいWebサイトを別のWebサイトに置き換えると、コードは正常に機能します。

(私はunRaidのステータスページからデータをスクレイピングしようとしていました。unRaidは実際のWebサーバーではなくemhttpを使用しているので、それはそれと関係があると思います)

于 2010-03-01T15:04:33.050 に答える
0

あなたが制御していない唯一のものは、の戻り値です[challenge sender]。nil でないことを確認するために、その説明を出力してみてください。

于 2010-02-22T05:29:33.723 に答える