IPhone application HTTPS server connection tutorial

Source: Internet
Author: User

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.

 
 
  1. libcrypto.a  
  2. 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 ).

 
 
  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 

If the following content is output, you can compile libcurl that supports https.

 
 
  1. SSL support:     enabled (OpenSSL) 

Next

 
 
  1. make  
  2. 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.

 
 
  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 

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:

 
 
  1. libcrypto_simulator.a  
  2. libssl_simulator.a  
  3. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.