日期:2014-05-20  浏览次数:20915 次

xcode4.3下制作framework

?转自:xcode4.3下制作framework——xcode4.2下也通用)

将自己的类封成库供别人调用,非常方便,就行xcode中的许多类库一样。那么如何制作自己的类库呢?

本人在网上看过很多童鞋的文章,都没有试成功,最后在同事的帮助下搞定。今天有点空闲时间,赶紧记下来,一是跟大家分享,二是怕忘了,今天有同事问我,都感觉有点生了,所以有了这篇文章。

?

下面教大家一步步制作framework

?

?

1、???????? 新建一个项目选择Framwork&Library中的Cocoa Touch Static Library。如图

?

?

?

?

2、???????? 命名项目名称

?



?

?

3、???????? 加载自己要封装的东西



??

?

4、???????? 加载自己的代码

?



?

?

5、???????? 选择设备和模拟器两种方式,编译生成libhello_world.a文件

?



?

?

6、???????? 查看是否生成两个文件

?



?

?

7、???????? 选择Fill——>New——>Taget弹出界面选择Aggregate

?



?

?

8、???????? 命名类库的名称

?



?

?

9、???????? 选择Taget :HelloWorld的Bulid Phases选项卡

?



?

?

10、???? 添加Taget

?



?

?

11、???? 点击又下角Add Build Phase,选择Add Run Script。贴上这段脚本

xcodebuild -project ${PROJECT_NAME}.xcodeproj -sdk iphonesimulator -target ${PROJECT_NAME} -configuration ${CONFIGURATION} clean build

?

xcodebuild -project ${PROJECT_NAME}.xcodeproj -sdk iphoneos -target ${PROJECT_NAME} -configuration ${CONFIGURATION} clean build

?



?

?

12、???? 同样的方法添加另一个脚本,脚本的意思我没有研究,你可以详细看看

SIMULATOR_LIBRARY_PATH="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a" &&

DEVICE_LIBRARY_PATH="${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a" &&

UNIVERSAL_LIBRARY_DIR="${BUILD_DIR}/${CONFIGURATION}-iphoneuniversal" &&

UNIVERSAL_LIBRARY_PATH="${UNIVERSAL_LIBRARY_DIR}/${PRODUCT_NAME}" &&

FRAMEWORK="${UNIVERSAL_LIBRARY_DIR}/${PRODUCT_NAME}.framework" &&

?

# Create framework directory structure.

rm -rf "${FRAMEWORK}" &&

mkdir -p "${UNIVERSAL_LIBRARY_DIR}" &&

mkdir -p "${FRAMEWORK}/Versions/A/Headers" &&

mkdir -p "${FRAMEWORK}/Versions/A/Resources" &&

?

# Generate universal binary for the device and simulator.

lipo "${SIMULATOR_LIBRARY_PATH}" "${DEVICE_LIBRARY_PATH}" -create -output "${UNIVERSAL_LIBRARY_PATH}" &&

?

# Move files to appropriate locations in framework paths.

cp "${UNIVERSAL_LIBRARY_PATH}" "${FRAMEWORK}/Versions/A" &&

ln -s "A" "${FRAMEWORK}/Versions/Current" &&

ln -s "Versions/Current/Headers" "${FRAMEWORK}/Headers" &&

ln -s "Versions/Current/Resources" "${FRAMEWORK}/Resources" &&

ln -s "Versions/Current/${PRODUCT_NAME}" "${FRAMEWORK}/${PRODUCT_NAME}"

?



?

?

13、???? 点击右下角的Add Build Phase,选择Add Copy Files。在Destination选项中选择Absolute Path,在Subpath路径加载路径:${BUILD_DIR}/${CONFIGURATION}-iphoneuniversal/${PRODUCT_NAME}.framework/Versions/A/Headers

?



?

?

14、???? 点击“+按钮”,加载你要封装的.h文件

?



?
?

?



?

?

15、???? 选择Taget: HelloWorld进行编译

?



?

?

16、???? 然后选择libhello_world.a文件,右键选择Show in Finder。Debug-iphoneuniversal就是生成的framework。

?



?

?

17、???? 如果Headers文件夹里面为空,则把你封装的.h文件复制到里面,之后就可以在你的程序中使用了。

18、???? 至此framework制作完成,效果图:

?




?

?

?

?

Technical Q&A QA1490

Building Objective-C static libraries with categories

Q:? Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?

A: Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories?

The "selector not recognized" runtime exception occurs due to an issue between the implementation of standard UNIX static libraries, the linker and the dynamic nature of Objective-C. Objective-C does not define linker symbols for each function (or method, in Objective-C) - instead, linker symbols are only generated for each class. If you extend a pre-existing class with categories, the linker does not know to associate the object code of the core class implementation and the category implementation. This prevents objects created in the resulting application from responding to a selector that is defined in the category.

To resolve this issue, the target linking against the static library must pass the -ObjC option to the linker. This flag causes the linker to load every object file in the library that defines an Objective-C class or category. While this option will typically result in a larger executable (due to additional object code loaded into the application), it will allow the successful creation of effective Objective-C static libraries that contain categories on existing classes. Follow these steps to pass -ObjC to the linker:

  1. In Xcode, double-click the target's name under "Targets" in the Project window.
  2. Choose the Build pane from the ensuing Info window.
  3. Scroll down to the Other Linker Flags build setting under the Linking collection and set its value to -ObjC.

Figure 1??Target Build pane: Other Linker Flags



?

Important:?For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories and no classes. The workaround is to use the -all_load or -force_load flags.

?

-all_load forces the linker to load all object files from every archive it sees, even those without Objective-C code. -force_load is available in Xcode 3.2 and later. It allows finer grain control of archive loading. Each -force_load option must be followed by a path to an archive, and every object file in that archive will be loaded.

?

?

Document Revision History