時常能看到同一個有分付費版與免費版,但是又不想之後維護兩種版本的程式碼,該怎麼做呢?設定版本 (Set Version)還滿有技巧性的~
選擇 target -> Build Settings -> 搜尋 “preprocessor Macros"
在此我在Tarot with ad這個target的Debug和Release多定義AD=1。如此一來,只要在APP共用的AppDelegate加上判斷版本的method,即可讓所有Class使用。
TRAppDelegate.m定義這兩個method:
+(TRAppDelegate *)sharedAppDelegate { return (TRAppDelegate *)[[UIApplication sharedApplication] delegate]; } -(BOOL)hasAd { #ifdef AD return YES; #else return NO; #endif }
TRAppDelegate.h宣告這一個method,以便讓其它class呼叫:
+(TRAppDelegate *)sharedAppDelegate;
至於為何不宣告另一個method?因為透過上述method,Xcode可以自己找到另一個method來使用。
其它class只要#import “TRAppDelegate.h"即可使用該method來判斷版本。
-(void)versionInitial { if ([TRAppDelegate sharedAppDelegate].hasAd) { //廣告版設定 } else { //非廣告版設定 } }
參考:老闆的夥伴、iOS Multiple target in the same project。
隨意留個言吧:)~