简体   繁体   中英

Is app running on test device/simulator/real app store install

Is there a way to find out during runtime if the app is currently running on a test-device/simulator or from a real app store installation?

I need to implement ad-tracking into the application and during testing/debugging I do not want to send tracking data because this changes the statistics.

But I do not want just to distinguish between DEBUG/RELEASE builds because RELEASE builds are eg also used during profiling.

Maybe there is a config set somewhere if the app is from a real app store installation? Or any other way?

Thanks.

You can add following category on UIDevice to check whether it is running on device or simulator.

- (BOOL)isSimulator {
#if TARGET_IPHONE_SIMULATOR
    return true;
#else
    return false;
#endif  
}


+ (BOOL)isSimulator {
    return [[self currentDevice] isSimulator];
}

Edit: I just explored and find out that there is a way to know whether application instance is from app store or not. Here it is:

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

You can refer to this SO thread for more details.

If you create separate configuration for your debug, distribution and app store builds (which you ideally should) you can add User-Defined Setting' with different value for each configuration. This can be exposed to the code as a bundle property by exporting it through info.plist file and you can write your code to handle each configuration differently.

Refer this answer to see this in more detail with screenshots.

I came up with the following solution:

private func isAppStoreDeployedBuild() -> Bool {
    func isSimulator() -> Bool {
        return TARGET_OS_SIMULATOR != 0
    }

    var isAppStore = false
    #if DEBUG
        isAppStore = false
    #else
        if isSimulator() {
            isAppStore = false
        } else {
            // Check if Ad Hoc build e.g. HockeyApp
            if NSBundle.mainBundle().pathForResource("embedded", ofType: "mobileprovision") == nil {
                isAppStore = true
            } else {
                isAppStore = false
            }
        }
    #endif
    return isAppStore
}

This covers everything except RELEASE Builds uploaded directly (over USB) to the device. I still can't find a solution to check this (apart from lukya's manual solution).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM