3

How can I get the build configuration type from a static library (debug or release)?
Normally we use #ifdef DEBUG but in this case it will not work, because this check is compile time and our static library is already compiled.

5
  • I'm not quite sure I follow - are you trying to determine how the static library was built? What config type the app using the library is being run under? Commented May 4, 2015 at 14:37
  • No. I want to know what config type the app using itself Commented May 4, 2015 at 14:41
  • I don't think you're able to do that, as it would not be set at the time your code is compiled. I'm curious if there is another way to solve your initial problem - maybe have 2 versions of the library and link the appropriate one in the app? Commented May 4, 2015 at 18:33
  • My goal is to determine the build type of the app and send it to the server... Commented May 4, 2015 at 18:41
  • Ok, so couldn't you make a method that takes it in as a parameter to the library, then use #ifdef etc. to set that from the app? I'm assuming are writing both pieces Commented May 4, 2015 at 20:47

1 Answer 1

1
+50

If you just want to know if the app was compiled in debug or production (AdHoc equals production), you can use the following method, which can be called within the static library:

+ (BOOL)isDevelopmentBuild
{
#if TARGET_IPHONE_SIMULATOR
    return YES;
#else
    BOOL isDevelopment = NO;

    // There is no provisioning profile in AppStore Apps.
    NSData *data = [NSData dataWithContentsOfFile:[NSBundle.mainBundle pathForResource:@"embedded" ofType:@"mobileprovision"]];
    if (data) {
        const char *bytes = [data bytes];
        NSMutableString *profile = [[NSMutableString alloc] initWithCapacity:data.length];
        for (NSUInteger i = 0; i < data.length; i++) {
            [profile appendFormat:@"%c", bytes[i]];
        }
        // Look for debug value, if detected we're in a development build.
        NSString *cleared = [[profile componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] componentsJoinedByString:@""];
        isDevelopment = ([cleared rangeOfString:@"<key>get-task-allow</key><true/>"].length > 0);
    } 

    return isDevelopment;
#endif
}

Notice it will return YES if the app is in development mode.

Sign up to request clarification or add additional context in comments.

12 Comments

Well, your answer is useful, but actually I want to know the build type of the app (debug or release), and doesn't matter is it running on a simulator or on a device. See screenshot: oi59.tinypic.com/atvlm9.jpg
The simulator is just a specific check in order to avoids unnecessary code. It returns Yes if it's in development mode, otherwise No. Please try it in your code and let me know if it works for you.
Just to clarify - if you check this method one real device in development mode, it will return YES.
Seems it works on the device. But what about simulator?
Simulator will always return YES cause you can't really install a release version on it (as far as I know).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.