Creating dynamic libraries and calling methods in iOS

Source: Internet
Author: User

Last year because of the need to use the dynamic library, I found a lot of information, and finally found a set of methods, how to create and use the dynamic library, record:

Xcode provides the ability to create a static library in iOS engineering, and the ability to create dynamic and static libraries on your Mac.

However, there is no ability to create a dynamic library in the iOS project (Apple does not allow dynamic library links in the program, such programs are AppStore rejected), such as:

Since Apple does not support the creation of an iOS dynamic library, to create a dynamic library, you first need to modify the Xcode profile to support the ability to create an iOS dynamic library.

After research and inquiry online some information, and after their own test success, the following is the modification plan.

  1. open 2 Directories in Finder: (Note If you have more than one Xcode,xcode name, change the following xcode.app to your Xcode name: For example, if the name on the computer is Xcode5.1, the corresponding change is Xcode5.1.app)

    Catalogue ONE:/applications/xcode.app/contents/developer/platforms/iphoneos.platform/developer/library/xcode/ specifications/ 

    where directory one corresponds to ios project, while directory two o'clock xcode creates a macosx profile. (This is only said ios real version, simulator version similar, /applications/xcode.app/contents/developer/platforms/ Iphonesimulator.platform)

  2. Copy the contents of the Iphoneospackagetypes.xcspec and Iphoneosproducttypes.xcspec to the desktop (or any other place with write permission) and open it with Xcode, respectively. Then use Xcode to open the MacOSX package Types.xcspec and MacOSX Product types.xcspec under directory two.

    Identifier The MACOSX package Types.xcspec as a com.apple.package-type.mach-o-dylib item, such as

Drag it to Iphoneospackagetypes.xcspec and make Iphoneospackagetypes.xcspec into:

Then MacOSX Product Types.xcspec in identifier as the com.apple.product-type.library.dynamic item, as

Drag it to Iphoneosproducttypes.xcspec and make Iphoneosproducttypes.xcspec into:

Save the above changes, copy Iphoneospackagetypes.xcspec and iphoneosproducttypes.xcspec back to catalogue one,

At this point, because there is no write permission to prompt for authentication, enter the password.

3. Modify the Create project template

Open the directory in the Finder:

/applications/xcode.app/contents/developer/platforms/iphoneos.platform/developer/library/xcode/templates/ Project templates/framework & Library/cocoa Touch Static library.xctemplate

Copy the entire folder above to the desktop and rename the folder to Cocoa Touch Dynamic library.xctemplate,

Use Xcode to open the templateinfo.plist inside, modify three places:

    

Save and copy the Cocoa Touch Dynamic library.xctemplate back to the directory:

/applications/xcode.app/contents/developer/platforms/iphoneos.platform/developer/library/xcode/templates/ Project Templates/framework & library/

Then restart Xcode to see that you can create an iOS dynamic library:

    

Above is to modify the Xcode configuration so that it can directly produce a dynamic library, the following is a brief talk about the use of dynamic libraries:

When using a dynamic library, you typically do not add the dynamic library directly to the project, but instead import the dynamic library into the Documents folder under the generated app app.

use dynamic libraries in your project to introduce two header files #import<objc/runtime.h> and #import <dlfcn.h>then use the following methods in the project://get the path to the dynamic librarynsarray* 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]; //use Dlopen to open a dynamic library and return a handle    void*lib_handle =Dlopen ([Destlibpath cstringusingencoding:nsutf8stringencoding], rtld_local); if(!lib_handle) {NSLog (@"Unable to open library:%s\n", Dlerror ()); return; }        //class for obtaining a dynamic libraryClass Test_class = nsclassfromstring (@"Test1"); if(!Test_class) {NSLog (@"Unable to get Testdylib class"); return; }        //method to create a class instance and invoke a dynamic library @selector test as a method name in a dynamic librarynsobject* test = [Test_classNew];        [Test Performselector: @selector (test)]; //Close the library.    if(Dlclose (lib_handle)! =0) {NSLog (@"Unable to close library:%s\n", Dlerror ()); Note: When using dynamic libraries, it is important to note that the certificate that generated the dynamic library and the certificate in the test demo are the same certificate. 

Here are some useful information:

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

Creating dynamic libraries and calling methods in iOS

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.