iOS开发中 workspace 与 static lib 工程的联合使用

在iOS开发中,其实workspace的使用没有完全发挥出来,最近做了一些研究,也想把之前写过的代码整理下,因为iOS里面的布局方式,交互方式也就那么几种。所以,整理好了之后,更能快捷开发,而且能够形成积累。所以把常用的东西封装成lib文件。

我自己的实施过程如下,同时会穿插一些自己参考的文章的链接或者方法。主要参考链接:

http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/#set_the_installation_directory

1、新建一个文件夹,然后新建一个workspace, 将这个workspace放置在这个文件夹中,创建workspace的过程与创建工程类似。

如下是创建的截图:

2、在这个workspace中创建一个static lib 工程

3、在workspace中创建一个empty project

 

4、工程创建完成,后面就有很多事情要做了,配置工程,empty project能够使用 static lib中的代码。

首先,在empty project 中的Build Phases->Link Binary with Libraries 中添加 libTTStyles.a 

然后在 empty project 的Build Settings->User Header Search Paths 里面添加"$(BUILT_PRODUCTS_DIR)” 和“$(BUILT_PRODUCTS_DIR)/static_library_name”两个选项,才能在编译的时候搜索到相关的头文件

然后,在static lib工程的 Other Linker Flags 中添加-ObjC, -all_load, -force_load 这三个选项,至少添加前两个,不然会报错,

这个是Xcode里面的一个bug, Three20里面有这么一段代码:

1 #define TT_FIX_CATEGORY_BUG(name) @interface TT_FIX_CATEGORY_BUG_##name @end \
2 @implementation TT_FIX_CATEGORY_BUG_##name @end

就是为了解决,static lib中的category无法引用 的问题,添加了一个同名的空类。上面最开始的链接里面有这个问题的解答:

The “User Header Search Paths” setting defines the headers available as quoted imports (eg “#import “MyLibraryClass.h”) while the “Header Search Paths” setting defines those headers available as bracketed imports (eg “#import ). I’ve found that Xcode will only autocomplete header names in the quoted form so I always add libraries to the user header search path even though, from my project’s perspective, they might be more appropriate as system level (angle bracketed) libraries.

When using a static library which includes categories we will also have to add the “-ObjC” flag to the “Other Linker Flags” build setting. This will force the linker to load all objective-c classes and categories from the library. If the library contains only categories “-all_load” or “-force_load” may be needed as well. See Technical Q&A QA1490 for a more detailed explanation of these settings.

 

5、配置工程的编译选项,控制编译工程之间的依赖关系。

在,TTProject的 scheme manage里面添加之前建立的lib文件,编译TTProjects的时候,就会先编译 statib lib的内容,然后再编译自身工程

At this point Xcode should have detected this implicit dependency between our app’s project and the static library’s project and have automatically configured our schemes correctly. Unfortunately I haven’t found this to be the case in practice. Instead we will have to edit our current scheme and add the static library’s build target before our app’s build target.

 

总结:用这种方式来创建工程,最大的好处就是,每次每个项目的开发,都能有所积累,到后面,可以达到3--5天即可开发一个App的地步。

后面会把这套lib库开源出来,也是使用了很多第三方的代码,自己做了一些封装和吸收吧。加TT前缀的原因是,Three20那套里面有很多东西都不错。借鉴了一些。但是不像three20那样庞大,不重复造车吧。。

posted on 2013-07-21 00:51  easonoutlook  阅读(5515)  评论(1编辑  收藏  举报