background image

 iPhone App  https 方式连接网络

    是否想让自己的 iPhone 应用程序连接 https 服务器呢?下面就介绍一下其使用方法。
    通常使用 Objective-C 的 NSURLConnection 连接有证明书的 https 服务器时会出现验证错
误,我们可以使用私有

API — setAllowsAnyHTTPSCertificate:forHost 来解决这个问题。如果

 Cocoa 的应用程序应该是没有什么问题,但是用在 iPhone 上,很可能过不了 App Store 

的审查。
    所以这里我们使用 libcurl 来完成在 iphone 上连接 https 服务器。
准备
编译

 openssl

    连接 https 的前提是要有 OpenSSL。你可以为 iPhone 编译 OpenSSL 静态库。最终得到下面
两个静态库文件。
libcrypto.a
libssl.a
编译

 libcurl

    接下来下载/编译 libcurl。下载展开后,按照下面配置(根据实际情况更改你的 SDK 目录,
版本

)。

./configure --prefix=$HOME/tmp/iphonelib/curl \
    --host=arm-apple-darwin --disable-shared --with-random=/dev/urandom \
    CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc \
 

 

 

 CFLAGS="-arch 

armv6 

-isysroot 

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk 
-I$HOME/tmp/iphonelib/openssl/include -L$HOME/tmp/iphonelib/openssl/lib" \
    CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp \
    AR=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar
如果最后输出下面的内容,说明可以编译支持

 https 的 libcurl 了。

SSL support:     enabled (OpenSSL)
    接下来
make
make install
    编译结果输出到 ~/tmp/iphonelib/curl/lib 下的 libcurl.a。
使用
添加到工程中

如下图所示,将编译好的静态库拖到你的工程中:

另外,由于

 openssl  中使用

 zlib,所以还需要在工程

中加入链接开关。

(该库被包