2009-06-14 127 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"); 
     } 
}