IPhoneApplicationsHTTPS ServerThe connection tutorial is what we will learn together. Do you want your ownIPhoneApplication connectionHttps ServerWhat about it? Next I will introduce how to use it.
Normally, use Objective-C's NSURLConnection to connect to a certifiedHttps ServerAuthentication error occurs. We can use private API-setAllowsAnyHTTPSCertificate: forHost to solve this problem. If it is a Cocoa application, there should be no problem, but it is used inIPhoneProbably won't pass the App Store review.
So here we use libcurl to connect to the https server on the iphone.
Preparation
Compile openssl
To connect to https, you must have OpenSSL. You can refer to here to compile the OpenSSL static library for the iPhone. The following two static library files are obtained.
- libcrypto.a
- libssl.a
Compile libcurl
Next we will download/compile libcurl. After downloading the SDK, follow the configuration below (change the SDK directory and version according to the actual situation ).
- ./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
If the following content is output, you can compile libcurl that supports https.
- SSL support: enabled (OpenSSL)
Next
- make
- make install
The compilation result is output ~ Libcurl. a under/tmp/iphonelib/curl/lib.
Use
Add to project,
Drag the compiled static library to your project, as shown in:
In addition, because zlib is used in openssl, you also need to add the link switch in the project. (This library is included in the iPhone and does not need to be re-compiled)
As shown in, append-lz to the connection.
Finally, add the header file path required for compilation.
For example, the path of the header file when compiling libcurl ~ /Tmp/iphonelib/curl/include.
Let's take a look at the libcurl example in the program. The following example is implemented in AppDelegate. m.
- #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
Compile and run. You can use the debugging tool to obtain the html, as shown in.
Use libcurl in the simulator
The above is an example of running on a device. If you want to use it on a simulator, because the processor structure is different, you need to re-compile the openssl and curl static libraries. During compilation, you only need to change the SDK path from iPhoneOS. platform to iPhoneSimulator. platform. Compile the switch-arch armv6 Plugin-arch i386. The only difference between the compiled file name and the one used on the iphone is as follows:
- libcrypto_simulator.a
- libssl_simulator.a
- libcurl_simulator.a
Or Add a new compilation target without changing the library name.
Summary:IPhoneApplicationsHTTPS ServerI have introduced the content of the connection tutorial. I hope this article will help you!