Use static Link Library for iOS (basic)

Source: Internet
Author: User
1. Develop a static Link Library for iOS

Open xcode to create a project and select "cocoa" under the library
Touch static library and name it "encryptlibrary ". This new static library project has no program files except "encryptlibrary_prefix.pch". Right-click the classes folder and choose "New"
File ...", Select "objective-C Class" under "cocoa touch class" and name the source file "encrypt. m ", and select generate encrypt. h header file. You can see that encrypt is generated under the classes directory. H and encrypt. M file. Enter the following content in the encrypt. h header file:

 

# Import <Foundation/Foundation. h>

@ Interface encrypt: nsobject {

}

// Encode the plaintext user name and password and return the encoded string

+ (Nsstring *) encryptusernameandpassword :( nsstring *) strusername password :( nsstring *) strpassword;

@ End

 

The content of the implementation file encrypt. m is as follows:

 

# Import "encrypt. H"

@ Implementation Encrypt

+ (Nsstring *) encryptusernameandpassword :( nsstring *) strusername password :( nsstring *) strpassword

{

Nsstring * strencrypted = [nsstring stringwithformat: @ "username: % @, password: % @", strusername, strpassword];

Return strencrypted;

}

@ End

 

Here we provide a function to encode the plaintext user name and password. So far, this static function library has been compiled. Compile this program and you will see a static library file named "libencryptlibrary. A" is generated under the products directory.

 

2. Create a static link library developed in the Project Test

Create a new "window-based application" project named "encryptlibrarytest". The following shows how to use the previously generated static library libencryptlibrary. A file in this new project.

First open the finder and translate the libencryptlibrary generated above. copy file a to encryptlibrarytest. xcodeproj directory of the same level, encrypt. copy h to encryptlibrarytest. under the classes folder in the same directory of xcodeproj, right-click frameworks> Add> existing in xcode.
Files .. add the copied libencryptlibrary. A file, and then use the functions in the static library, as shown below:

# Import <uikit/uikit. h>

# Import "encrypt. H"

 

@ Interface encryptlibrarytestappdelegate: nsobject <uiapplicationdelegate> {

Uiwindow * window;

}

 

@ Property (nonatomic, retain) iboutlet uiwindow * window;

 

@ End

Modify the implementation file as follows:

# Import "encryptlibrarytestappdelegate. H"

 

@ Implementation encryptlibrarytestappdelegate

@ Synthesize window;

 

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {

// Override point for customization after app launch.

[Self. Window addsubview: viewcontroller. View];

[Self. Window makekeyandvisible];

Nsstring * strusername = @ "caijinhui ";

Nsstring * strpassword = @ "Password ";

Nsstring * strencrypted = [encrypt encryptusernameandpassword: strusername password:
Strpassword];

Nslog (@ "% @", strencrypted );

Return yes;

}

-(Void) dealloc {

[Window release];

[Super dealloc];

}

@ End

Compile the code and pass it smoothly. The encoded string is output on the console.

Note: Because this document is written in Office 2007, opening it in a text editor on the Mac system will result in abnormal Department characters, especially double quotation marks in the program. If compilation fails, please change the double quotation marks.

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.