Objective-C教學
Objective-C 教學首頁
Objective-C 語言概述
Objective-C 開發環境(安裝配置)
Objective-C語言程序結構
Objective-C 基本語法
Objective-C 數據類型
Objective-C 變量
Objective-C 常量
Objective-C 運算符
Objective-C 算術運算符
Objective-C 關係運算符
Objective-C 邏輯運算符
Objective-C 位運算符
Objective-C 賦值運算符
Objective-C 循環
Objective-C while循環
Objective-C for循環
Objective-C do...while循環
Objective-C 嵌套循環
Objective-C break語句
Objective-C continue語句
Objective-C 決策
Objective-C if語句
Objective-C if...else 語句
Objective-C 嵌套 if 語句
Objective-C switch語句
Objective-C 嵌套switch語句
Objective-C 函數
Objective-C 函數按值調用
Objective-C 函數引用調用
Objective-C 塊
Objective-C Numbers/數字
Objective-C Arrays/數組
Objective-C 多維數組
Objective-C 數組作爲函數參數傳遞
Objective-C 從函數返回數組
Objective-C 指針的數組
Objective-C 指針
Objective-C 指針運算
Objective-C 數組的指針
Objective-C 指向指針的指針
Objective-C 傳遞函數的指針
Objective-C 函數返回指針
Objective-C NSString/字符串
Objective-C struct/結構
Objective-C 預處理器
Objective-C typedef
Objective-C 類型轉換
Objective-C 日誌處理
Objective-C 錯誤處理
命令行參數
Objective-C 類&對象
Objective-C 繼承
Objective-C 多態性
Objective-C 數據封裝
Objective-C Categories/類別
Objective-C Posing/冒充
Objective-C 擴展
Objective-C Protocols/協議
Objective-C 動態綁定
Objective-C 複合對象
Obj-C Foundation/基礎框架
Objective-C 數據存儲
Objective C 文本和字符串
Objective-C 日期和時間
Objective-C 異常處理
Objective-C 文件處理
Objective-C URL加載系統
Objective-C 快速枚舉
Objective-C 內存管理

Objective-C URL加載系統

要訪問互聯網的URL,即項目是有用的URL加載。它提供的下面的類的幫助:

  • NSMutableURLRequest

  • NSURLConnection

  • NSURLCache

  • NSURLAuthenticationChallenge

  • NSURLCredential

  • NSURLProtectionSpace

  • NSURLResponse

  • NSURLDownload

  • NSURLSession

下面是一個簡單的URL加載的例子。這是不能在命令行中運行。我們需要創建Cocoa應用程序。

這可以通過選擇新,然後在XCode項目下的OS X應用程序部分出現的窗口中選擇Cocoa應用程序。

完成步驟的順序點擊下一步,會被要求提供一個項目名稱,給它一個名字。

appdelegate.h 文件內容如下:

#import <Cocoa/Cocoa.h> @interface AppDelegate : NSObject <NSApplicationDelegate> @property (assign) IBOutlet NSWindow *window; @end

更新以下AppDelegate.m文件:

#import "AppDelegate.h" @interface SampleClass:NSObject<NSURLConnectionDelegate> { NSMutableData *_responseData; } - (void)initiateURLConnection; @end @implementation SampleClass - (void)initiateURLConnection { // Create the request. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://date.jsontest.com"\]\]; // Create url connection and fire request NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [conn start]; } #pragma mark NSURLConnection Delegate Methods - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // A response has been received, this is where we initialize the instance var you created // so that we can append data to it in the didReceiveData method // Furthermore, this method is called each time there is a redirect so reinitializing it // also serves to clear it _responseData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Append the new data to the instance variable you declared [_responseData appendData:data]; } - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse { // Return nil to indicate not necessary to store a cached response for this connection return nil; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // The request is complete and data has been received // You can parse the stuff in your instance variable now NSLog(@"%@",[[NSString alloc]initWithData:_responseData encoding:NSUTF8StringEncoding]); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { // The request has failed for some reason! // Check the error var } @end @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { SampleClass *sampleClass = [[SampleClass alloc]init]; [sampleClass initiateURLConnection]; // Insert code here to initialize your application } @end

現在,當我們編譯並運行程序,我們會得到以下的結果。

2013-09-29 16:50:31.953 NSURLConnectionSample[1444:303] {
"time": "11:20:31 AM",
"milliseconds_since_epoch": 1380453631948,
"date": "09-29-2013"
}

在上述程序中,我們已經創建了一個簡單的JSON格式的,帶有時間和顯示時間的URL連接。