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那樣龐大,不重復造車吧。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM