How to create multiple apps from same shared code in Xcode?

前端 未结 1 692
礼貌的吻别
礼貌的吻别 2020-12-24 07:47

I\'m developing 2 different apps that share 95% of the same code and views. What is the best way to go about this using Xcode?

1条回答
  •  生来不讨喜
    2020-12-24 08:30

    Use targets. That's exactly what they are for.

    Learn more about the concept of targets here.

    Typically, the majority of projects have a single Target, which corresponds to one product/application. If you define multiple targets, you can:

    • include some of your source code files (or maybe all) in both targets, some in one target and some in the other
    • you can play with Build Settings to compile the two targets using different settings.

    For example you may define Precompiler Macros for one target and other macros for the other (let's say OTHER_C_FLAGS = -DPREMIUM in target "PremiumVersion" and OTHER_C_FLAGS = -DLITE to define the LITE macro in the "LiteVersion" target) and then include similar code in your source:

    -(IBAction)commonCodeToBothTargetsHere
    {
       ...
    }
    
    -(void)doStuffOnlyAvailableForPremiumVersion
    {
    #if PREMIUM
       // This code will only be compiled if the PREMIUM macro is defined
       // namely only when you compile the "PremiumVersion" target
       .... // do real stuff
    #else
       // This code will only be compiled if the PREMIUM macro is NOT defined
       // namely when you compile the "LiteVersion" target
    
       [[[[UIAlertView alloc] initWithTitle:@"Only for premium" 
           message:@"Sorry, this feature is reserved for premium users. Go buy the premium version on the AppStore!"
           delegate:self
           cancelButtonTitle:@"Doh!"
           otherButtonTitles:@"Go buy it!",nil]
       autorelease] show];
    #endif
    }
    
    -(void)otherCommonCodeToBothTargetsHere
    {
       ...
    }
    

    0 讨论(0)
提交回复
热议问题