Detect TestFlight?

2019-01-18 23:57发布

问题:

I would prefer to use the same build configuration for TestFlight vs. App Store. Is there a way to detect at runtime whether the app has been installed via TestFlight or the App Store? (My thinking is I'll only call takeOff if it's not installed via the App Store.)

I want to avoid using TestFlight in App Store builds to protect my users' privacy, and also to avoid the potential derailing of networking discussed here.

回答1:

I believe this to be close enough to a duplicate of Check if iOS app is live in app store that this can be closed.

You can determine if your app was distributed via the app store by checking for the absence of embedded.mobileprovision. This file is only included in adhoc builds. It follows that if you're distributing builds only via TestFlight or HockeyApp and it is not a store build, it must be a TestFlight or HockeyApp build.

Like this:

if ([[NSBundle mainBundle] pathForResource:@"embedded"
                                    ofType:@"mobileprovision"]) {
  // not from app store
} else {
  // from app store
}

This technique is from the HockeyApp SDK.



回答2:

To determine Debug, TestFlight or AppStore deployment in Swift:

  private static let isTestFlight = NSBundle.mainBundle().appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"

  // This can be used to add debug statements.
  static var isDebug: Bool {
    #if DEBUG
      return true
    #else
      return false
    #endif
  }

Complete source and sample: https://stackoverflow.com/a/33830605/639227



回答3:

Here is a blog post that shows you how to add additional configurations besides Debug and Release (ex. Beta).

And after you add Beta configuration, you create another project scheme. And then edit this new scheme. Under section Archive please select to use Beta configuration. Then you use this scheme for archiving for Testflight and previous scheme for achiving for app store.