2

UIWebView に安全な Web サイトをロードしようとしています。私の基本的なアプローチは、NSURL、na NSURLRequest、次に NSURLConnection を作成し、次に NSURLRequest を UIWebView にロードすることです。ウェブサイトが読み込まれると、私は受け取ります

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

チャレンジ送信者に応答します

- (void)useCredential:(NSURLCredential *)credential forAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

しかし、その後は何も得られません...ハングするだけです。私はブレークポイントを入れたので、それを知っています

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

が呼び出されています。NSURLConnection が完了しないことが確実になるまで待ってからビューをリロードすると、認証チャレンジは送信されませんが、ビューはロードされます。サーバーを制御することはできません。私は AFNetworking を使用することにオープンですが、必要な場合のみです。

ソース コードの完全なリストを以下に示します。

 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:

    (NSURLAuthenticationChallenge *)challenge
    {
      NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
      if ([challenge previousFailureCount] == 0)
  {
    NSString *username = @"username";
    NSString *password = @"passsword";
    NSURLCredential * cred = [NSURLCredential credentialWithUser:username
                                                        password:password
                                                     persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];
  }
  else
  {

  }
}

-(void)updateCard
{
  NSURL * url = [NSURL URLWithString:@"https://ssl.letu.edu/applications/chapelattendance/attendance.html"];
  NSURLRequest * request = [NSURLRequest requestWithURL:url
                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                        timeoutInterval:50.0];
  self.webView =[[UIWebView alloc] initWithFrame:self.bounds];
  self.webView.delegate = self;
  [self.webView loadRequest:request];
  self.connection = [[ NSURLConnection alloc]initWithRequest:request delegate:self];
  [self.connection start];
}

どこで私は間違えましたか?

4

4 に答える 4

0

誰かが来て同じ問題を抱えている場合は、私が見つけた解決策を共有したいことを確認してください. AFNetworking を使用します。

改訂されたコードは次のとおりです。

-(void)updateCard
{
  if(!self.webView)
  {
    self.webView =[[UIWebView alloc] initWithFrame:self.bounds];
    self.webView.delegate = self;
  }
  NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
  NSString *username = @"username";
  NSString *password = @"password";
  NSURL *url = [NSURL URLWithString:@"https://ssl.letu.edu/"];
  AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: url];

  [client setAuthorizationHeaderWithUsername:username password:password];

  NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"applications/chapelattendance/attendance.html"
                                                parameters:nil];

  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

  [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
  {
    [self.webView loadRequest:request];
  }
                                   failure: ^(AFHTTPRequestOperation *operation, NSError *error)
  {
    NSLog(@"Could not load chapel attendance");
  }];

  NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  [queue addOperation:operation];
}
于 2013-06-15T06:04:02.547 に答える
0

リクエストの送信中にリクエストを認証するには、ユーザー名とパスワードの組み合わせを http ヘッダーと一緒に送信する必要があります。

NSData *authData = [@"username:password" dataUsingEncoding:NSASCIIStringEncoding];

NSString *authorization = [NSString stringWithFormat:@"Basic %@", [authData base64Encoding]];

[mutableRequest addValue:authorization forHTTPHeaderField:@"Authorization"];
于 2014-09-19T14:58:02.480 に答える