自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone應(yīng)用程序 HTTPS服務(wù)器連接教程

移動開發(fā) iOS
本文介紹的是iPhone應(yīng)用程序 HTTPS服務(wù)器連接教程,很詳細的講解了服務(wù)器的鏈接,具體內(nèi)容我們看正文。

iPhone應(yīng)用程序 HTTPS服務(wù)器連接教程是我們要一起來學(xué)習(xí)的內(nèi)容。你是否也想讓自己的 iPhone 應(yīng)用程序連接 https 服務(wù)器呢?下面我就介紹一下其使用方法。

通常使用 Objective-C 的 NSURLConnection 連接有證明書的 https 服務(wù)器時會出現(xiàn)驗證錯誤,我們可以使用私有API — setAllowsAnyHTTPSCertificate:forHost 來解決這個問題。如果是 Cocoa 的應(yīng)用程序應(yīng)該是沒有什么問題,但是用在 iPhone 上,很可能過不了 App Store 的審查。

所以這里我們使用 libcurl 來完成在 iphone 上連接 https 服務(wù)器。

準備

編譯 openssl

連接 https 的前提是要有 OpenSSL。你可以參考 這里 來為 iPhone 編譯 OpenSSL 靜態(tài)庫。最終得到下面兩個靜態(tài)庫文件。 

  1. libcrypto.a  
  2. libssl.a 

編譯 libcurl 

接下來我們下載/編譯 libcurl。下載展開后,按照下面配置(根據(jù)實際情況更改你的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 

如果***輸出下面的內(nèi)容,說明可以編譯支持 https 的 libcurl 了。

  1. SSL support:     enabled (OpenSSL) 

接下來

  1. make  
  2. make install 

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

使用

添加到工程中,如圖:

iPhone應(yīng)用程序 HTTPS服務(wù)器連接教程

如下圖所示,將編譯好的靜態(tài)庫拖到你的工程中:

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

如下圖所示,在連接中追加 -lz。如圖:

iPhone應(yīng)用程序 HTTPS服務(wù)器連接教程

***,如下圖添加編譯所需的頭文件路徑。如圖:

iPhone應(yīng)用程序 HTTPS服務(wù)器連接教程

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

代碼例子下來,讓我們看看在程序中使用 libcurl 的例子。下面的例子在 AppDelegate.m 中實現(xiàn)。  

  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 

編譯運行,可以用調(diào)試工具得到取得的html,如下圖。

iPhone應(yīng)用程序 HTTPS服務(wù)器連接教程
 
在模擬器中使用 libcurl

上面介紹的都是在設(shè)備上運行的例子,如果要在模擬器上使用,由于處理器結(jié)構(gòu)不一樣,需要重新編譯 openssl 和 curl 靜態(tài)庫。編譯的時候,只要將 SDK 的路徑由 iPhoneOS.platform ⇒ iPhoneSimulator.platform,編譯開關(guān) -arch armv6 ⇒ -arch i386 就可以了。只是編譯的文件名***和iphone上用的區(qū)別開來,如下所示:

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

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

小結(jié):iPhone應(yīng)用程序 HTTPS服務(wù)器連接教程的內(nèi)容介紹我那了,希望本文對你有所幫助!

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-26 16:43:59

iPhone Web 服務(wù)器

2017-11-10 08:58:49

Web服務(wù)器應(yīng)用程序

2009-06-25 17:08:14

2010-04-16 13:32:28

Win2008 R2

2011-07-21 10:47:37

iPhone Cocoa 委托

2011-09-06 10:58:10

服務(wù)器應(yīng)用程序虛擬化

2009-06-01 11:37:46

EquinoxOSGi服務(wù)器

2024-02-02 09:28:21

FrankenPHP應(yīng)用

2018-03-12 09:13:12

應(yīng)用程序服務(wù)器部署

2009-11-18 09:48:38

Windows PHP

2011-11-16 11:30:08

虛擬化虛擬服務(wù)器虛擬服務(wù)器備份

2019-05-14 09:39:07

Web服務(wù)器Web容器應(yīng)用程序服務(wù)器

2011-07-26 09:41:23

iPhone xcode Mac OS X

2010-08-27 10:41:41

iPhone核心應(yīng)用程序

2011-07-20 15:58:58

iPhone 應(yīng)用程序 生命周期

2018-11-23 08:54:27

服務(wù)器程序監(jiān)控

2011-07-19 14:36:32

iPhone

2011-07-27 17:30:40

iPhone Locate 定位

2011-08-12 14:54:45

iPhone委托

2011-08-05 13:49:53

iPhone 應(yīng)用 開發(fā)
點贊
收藏

51CTO技術(shù)棧公眾號