我對iOS很新,嘗試實現HTTP post/Get通信的最佳方式。正確的方式來實現HTTP POST /在iOS中獲取
問題:
我想打多的API調用,並且每個電話都有其獨立的響應。我正在嘗試編寫通用網絡應用,理想情況下,它將採取API網址,撥打電話並將數據返回給調用者。什麼是實現它的正確方法?我發現每種方法都有適度的辯論和粉絲。
選項1:
dispatch_async(aQueue,^{
...[ make a sync network request get data back]
--- perform operation on data
--- then pass proceed data UI or set it in model.
dispatch_async(dispatch_get_main_queue()
}
選項2:
-(NSString *) postData:(NSDictionary *)data serverUrl:(NSString *)targetUrl
-- call post data method with seperate delegate for each caller
-- start async request
-- on DidFinishedLaunching or OnError check delegate & then
return response back to callback
感謝您的投入。
快速思考。在MVC設計中,我會更新Model對象。控制器將更新視圖。不要將數據傳回UI。 –
你可以使用['NSURLConnection'](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html)。請參閱該文檔,該文檔頂部的鏈接以及[SimpleURLConnections示例]中的GetController和PostController(http://developer.apple.com/library/ios/#samplecode/SimpleURLConnections /Introduction/Intro.html)來演示如何執行'GET'和'POST'請求。 – Rob
感謝球員們,對於選項1或選項2的任何最後投票 – kodeshpa