標籤:
去年因需要用到動態庫,自己就找了好多一些 資料,最終找到了一套方法,怎麼建立與使用動態庫,記錄一下:
Xcode提供了在iOS工程中建立靜態庫的功能,和在MAC上建立動態庫和靜態庫的功能。
但是沒有提供在iOS工程中建立動態庫的功能(蘋果官方不允許程式中存在動態庫連結,這樣的程式會被AppStore拒),如:
由於蘋果不支援自己建立iOS動態庫,所以要想建立動態庫首先要修改Xcode的設定檔使其支援具備建立iOS 動態庫的功能,
經過調研和查詢網上的一些資料,並經過自己測試成功,以下是修改方案。
- 在Finder中開啟2個目錄:(注意若裝有多個Xcode,Xcode命名不同的話要把下面的Xcode.app改為自己Xcode的名字:如,電腦上的名字為Xcode5.1,則對應改為xcode5.1.app)
目錄一:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Specifications/
目錄二:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/
其中目錄一對應的是Xcode下建立iOS工程的設定檔,而目錄二時Xcode下建立MacOSX的設定檔。(這裡只說iOS真機版,模擬器版類似,/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform)
- 把目錄一下的iPhoneOSPackageTypes.xcspec和iPhoneOSProductTypes.xcspec拷貝到案頭(或者其它有寫入權限的地方),分別用xcode開啟。再用xcode開啟目錄二下的MacOSX Package Types.xcspec和MacOSX Product Types.xcspec
把MacOSX Package Types.xcspec中Identifier為com.apple.package-type.mach-o-dylib的item,如
拖到iPhoneOSPackageTypes.xcspec,令iPhoneOSPackageTypes.xcspec變成:
再把MacOSX Product Types.xcspec中Identifier為com.apple.product-type.library.dynamic的item,如
拖到iPhoneOSProductTypes.xcspec,令iPhoneOSProductTypes.xcspec變成:
儲存以上修改,把iPhoneOSPackageTypes.xcspec和iPhoneOSProductTypes.xcspec拷貝回去目錄一,
此時會因為沒有寫入權限而提示需要評鑑,輸入密碼即可。
3. 修改建立工程模板
在Finder中開啟目錄:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/Framework & Library/Cocoa Touch Static Library.xctemplate
把以上整個檔案夾複製到案頭,檔案夾改名為Cocoa Touch Dynamic Library.xctemplate,
用xcode開啟裡面的TemplateInfo.plist,修改三個地方:
儲存,然後把Cocoa Touch Dynamic Library.xctemplate拷貝回目錄:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/Framework & Library/
然後重啟Xcode即可看到能建立iOS動態庫了,:
上面就是修改xcode配置可以使其直接生產動態庫,下面簡單說一下動態庫的用法:
使用動態庫時,一般不是直接把動態庫添加到項目工程中,而是把動態庫匯入到產生的APP應用下的Documents檔案夾下。
在工程中使用動態庫要引入兩個標頭檔#import <objc/runtime.h> 和 #import <dlfcn.h> 然後在工程中使用如下方法: // 擷取動態庫的路徑 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentDirectory = nil; if ([paths count] != 0) documentDirectory = [paths objectAtIndex:0]; NSString *libName = @"Test1.dylib"; NSString *destLibPath = [documentDirectory stringByAppendingPathComponent:libName]; //使用dlopen開啟動態庫,返回一個控制代碼 void *lib_handle = dlopen([destLibPath cStringUsingEncoding:NSUTF8StringEncoding], RTLD_LOCAL); if (!lib_handle) { NSLog(@"Unable to open library: %s\n", dlerror()); return; } // 獲得動態庫的類 Class Test_class = NSClassFromString(@"Test1"); if (!Test_class) { NSLog(@"Unable to get TestDylib class"); return; } // 建立類執行個體 並調用動態庫的方法 @selector(test) 中的test為動態庫中的方法名 NSObject* test = [Test_class new]; [test performSelector:@selector(test)]; // Close the library. if (dlclose(lib_handle) != 0) { NSLog(@"Unable to close library: %s\n",dlerror()); }注意: 在使用動態庫時,一定要注意產生動態庫的認證和測試Demo中的認證是同一個認證。
下面是一些借鑒的資料:
http://blog.csdn.net/hursing/article/details/8951958
http://blog.csdn.net/stackhero/article/details/9032891
http://blog.csdn.net/stackhero/article/details/9032999
http://www.cocoachina.com/bbs/read.php?tid=136750&fpage=49
http://stackoverflow.com/questions/4733847/can-you-build-dynamic-libraries-for-ios-and-load-them-at-runtime
iOS中建立動態庫及調用方法