顯示具有 iOS network 標籤的文章。 顯示所有文章
顯示具有 iOS network 標籤的文章。 顯示所有文章

2012年4月28日 星期六

HTTPS加密的V.K克鋼琴大賽

第一屆琴之翼V.K克國際鋼琴大賽 即將開始,



可惜彼得潘沉迷於App開發多年,
早已忘了如何彈琴。
但是,
彼得潘的字典裡沒有放棄呀,
為了提醒自己練習,
彼得潘決定將比賽的圖片收藏於Server,
每天經由iPhone連到Server觀看比賽圖片,
提醒自己比賽日期的逼近。

接下來彼得潘將示範如何經由iPhone存取遠在天邊Server上的圖片。
存取圖片沒什麼特別,
然而當它是https時,
就有些值得一提的密技了。

1. 經由https下載圖片

2. 宣告儲存下載資料的receiveData

我們從遙遠天邊網路下載的資料為NSData型別,由於一張圖片可能很大,我們將收到一連串的NSData物件,因此我們宣告NSMutableData型別的receiveData,利用它蒐集所有收到的NSData物件。一定要湊齊構成圖片的所有NSData,美美的圖片才能完整呈現。


3. 實作NSURLConnectionDataDelegate和NSURLConnectionDelegate宣告的接收資料相關callback method。

執行App:
https果然不是好惹的,
我們完全下載不到任何資料,

connection:didFailWithError:的訊息殘酷地出現在我們眼前。

-[TestHttpsViewController connection:didFailWithError:]

4. 破解https


在iOS 5之後, 破解https變得更容易了, 只要實作connection:willSendRequestForAuthenticationChallenge:。
但在iOS 5之前, 稍微麻煩點, 需要實作以下2個method。 connection:canAuthenticateAgainstProtectionSpace:
connection:didReceiveAuthenticationChallenge:

執行App:


距離2012.8.11還有約100天。
志在參加,
還要得獎。
接下來彼得潘要白天寫App,晚上練琴了!

2012年3月7日 星期三

非同步網路探究

NSURLConnection:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request
                          queue:(NSOperationQueue*) queue
              completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler
->
When the request has completed or failed, the block will be executed from the context of the
specified NSOperationQueue

ex:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue 
                           completionHandler:^(NSURLResponse *response,    
                                    NSData *data, NSError *error) 
 {
         NSLog(@"thread %p %p", [NSThread currentThread], [NSThread mainThread]);
}];
->   currentThread != mainThread

NSOperationQueue *queue = [NSOperationQueue currentQueue];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue 
                           completionHandler:^(NSURLResponse *response,    
                                    NSData *data, NSError *error) 
 {
         NSLog(@"thread %p %p", [NSThread currentThread], [NSThread mainThread]);
}];
->   currentThread == mainThread

block和物件生命的關係:


假設呼叫以下method


 [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

}];
如果block裡帶有某個物件,則此物件將維持生命直到block執行完。