iPhone網絡開發中如何使用NSURLConnection是本文要介紹的內容,這篇文章是翻譯的蘋果官方文檔,想要看英文原版的可以到蘋果網站查看,來看詳細內容。
NSURLConnection 提供了很多靈活的方法下載URL內容也提供了一個簡單的接口去創建和放棄連接,同時使用很多的delegate方法去支持連接過程的反饋和控制
如何創建一個連接呢?
爲了下載url的內容,程序需要提供一個delegate對象,並且至少實現下面的方法
- - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response
- - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
- - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSURLConnect還提供了一個方便的類方法(class method) : sendSynchronousRequest:returningResponse:error: 可用來 同步地加載一個URL請求
- + (NSData *)sendSynchronousRequest: (NSURLRequest *)request returningResponse: (NSURLResponse **)response error: (NSError **)error
1. request 要裝載的URL請求. 這個request 對象 作爲初始化進程的一部分,被深度複製(deep-copied). 在這個方法返回之後, 再修改request, 將不會影響用在裝載的過程中的request
2. reponse 輸出參數, 由服務器返回的URL響應
3. error 輸出參數, 如果在處理請求的過程中發生錯誤,就會使用. 無錯誤,就爲NULL
舉例一
1、先創建一個NSURL
2、在通過NSURL創建NSURLRequest,可以指定緩存規則和超時時間
3、創建NSURLConnection實例,指定NSURLRequest和一個delegate對象
如果創建失敗,則會返回nil,如果創建成功則創建一個NSMutalbeData的實例用來存儲數據
代碼:
- NSURLRequest *theRequest=[NSURLRequest requestWithURL:
- [NSURL URLWithString:@“http://www.sina.com.cn/”]
- cachePolicy:NSURLRequestUseProtocolCachePolicy
- timeoutInterval:60.0];
- NSURLConnection *theConncetion=[[NSURLConnection alloc]
- initWithRequest:theRequest delegate:self];
- if(theConnection)
- {
- //創建NSMutableData
- receivedData=[[NSMutableData data] retain];
- }else // 創建失敗
- NSURLRequestUseProtocolCachePolicy //NSURLRequest默認的cache policy, 是最能保持一致性的協議。
- NSURLRequestReloadIgnoringCacheData //忽略緩存直接從原始地址下載
- NSURLRequestReturnCacheDataElseLoad //只有在cache中不存在data時才從原始地址下載
- NSURLRequestReturnCacheDataDontLoad //允許app確定是否要返回cache數據,如果使用這種協議當本地不存在response的時候,創建NSURLConnection or NSURLDownload實例時將會馬上返回nil;這類似於離線模式,沒有建立網絡連接;
NSURLConnection還有幾個初始化函數,有個初始化函數可以做到創建連接但是並不馬上開始下載,而是通過start:開始
當收到initWithRequest: delegate: 消息時,下載會立即開始,在代理(delegate)收到connectionDidFinishLoading:或者 connection:didFailWithError:消息之前可以通過給連接發送一個cancel:消息來中斷下載。
當服務器提供了足夠客戶程序創建NSURLResponse對象的信息時,代理對象會收到一個connection:didReceiveResponse:消息,在消息內可以檢查NSURLResponse對象和確定數據的預期長途,mime類型,文件名以及其他服務器提供的元信息
要注意,一個簡單的連接也可能會收到多個connection:didReceiveResponse:消息當服務器連接重置或者一些罕見的原因(比如多組mime文檔),代理都會收到該消息這時候應該重置進度指示,丟棄之前接收的數據
- -(void)connection:(NSURLConnection *) connectiondidReceiveResponse:
- (NSURLResponse*)response
- {
- [receiveData setLength:0];
- }
當下載開始的時候,每當有數據接收,代理會定期收到connection:didReceiveData:消息代理應當在實現中儲存新接收的數據,下面的例子既是如此
- -(void) connection:(NSURLConnection *) connection didReceiveData:
- (NSData *) data
- {
- [receiveData appendData:data];
- }
在上面的方法實現中,可以加入一個進度指示器,提示用戶下載進度
當下載的過程中有錯誤發生的時候,代理會收到一個connection:didFailWithError消息,消息參數裏面的NSError對象提供了具體的錯誤細節,它也能提供在用戶信息字典裏面失敗的url請求(使用NSErrorFailingURLStringKey)
當代理接收到連接的connection:didFailWithError消息後,對於該連接不會在收到任何消息
舉例
- -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- {
- [connection release];
- [receivedData release];
- NSLog(@"Connection failed! Error - %@ %@",
- [error localizedDescription],
- [[error userInfo] objectForKey:NSErrorFailingURLStringErrorKey]);
- }
最後,如果連接請求成功的下載,代理會接收connectionDidFinishLoading:消息代理不會收到其他的消息了,在消息的實現中,應該釋放掉連接
舉例:
- -(void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- //do something with the data
- NSLog(@"succeeded %d byte received",[receivedData length]);
- [connection release];
- [receivedData release];
- }
一個實現異步get請求的例子:
- NSString *url = [NSString stringWithFormat:@"http://localhost/chat/messages.php?past=%ld&t=%ld",
- lastId, time(0) ];
- NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
- [request setURL:[NSURL URLWithString:url]];
- [request setHTTPMethod:@"GET"];
- NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
- if (conn)
- {
- receivedData = [[NSMutableData data] retain];
- }
- else
- {
- }
- - (void)timerCallback {
- //[timer release];
- [self getNewMessages];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- {
- [receivedData setLength:0];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- [receivedData appendData:data];
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- if (chatParser)
- [chatParser release];
- if ( messages == nil )
- messages = [[NSMutableArray alloc] init];
- chatParser = [[NSXMLParser alloc] initWithData:receivedData];
- [chatParser setDelegate:self];//set the delegate
- [chatParser parse];//start parse
- [receivedData release];
- [messageList reloadData];
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
- [self methodSignatureForSelector: @selector(timerCallback)]];
- [invocation setTarget:self];
- [invocation setSelector:@selector(timerCallback)];
- //timer = [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];
- [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];//if set yes,then very 5 seconds updata the table
- }
一個實現同步Get請求的例子:
- // 初始化請求
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
- // 設置URL
- [request setURL:[NSURL URLWithString:urlStr]];
- // 設置HTTP方法
- [request setHTTPMethod:@"GET"];
- // 發 送同步請求, 這裏得returnData就是返回得數據了
- NSData *returnData = [NSURLConnection sendSynchronousRequest:request
- returningResponse:nil error:nil];
- // 釋放對象
- [request release];
來源: