Creating and Using Static Libraries for iPhone using Xcode 4.3

简介:

Recently, after developing a collection of applications for iPhone that were intended to be used as libraries, I began looking into how to convert Xcode projects into static libraries. Most of what I found was vague, unclear, and outdated. Instructions were either for older versions of Xcode, or assumed that you were creating a blank library, and would be coding after creating a new project.

So, I read through several tutorials, and believe that I have come up with the best way to convert a finished, working project into a library for use.

Creating a static library

First, you need to create a new Xcode project. From the template selection screen, choose Cocoa Touch Static Library under Framework & Library for iOS.

You can give the project whatever descriptive name you would like, and you can change the name of the resulting .a (archive) file to whatever you want in the end. Select where you want to create the project and press create.

Next, remove the .m and .h file that were auto-created with the project. They should be named the same as the newly created project.

Next, select the files from your original project that you want included in the library, and move them to the library project. Be sure to include the .m and .h file for each file wanted, as well as any .plist or other resources, such as libraries, that the files need or reference. When copying the files, be sure to select the check box under Add to Targets to ensure that all the imported .m files will be included in the library.

At this point, you should check that the prefix file of the library project has all of the necessary imports, to prevent build errors within your library.

Now, we want to build the library for both device and simulator, starting with the device. To build the library for a device, either select a connected device from the device list or select iOS device from the list, as below.

Then build the library. If the build was successful, the filename of the .a file in the Products folder should now be black.

To build the library for a simulator, select a valid simulator from the device list, and build the library again.

Next, we are going to locate our newly created libraries. You can right click the created library under Products and select show in finder to open the folder containing the device build.

The enclosing folder is the Products folder of the library project, and contains two folders, one for device, and one for simulator.

The next step is to combine these two libraries into one “fat” library that can be used for device and simulator. The easiest way to do this is to copy the two libraries to another folder, renaming them to distinguish them.

From the terminal, navigate to the folder containing the libraries and run the following command, substituting the names of your libraries:

lipo -create libPictureTouchLibraryDevice.a libPictureTouchLibrarySimulator.a -output libPictureTouchLibrary.a```

This will create a new library in the same folder that can be used for either devices or simulators.

Finally, to distribute the library, we need to include the .h files and any resources, such as plists, that will be used in the library. These files, along with the “fat” .a file are allyou need to implement the library.

<img src='/blog/349/files/CreateLibrary7.png' />

You now have a static library, ready to be added to any Xcode project.

#Adding a static library to a project#

First, if you created the library as mentioned above, and are adding the newly created library back to the project it originated from, be sure to remove any .h or .m files that are included in the library before adding it.

<img src='/blog/349/files/CreateLibrary8.png' />

Next, add the .a file and all resource files to the project. Be sure to select the check box under Add to targets to add it to the main target of the application.

<img src='/blog/349/files/CreateLibrary9.png' />

Otherwise, you will have to add the library under the build settings of the project. The library should now be working! You can now reference it anywhere within your project.
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!














本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/p/3321074.html ,如需转载请自行联系原作者





相关文章
|
6月前
|
iOS开发 芯片 MacOS
[Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture...
[Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture...
131 0
|
9月前
|
iOS开发
Xcode 12.3 编译提示Building for iOS Simulator, but the linked and embedded framework
Xcode 12.3 编译提示Building for iOS Simulator, but the linked and embedded framework
|
iOS开发
未解决:configure: error: XCode tool ‘metal‘ neither found in path nor with xcrunchecking for metal...
未解决:configure: error: XCode tool ‘metal‘ neither found in path nor with xcrunchecking for metal...
122 0
Xcode12适配The linked library is missing one or more architectures required by thi
升级到Xcode12后,运行Release模式后,会提示以下信息
162 0
安装APK时弹窗提示“Detected problems with app native libraries”
安装APK时弹窗提示“Detected problems with app native libraries”
98 0
|
移动开发 JavaScript weex
weex-platform里iOS项目打包报错/platforms/ios/../../start does not exist.
weex-platform里iOS项目打包报错/platforms/ios/../../start does not exist.
62 0
weex-platform里iOS项目打包报错/platforms/ios/../../start does not exist.
Xcode12.x报错: ld: library not found for -lstdc++.6.0.9
错误信息: ld: library not found for -lstdc++.6.0.9 报错原因:Xcode10移除了动态库libstdc
240 0
|
iOS开发
XCode出现Could not locate device support files的解决办法
XCode出现Could not locate device support files的解决办法
653 0
|
iOS开发
Xcode真机测试could not find developer disk image解决方法
Xcode真机测试could not find developer disk image解决方法
123 0
Xcode真机测试could not find developer disk image解决方法
|
开发工具 iOS开发 Perl
Xcode10再坑之framework not found CoreServices for architecture armv7
1、前言 前段时间,升级了 Xcode10 后,提供给第三方用户的SDK,客户反馈说报错: ld: framework not found CoreServices for architecture armv7 为什么 iOS SDK 拖入项目中就报错"ld: framework not fou...
4629 0