rutini
rutini

Reputation: 5

Swift flags not working

I'm developing an app that I wan't to make in a free version with an ad banner and a paid version without an ad banner.

My idea is to use an #if statement with a flag to indicate when to display the ad banner.

What I've done is the following:

Created two targets one for the paid version and one for the free version. Created a flag for the free version target like this:

Other Swift Flags

In the viewDidLoad method I added the following code:

    #if FREE
    bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
    self.view.addSubview(bannerView)
    bannerView.frame = CGRect(x: 0.0, y: self.view.frame.size.height - bannerView.frame.size.height, width: bannerView.frame.size.width, height: bannerView.frame.size.height)
    bannerView.adUnitID = ...
    bannerView.rootViewController = self
    bannerView.delegate = self
    bannerView.load(GADRequest())
    #endif

When I choose the free version and run the code it won't show the ad banner. Any help is appreciated, thanks.

Upvotes: 0

Views: 3651

Answers (1)

shim
shim

Reputation: 10116

By default Xcode runs builds in the Debug build configuration.

If you would like this to work as is when running from Xcode, then either add the flag to the Debug scheme in "Other Swift Flags" or change your scheme to Release (Product > Scheme > Edit Scheme > Run > Build Configuration -- change to Release — I would recommend changing it back after though, not leaving the Run scheme pointing to Release since that would make debugging more difficult.)

If you are making an app with a free / paid version, you might want to consider looking into in-app purchases as dfd mentioned in the comments. It is not inconceivable that a developer would want to use Swift flags like this. However, if you're going to be making a single app in Xcode, but distributing it to the App Store as two separate apps, that could be a bit annoying to handle via flags like this.

Upvotes: 1

Related Questions