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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With