1

I'm developing iOS app and need to identify the environment where the app is running to classify the API endpoint. I want to know the app is running under whether production, simulator and also Test Flight. I've already done classifying production and simulator by User-defined setting, but am still not sure how I can identify Test Flight. Any tips? thanks!

Dave
  • 405
  • 4
  • 17
  • Possible duplicate of [How to tell at runtime whether an iOS app is running through a TestFlight Beta install](http://stackoverflow.com/questions/26081543/how-to-tell-at-runtime-whether-an-ios-app-is-running-through-a-testflight-beta-i) – combinatorial May 16 '16 at 03:06

3 Answers3

2

If you are asking to get this information from within the app, you can get all this from appStoreReceiptURL of NSBundle

From apple documentation...

For an application purchased from the App Store, use this application bundle property to locate the receipt. This property makes no guarantee about whether there is a file at the URL—only that if a receipt is present, that is its location.

NSBundle.mainBundle().appStoreReceiptURL?.lastPathComponent

For implementation refer to this question

Community
  • 1
  • 1
Penkey Suresh
  • 5,816
  • 3
  • 36
  • 55
  • 1
    Thanks! @penkey-suresh let receiptURL = NSBundle.mainBundle().appStoreReceiptURL! let receiptURLString = receiptURL.path! let isRunningTestFlightBeta = (receiptURLString.rangeOfString("sandboxReceipt") != nil) worked! – Dave May 15 '16 at 14:32
  • Do consider accepting the answer if it solves your issue :) – Penkey Suresh May 15 '16 at 14:33
  • Sorry about that. Just wanted to add comment to thank you! – Dave May 15 '16 at 14:35
2

Also can use environment field from receipt fields. Please check the attached screenshot for sandbox and production receipts. enter image description here enter image description here

Ashvin
  • 8,227
  • 3
  • 36
  • 53
1

Swift 5. Use the below WhereAmIRunning class to check the environment.

import Foundation

class WhereAmIRunning {

    // MARK: Public

    func isRunningInTestFlightEnvironment() -> Bool{
        if isSimulator() {
            return false
        } else {
            if isAppStoreReceiptSandbox() && !hasEmbeddedMobileProvision() {
                return true
            } else {
                return false
            }
        }
    }

    func isRunningInAppStoreEnvironment() -> Bool {
        if isSimulator(){
            return false
        } else {
            if isAppStoreReceiptSandbox() || hasEmbeddedMobileProvision() {
                return false
            } else {
                return true
            }
        }
    }

    // MARK: Private

    private func hasEmbeddedMobileProvision() -> Bool{
      if let _ = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision") {
            return true
        }
        return false
    }

    private func isAppStoreReceiptSandbox() -> Bool {
        if isSimulator() {
            return false
        } else {
          if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
            let appStoreReceiptLastComponent = appStoreReceiptURL.lastPathComponent as? String, appStoreReceiptLastComponent == "sandboxReceipt" {
                    return true
            }
            return false
        }
    }

    private func isSimulator() -> Bool {
        #if arch(i386) || arch(x86_64)
            return true
            #else
            return false
        #endif
    }
}
Gurjinder Singh
  • 9,221
  • 1
  • 66
  • 58