Use static Link Library in iOS

Source: Internet
Author: User

InIOS systemUsed inStatic Link LibraryIs the content to be introduced in this article, mainly to understand and learnIOS systemMediumStatic Link LibraryFor more information, see this article.

1. Develop a static Link Library for iOS

Open XCode to create a project, select "CocoaTouchStaticLibrary" under the Library, and name it "EncryptLibrary ". This newly createdStaticIn the library project, there are no program files except "EncryptLibrary_Prefix.pch". Right-click the Classes folder and choose "NewFile ...", Select "Objective-Cclass" under "CocoaTouchClass" 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:

 
 
  1. #import  
  2. @interfaceEncrypt:NSObject{  

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

 
 
  1. +(NSString*)EncryptUserNameAndPassword:(NSString*)strUserNamePassword:(NSString*)strPassword;  
  2. @end 

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

 
 
  1. #import"Encrypt.h"  
  2. @implementationEncrypt  
  3. +(NSString*)EncryptUserNameAndPassword:(NSString*)strUserNamePassword:(NSString*)strPassword  
  4. {  
  5. NSString*strEncrypted=[NSStringstringWithFormat:@"UserName:%@,Password:%@",strUserName,strPassword];  
  6. ReturnstrEncrypted;  
  7. }  
  8. @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-basedApplication" project and name it "EncryptLibraryTest". The following shows how to use the previously generated static library libEncryptLibrary. a file in the 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> ExistingFiles in Xcode .. add the copied libEncryptLibrary. file a, and then use the functions in the static library as follows:

 
 
  1. #import  
  2. #import"Encrypt.h"  
  3. @interfaceEncryptLibraryTestAppDelegate:NSObject{  
  4. UIWindow*window;  
  5. }  
  6. @property(nonatomic,retain)IBOutletUIWindow*window;  
  7. @end 

Modify the implementation file as follows:

 
 
  1. #import"EncryptLibraryTestAppDelegate.h"  
  2. @implementationEncryptLibraryTestAppDelegate  
  3. @synthesizewindow;  
  4. -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{  
  5. //Overridepointforcustomizationafterapplaunch.  
  6. [self.windowaddSubview:viewController.view];  
  7. [self.windowmakeKeyAndVisible];  
  8. NSString*strUserName=@”caijinhui”;  
  9. NSString*strPassWord=@”password”;  
  10. NSString*strEncrypted=[EncryptEncryptUserNameAndPassword:strUserNamePassword:strPassWord];  
  11. NSLog(@”%@”,strEncrypted);  
  12. returnYES;  
  13. }  
  14. -(void)dealloc{  
  15. [windowrelease];  
  16. [superdealloc];  
  17. }  
  18. @end 

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

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

Summary: InIOS systemUsed inStatic Link LibraryThe contentIOS systemMediumLink LibraryThe learning of application content is helpful to you.

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.