0
我是新來的目標C。我正在創建一個需要登錄的網站的移動版本。我想弄清楚如何讓用戶名和密碼成爲收到NSURLAuthenticationChallenge後傳入。這裏是我的代碼:Objective-C在NSURLAuthenticationChallenge之後等待響應
Session.m
// log in
-(BOOL)logIn
{
if (([Password class] == [NSNull class]) || ([Password length] == 0))
return NO;
if (([Username class] == [NSNull class]) || ([Username length] == 0))
return NO;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL_CONSTANT]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
if (!connection)
{
return NO;
}
return Connected;
}
// handle authentiaction challenge
-(void)connection:(NSURLConnection*)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"received auth challenge");
if ([challenge previousFailureCount] == 0)
{
NSLog(@"received auth challenge");
NSURLCredential *credentials = [NSURLCredential credentialWithUser:Username
password:Password
persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
[[challenge sender] useCredential:credentials forAuthenticationChallenge:challenge];
NSLog(@"responded to auth challenge");
}
else
{
Connected = NO;
NSLog(@"previous auth failure");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Data received");
Connected = YES;
}
的用戶名和密碼都被傳遞到從一個的UITextField一個Session對象。這裏的問題是,在連接獲取數據之前NO會被返回。我如何告訴logIn方法等待傳入憑據,然後等待響應(如果憑據有效)?