Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - preprocessor macros and class declarations

Tags:

ios

swift

So I'm following a tutorial that shows how to use the same codebase to build both a FREE and PAID version of an app.

It shows how to set up a DEFINE for "FREE" and I've got all that stuff working fine. What seems to be glossed over, is how to deal with this class definition. For the FREE app, it needs to be delegates for GADBannerViewDelegate, GADInterstitialDelegate, while the PAID version does not.

So I was hoping to set up something like this:

#if FREE
class GameViewController:    UIViewController, GKGameCenterControllerDelegate,  GADBannerViewDelegate, GADInterstitialDelegate {
#else
class GameViewController:    UIViewController, GKGameCenterControllerDelegate{
#endif

But that's not valid, and I after reading the docs, I understand why. What I don't understand, is how to deal with this. As a workaround, I figured I'd just make the PAID version have those GoogleAd delegates as well. But then Apple rejected my app, because including that makes it look like my app is serving up ads, when it isn't, and that's against the rules.

So for now, whenever I build the PAID vs FREE app, I'm just manually editing the code to change the declaration. It works, but is dumb. There's got to be a way to automate this, right?

like image 767
user3302800 Avatar asked Sep 11 '15 04:09

user3302800


People also ask

What are preprocessor macros in SwiftUI?

Preprocessor macros are used to bring context to a build, allowing you to transform how your app is compiled depending on why it's being built. Stoked about SwiftUI but hesitant to start using it in production? Build better apps faster. Moving Parts is a library of high-level production-ready SwiftUI components, made to solve real-world problems.

What is the use of preprocessor macros?

As the prefix "pre" implies, the value of a macro is tested before your code is compiled, meaning that everything in the "else" block of a preprocessor macros will not be present in the final binary. For things like debug views and mocked components, this is an amazing feature.

What is the use of if in Swift?

Flags and environment checks Perhaps the most commonly used Swift compiler directive is the #if command, which enables us to conditionally include or exclude certain code blocks when our program is being compiled.

What are compiler directives in Swift?

Perhaps the most commonly used Swift compiler directive is the #if command, which enables us to conditionally include or exclude certain code blocks when our program is being compiled.


1 Answers

You can use extension to implement protocols:

class GameViewController: UIViewController, GKGameCenterControllerDelegate {

    #if FREE
    var bannerView: GABannerView!
    var interstitial: GADInterstitial!
    #endif

    override func viewDidLoad() {
        super.viewDidLoad()

        #if FREE
        bannerView = GADBannerView(adSize:kGADAdSizeBanner)
        bannerView.delegate = self

        interstitial = GADInterstitial(adUnitID: "...")
        interstitail.delegate = self

        // ... other FREE stuff
        #endif

        // common stuff
    }
}

#if FREE
extension GameViewController: GADBannerViewDelegate, GADInterstitialDelegate {
    // Ad delegate methods
}
#endif
like image 135
rintaro Avatar answered Sep 23 '22 02:09

rintaro