【问题标题】:How to identify the app running environment including Test Flight如何识别包括Test Flight在内的应用运行环境
【发布时间】:2019-11-26 17:02:02
【问题描述】:

我正在开发 iOS 应用,需要识别应用运行的环境以对 API 端点进行分类。我想知道该应用程序是否在生产、模拟器和试飞下运行。 我已经通过用户定义的设置对生产和模拟器进行了分类,但我仍然不确定如何识别试飞。 有小费吗?谢谢!

【问题讨论】:

标签: ios xcode testflight


【解决方案1】:

如果您要求从应用内获取此信息,您可以从appStoreReceiptURLNSBundle 获取所有这些信息

来自apple documentation...

对于从 App Store 购买的应用程序,请使用此应用程序捆绑属性来查找收据。此属性不保证 URL 上是否存在文件,仅保证如果存在收据,则说明其位置。

NSBundle.mainBundle().appStoreReceiptURL?.lastPathComponent

具体实现请参考this问题

【讨论】:

  • 谢谢! @penkey-suresh 让receiptURL = NSBundle.mainBundle().appStoreReceiptURL!让receiptURLString =receiptURL.path!让 isRunningTestFlightBeta = (receiptURLString.rangeOfString("sandboxReceipt") != nil) 工作!
  • 如果它解决了您的问题,请考虑接受答案:)
  • 很抱歉。只是想添加评论以感谢您!
【解决方案2】:

还可以使用收据字段中的environment 字段。请查看所附截图以获取sandboxproduction 收据。

【讨论】:

    【解决方案3】:

    Swift 5. 使用下面的 WhereAmIRunning 类来检查环境。

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-16
      • 1970-01-01
      • 2022-12-11
      • 2012-12-26
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多