iPhone應用程式 HTTPS伺服器串連教程

來源:互聯網
上載者:User

iPhone應用程式 HTTPS伺服器串連教程是我們要一起來學習的內容。你是否也想讓自己的 iPhone 應用程式串連 https 伺服器呢?下面我就介紹一下其使用方法。

通常使用 Objective-C 的 NSURLConnection 串連有證明書的 https 伺服器時會出現驗證錯誤,我們可以使用私人API — setAllowsAnyHTTPSCertificate:forHost 來解決這個問題。如果是 Cocoa 的應用程式應該是沒有什麼問題,但是用在 iPhone 上,很可能過不了 App Store 的審查。

所以這裡我們使用 libcurl 來完成在 iphone 上串連 https 伺服器。

準備

編譯 openssl

串連 https 的前提是要有 OpenSSL。你可以參考 這裡 來為 iPhone 編譯 OpenSSL 靜態庫。最終得到下面兩個靜態庫檔案。 

 
  1. libcrypto.a  
  2. libssl.a 

編譯 libcurl 

接下來我們下載/編譯 libcurl。下載展開後,按照下面配置(根據實際情況更改你的SDK目錄,版本)。

 
  1. ./configure --prefix=$HOME/tmp/iphonelib/curl \  
  2.     --host=arm-apple-darwin --disable-shared --with-random=/dev/urandom \  
  3.     CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc \  
  4.     CFLAGS="-arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/
  5. iPhoneOS3.0.sdk -I$HOME/tmp/iphonelib/openssl/include -L$HOME/tmp/iphonelib/openssl/lib" \  
  6.     CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp \  
  7.     AR=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar 

如果最後輸出下面的內容,說明可以編譯支援 https 的 libcurl 了。

 
  1. SSL support:     enabled (OpenSSL) 

接下來

 
  1. make  
  2. make install 

編譯結果輸出到 ~/tmp/iphonelib/curl/lib 下的 libcurl.a。

使用

添加到工程中,

如所示,將編譯好的靜態庫拖到你的工程中:

另外,由於 openssl 中使用了 zlib,所以還需要在工程中加入連結開關。(該庫被包含在iPhone中,不需要重新編譯)

如所示,在串連中追加 -lz。

最後,如添加編譯所需的標頭檔路徑。

比如,編譯 libcurl 時的標頭檔的路徑 ~/tmp/iphonelib/curl/include 。

代碼例子下來,讓我們看看在程式中使用 libcurl 的例子。下面的例子在 AppDelegate.m 中實現。  

 
  1. #import "AppDelegate.h"  
  2. #include <curl/curl.h> 
  3. @implementation AppDelegate  
  4. -(void)applicationDidFinishLaunching:(UIApplication *)application {  
  5.     window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  6.     // Override point for customization after application launch  
  7.     [window makeKeyAndVisible];  
  8.     CURL *curl;  
  9.     CURLcode res;  
  10.     curl = curl_easy_init();  
  11.     if (curl) {  
  12.         curl_easy_setopt(curl, CURLOPT_URL, "https://twitter.com/");  
  13.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);  
  14.  
  15.         res = curl_easy_perform(curl);  
  16.         if (0 != res) {  
  17.             fprintf(stderr, "curl error: %d\n", res);  
  18.         }  
  19.         curl_easy_cleanup(curl);  
  20.     }  
  21. }  
  22. -(void)dealloc {  
  23.     [window release];  
  24.     [super dealloc];  
  25. }  
  26. @end 

編譯運行,可以用調試工具得到取得的html,如。


 
在模擬器中使用 libcurl

上面介紹的都是在裝置上啟動並執行例子,如果要在模擬器上使用,由於處理器結構不一樣,需要重新編譯 openssl 和 curl 靜態庫。編譯的時候,只要將 SDK 的路徑由 iPhoneOS.platform ⇒ iPhoneSimulator.platform,編譯開關 -arch armv6 ⇒ -arch i386 就可以了。只是編譯的檔案名稱最好和iphone上用的區別開來,如下所示:

 
  1. libcrypto_simulator.a  
  2. libssl_simulator.a  
  3. libcurl_simulator.a 

又或者不改變庫的名稱,而是增加新的編譯目標。

小結:iPhone應用程式 HTTPS伺服器串連教程的內容介紹我那了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.