在iOS系統(tǒng)中使用靜態(tài)鏈接庫
在iOS系統(tǒng)中使用靜態(tài)鏈接庫是本文要介紹的內(nèi)容,主要是來了解并學(xué)習(xí)IOS系統(tǒng)中靜態(tài)鏈接庫的應(yīng)用操作,具體內(nèi)容來看本文詳解。
1、開發(fā)iOS系統(tǒng)下靜態(tài)鏈接庫
打開XCode新建一個(gè)項(xiàng)目,選擇Library下的“CocoaTouchStaticLibrary”并命名為“EncryptLibrary”。這個(gè)新建的靜態(tài)庫項(xiàng)目下除了“EncryptLibrary_Prefix.pch”外沒有任何程序文件,在Classes文件夾上點(diǎn)右鍵選擇“NewFile…”,然后在“CocoaTouchClass”下選擇“Objective-Cclass”,將源文件命名為“Encrypt.m”,同時(shí)選擇生成Encrypt.h頭文件,可以看到在Classes目錄下產(chǎn)生了Encrypt.h和Encrypt.m文件。接著在Encrypt.h頭文件里輸入以下內(nèi)容:
- #import
- @interfaceEncrypt:NSObject{
- }
//對明文的用戶名和密碼進(jìn)行編碼,返回編碼后的字符串
- +(NSString*)EncryptUserNameAndPassword:(NSString*)strUserNamePassword:(NSString*)strPassword;
- @end
實(shí)現(xiàn)文件Encrypt.m內(nèi)容如下:
- #import"Encrypt.h"
- @implementationEncrypt
- +(NSString*)EncryptUserNameAndPassword:(NSString*)strUserNamePassword:(NSString*)strPassword
- {
- NSString*strEncrypted=[NSStringstringWithFormat:@"UserName:%@,Password:%@",strUserName,strPassword];
- ReturnstrEncrypted;
- }
- @end
這里提供了一個(gè)對明文的用戶名和密碼進(jìn)行編碼的函數(shù)。至此,這個(gè)靜態(tài)函數(shù)庫已經(jīng)編寫完畢,編譯這個(gè)程序會看到在Products目錄下產(chǎn)生了名為“libEncryptLibrary.a”的靜態(tài)庫文件。
2、新建項(xiàng)目測試上面開發(fā)的靜態(tài)鏈接庫
新建一個(gè)“Window-basedApplication”項(xiàng)目并命名為“EncryptLibraryTest”,下面演示如何在這個(gè)新項(xiàng)目里利用前面生成的靜態(tài)庫libEncryptLibrary.a文件。
首先打開Finder,將上面編譯生成的libEncryptLibrary.a文件復(fù)制到EncryptLibraryTest.xcodeproj同級目錄,將Encrypt.h復(fù)制到EncryptLibraryTest.xcodeproj同級目錄的Classes文件夾下面,在Xcode中右鍵點(diǎn)Frameworks->Add->ExistingFiles..添加剛才復(fù)制的libEncryptLibrary.a文件,接下來使用靜態(tài)庫中的函數(shù),如下:
- #import
- #import"Encrypt.h"
- @interfaceEncryptLibraryTestAppDelegate:NSObject{
- UIWindow*window;
- }
- @property(nonatomic,retain)IBOutletUIWindow*window;
- @end
修改相應(yīng)的實(shí)現(xiàn)文件如下:
- #import"EncryptLibraryTestAppDelegate.h"
- @implementationEncryptLibraryTestAppDelegate
- @synthesizewindow;
- -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
- //Overridepointforcustomizationafterapplaunch.
- [self.windowaddSubview:viewController.view];
- [self.windowmakeKeyAndVisible];
- NSString*strUserName=@”caijinhui”;
- NSString*strPassWord=@”password”;
- NSString*strEncrypted=[EncryptEncryptUserNameAndPassword:strUserNamePassword:strPassWord];
- NSLog(@”%@”,strEncrypted);
- returnYES;
- }
- -(void)dealloc{
- [windowrelease];
- [superdealloc];
- }
- @end
編譯一下,順利通過,在Console輸出編碼后的字符串。
提示:因?yàn)楸疚臋n是用Office2007寫的,所以在Mac系統(tǒng)下用文本編輯器打開,會出現(xiàn)部門不正常字符,特別是程序中一些雙引號,若編譯出錯(cuò),請更改相關(guān)雙引號。
小結(jié):在iOS系統(tǒng)中使用靜態(tài)鏈接庫的內(nèi)容介紹完了,希望通過IOS系統(tǒng)中鏈接庫的應(yīng)用內(nèi)容的學(xué)習(xí)能對你有所幫助。