私はiOSとhttpsのものに慣れていません。私は初心者のソフトウェア学生です。ユニアカウントからデータ (クラス番号、お知らせなど) を抽出し、アプリに表示するアプリを作成したいと考えています。
通常、大学の学生ページにアクセスし、ユーザー名とパスワードでログインすると、それらの情報がブラウザに表示されます。
NSURLConnection とそのデリゲートを使用してみました。以下の私のコードを見てください。最初にメソッド connection:didReceviveAuthenticationChallenge で、[NSURLCredential withUser:password:persistence] で NSURLCredential オブジェクトを作成しました。そして、それは機能していませんでした。次に、NSLog を実行しようとしたところ、protectionSpace.authenticationMethod が NSURLAuthenticationMethodServerTrust であることがわかりました。
次に、Apple の文書といくつかのグーグルで調べたソースを読もうとしました。しかし、私は本当にそれを得ることができず、以下に示すようにコードを編集しました (これは私の最終版であり、まだ機能していません)。
私が実際に理解していない点は、サーバーがユーザー名とパスワードを要求するはずだと思っていることです。その代わりに、credentialForTrust を要求しました。これは、私が読んだドキュメントに基づいて、サーバーに対する NSURLConnection デリゲートの信頼を評価することを提案しています。ただし、サーバーはユーザー名とパスワードを要求しませんでした。では、サーバーはどのアカウントにアクセスしているかをどのように知ることができますか。
したがって、その証明書(信頼)とユーザー名とパスワードの間に何らかの関係があるはずだと思います。これらの仕組みがよくわかりません。
私の質問は本当に明確ではないかもしれませんし、ばかげているかもしれません。私はこれらすべてのことを学んだことがないので、これを受け入れます。
だから、誰かが私にこれらがどのように機能するかを説明してください。https、SSL、NSURLConnection、NSURLCredentialなどの基本的な理解があると仮定して、解決策を教えてください。
皆様のご尽力に感謝いたします。
以下は私のコードです(動作していません)。
connection:didReceiveAuthenticationChallenge の NSLog() は、「NSURLAuthenticationMethodServerTrust」を出力します。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"authenticationMethod is: %@\n",challenge.protectionSpace.authenticationMethod);
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}else{
NSURLCredential *creden = [[NSURLCredential alloc] initWithUser:myusername password:mypassword persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:creden forAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@\n %@\n",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.myData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.myData setLength:0];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *str = [[NSString alloc] initWithData:self.myData encoding:NSASCIIStringEncoding];
NSLog(@"Data in String %@", str);
}