2009-06-14 138 views
13

我试图让一个小型的twitter客户端运行,并在测试需要身份验证的API调用时遇到问题。iPhone上的基本HTTP身份验证

我的密码里有特殊字符,所以当我尝试使用下面的代码时它不起作用。

NSString *post = [NSString stringWithFormat:@"status=%@", [status stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@@%@/statuses/update.json", username, password, TwitterHostname]]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSURLResponse *response; 
NSError *error; 
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

我开始关注base64并将身份验证放入标头。我在他的base64实现中发现了Dave Dribin's的帖子,这似乎是有道理的。但是,当我尝试使用它时,编译器开始抱怨它无法找到openssl库。所以我读了我需要链接到libcrypto库,但似乎并不存在iphone。

我也读过一些人说苹果不会允许使用加密库的应用程序,这对我来说没有任何意义。

所以现在我有点卡住和困惑。在我的应用程序中获得基本身份验证的最简单方法是什么?

干杯

回答

15

两件事。首先你必须使用异步方法而不是同步/类方法。

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:req] 
                   cachePolicy:NSURLRequestUseProtocolCachePolicy 
                  timeoutInterval:30.0]; 

// create the connection with the request 
// and start loading the data 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

认证是通过实现您的代理这种方法管理:

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

而且你可能还需要过实现这些方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection; 

使用异步方法无论如何都倾向于提供更好的用户体验,因此即使没有进行身份验证的能力,额外的复杂性也是值得的。

+0

我改变了sendSynchronous调用initWithRequest:要求委托:自我和然后在委托中设置didReceiveAuthenticationChallenge,但没有任何被调用。我尝试了其他NSURLConnection委托方法,他们也没有工作。有任何想法吗? – Meroon 2009-06-14 22:09:35

0

您可以直接ASLO写下载主网址的用户名&密码即https://username:[email protected]/

首先你需要调用NSURLConnection的委托文件: -

  • (BOOL)连接:(NSURLConnection的* )连接canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

    {

    返回YES; }

,然后调用 - (无效)连接:(NSURLConnection的*)连接didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)挑战

{ 
if ([challenge previousFailureCount] == 0) 
     { 
      NSURLCredential *newCredential; 

      newCredential = [NSURLCredential credentialWithUser:@"username" 
                 password:@"password" 
                persistence:NSURLCredentialPersistenceForSession]; 
      NSLog(@"NEwCred:- %@ %@", newCredential, newCredential); 
      [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
     } 
     else 
     { 
      [[challenge sender] cancelAuthenticationChallenge:challenge]; 
      NSLog (@"failed authentication"); 
     } 
}