iPhone开发技巧之网络篇(5)— 使用libcurl连接https服务器
iPhone开发技巧之网络篇(5)--- 使用libcurl连接https服务器
问题
你是否也想让自己的 iPhone 应用程序连接 https 服务器呢?下面我就介绍一下其使用方法。
通常使用 Objective-C 的 NSURLConnection 连接有证明书的 https 服务器时会出现验证错误,我们可以使用私有API — setAllowsAnyHTTPSCertificate:forHost 来解决这个问题。如果是 Cocoa 的应用程序应该是没有什么问题,但是用在 iPhone 上,很可能过不了 App Store 的审查。
所以这里我们使用 libcurl 来完成在 iphone 上连接 https 服务器。
准备
编译 openssl
连接 https 的前提是要有 OpenSSL。你可以参考 这里 来为 iPhone 编译 OpenSSL 静态库。最终得到下面两个静态库文件。
1 2 |
libcrypto.a libssl.a |
编译 libcurl
接下来我们下载/编译 libcurl。下载展开后,按照下面配置(根据实际情况更改你的SDK目录,版本)。
1 2 3 4 5 6 |
./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 了。
1 |
SSL support: enabled (OpenSSL) |
接下来
1 2 |
make make install |
编译结果输出到 ~/tmp/iphonelib/curl/lib 下的 libcurl.a。
使用
添加到工程中
如下图所示,将编译好的静态库拖到你的工程中:
另外,由于 openssl 中使用了 zlib,所以还需要在工程中加入链接开关。(该库被包含在iPhone中,不需要重新编译)
如下图所示,在连接中追加 -lz。
最后,如下图添加编译所需的头文件路径。
比如,编译 libcurl 时的头文件的路径 ~/tmp/iphonelib/curl/include 。
代码例子
下来,让我们看看在程序中使用 libcurl 的例子。下面的例子在 AppDelegate.m 中实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#import "AppDelegate.h" #include <curl/curl.h> @implementation AppDelegate -(void)applicationDidFinishLaunching:(UIApplication *)application { window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch [window makeKeyAndVisible]; CURL *curl; CURLcode res; curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://twitter.com/"); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); res = curl_easy_perform(curl); if (0 != res) { fprintf(stderr, "curl error: %d\n", res); } curl_easy_cleanup(curl); } } -(void)dealloc { [window release]; [super dealloc]; } @end |
编译运行,可以用调试工具得到取得的html,如下图。
在模拟器中使用 libcurl
上面介绍的都是在设备上运行的例子,如果要在模拟器上使用,由于处理器结构不一样,需要重新编译 openssl 和 curl 静态库。
编译的时候,只要将 SDK 的路径由 iPhoneOS.platform ⇒ iPhoneSimulator.platform,编译开关 -arch armv6 ⇒ -arch i386 就可以了。
只是编译的文件名最好和iphone上用的区别开来,如下所示:
1 2 3 |
libcrypto_simulator.a libssl_simulator.a libcurl_simulator.a |
又或者不改变库的名称,而是增加新的编译目标。
相关文章
- iPhone开发技巧之发布篇(7)--- 制作自己的Cydia发布源 - (2012-01-20)
- iPhone开发技巧之发布篇(6)--- 不需Developper认证的真机调试方法 - (2011-12-25)
- iPhone开发技巧之环境篇(11) --- 让Xcode对应多个版本的iOS SDK - (2011-12-03)
- iPhone开发技巧之发布篇(5)--- 在程序中添加广告 - (2011-11-20)
- iPhone开发技巧之环境篇(10)--- 在控制台调试iPhone应用程序 - (2011-11-13)
- iPhone开发技巧之调试篇(3)--- 程序Crash后的调试技巧 - (2011-11-06)
- iPhone开发技巧之环境篇(9)--- Xcode中的注释 - (2011-01-12)
- iPhone开发技巧之数据篇(2)--- iPhone程序中的加密处理 - (2011-01-10)
- iPhone开发技巧之发布篇(4)--- 使用 Ad Hoc 发布自己的应用程序 - (2010-07-22)
- iPhone开发技巧之发布篇(3)--- 你的程序被拒了吗? - (2010-07-19)
- iPhone开发技巧之发布篇(2)--- 税务相关手续 - (2010-07-16)
- iPhone开发技巧之发布篇(1)--- 登录银行信息 - (2010-07-12)
- iPhone开发技巧之工具篇(4)--- 使用afconvert转换WAV文件 - (2010-06-24)
- iPhone开发技巧之私有API(8)--- UIApplication - (2010-06-18)
- iPhone开发技巧之私有API(7)--- 用UIWebView访问BASIC认证的页面 - (2010-06-16)
- iPhone开发技巧之私有API(6)--- 设置UIWebView中的User-Agent - (2010-06-14)
- iPhone开发技巧之私有API(5)--- UISegmentedControl - (2010-06-11)
- iPhone开发技巧之私有API(4)--- UIBarButtonItem - (2010-06-09)
- iPhone开发技巧之私有API(3)--- UIButton - (2010-06-07)
- iPhone开发技巧之数据篇(1)--- 使用正则表达式 - (2010-06-04)
- iPhone开发技巧之私有API(2)--- UITableView - (2010-06-01)
- iPhone开发技巧之私有API(1) --- 设备相关信息 - (2010-05-28)
- iPhone开发技巧之网络篇(4)--- 确认网络环境 3G/WIFI - (2010-05-14)
- iPhone开发技巧之网络篇(3)--- 使用NSOperation建立多任务网络连接 - (2010-05-12)
- iPhone开发技巧之网络篇(2)--- Web服务 - (2010-04-20)




2010年05月23日 12:06
为什么我编译的时候会出现下面的错误?而且编译出来的libcurl.a只有444k,还有哪里需要注意的,我的qq是634038132,方便的话请指教,这个问题把我愁死了……
make[6]: *** [install-pkgincludeHEADERS] Error 64
make[5]: *** [install-am] Error 2
make[4]: *** [install-recursive] Error 1
make[3]: *** [install-data-hook] Error 2
make[2]: *** [install-data-am] Error 2
make[1]: *** [install-am] Error 2
make: *** [install-recursive] Error 1
2010年07月30日 12:04
使用这样的库是否能通过审核?