Determine whether app is communicating with APNS sandbox or production environment

时光总嘲笑我的痴心妄想 提交于 2019-11-30 00:21:24
tcurdt

You can read and check the embedded provisioning profile.

https://github.com/tcurdt/TCMobileProvision

This is what I do:

NSString *mobileprovisionPath = [[[NSBundle mainBundle] bundlePath]
        stringByAppendingPathComponent:@"embedded.mobileprovision"];
TCMobileProvision *mobileprovision = [[TCMobileProvision alloc] initWithData:[NSData dataWithContentsOfFile:mobileprovisionPath]];
NSDictionary *entitlements = mobileprovision.dict[@"Entitlements"];
NSString *apsEnvironment = entitlements[@"aps-environment"];
BOOL production = entitlements && apsEnvironment && [apsEnvironment isEqualToString:@"production"];

This is a hack but its working on XCode 8 with Swift 3

We're basically opening the embedded.mobileprovision file, converting it to a string, then checking for a string that would indicate the app is using the development aps-environment.

func isDevelopmentEnvironment() -> Bool {
    guard let filePath = Bundle.main.path(forResource: "embedded", ofType:"mobileprovision") else {
        return false
    }
    do {
        let url = URL(fileURLWithPath: filePath)
        let data = try Data(contentsOf: url)
        guard let string = String(data: data, encoding: .ascii) else {
            return false
        }
        if string.contains("<key>aps-environment</key>\n\t\t<string>development</string>") {
            return true
        }
    } catch {}
    return false
}
Wiz
  1. The APNS environment is determined according to the code sign Entitlements matching your Code sign identity (good post here) - while identifying your build configuration may work, it may also be false if you've matched that build configuration with a mis-matched entitlement.

  2. Keeping that in mind, using DEBUG as a mean to determine your entitlements should work (if you find DEBUG to be tricky, you can add a your own linker flag under "Apple LLVM..." -> "Other C Flags" -> "Debug") for example, add -DDEBUGGING and then use:

#ifdef DEBUGGING BOOL isProd = YES; #else BOOL isProd = NO; #endif

As mentioned in @tcurdt's answer, the only safe way to determine whether to use the sandbox or not is to check the provisioning file. Here is the Swift code, using TCMobileProvision:

func isAPNSandbox() -> Bool {
    if let mobileProvisionURL = NSBundle.mainBundle().URLForResource("embedded", withExtension: "mobileprovision"),
    let mobileProvisionData = NSData(contentsOfURL: mobileProvisionURL),
    let mobileProvision = TCMobileProvision(data: mobileProvisionData) {
        if let entitlements = mobileProvision.dict["Entitlements"],
        let apsEnvironment = entitlements["aps-environment"] as? String
        where apsEnvironment == "development" {
            return true
        }
    }

    return false
}

To install TCMobileProvision, add this to your Podfile:

pod 'TCMobileProvision', :git => 'https://github.com/tcurdt/TCMobileProvision.git'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!