Detecting if iOS app is run in debugger

前端 未结 8 1838
北恋
北恋 2020-11-27 13:45

I set up my application to either send debugging output to console or a log file. Now, I\'d like to decide with in the code whether

  • it is run in the debugger
8条回答
  •  旧巷少年郎
    2020-11-27 14:01

    It is possible to instruct the debugger to set environment variables when it launches a process it is about to debug. This can be done in Xcode by going to the menu item Product->Edit Scheme. Then under the Debug scheme's Arguments tab add a new environment variable. The variable should be named "debugger" with the value "true". Then the following code snippet can be used to determine if the debugger launched your process:

    NSDictionary* env = [NSProcessInfo processInfo].environment;
    
    if ([env[@"debugger"] isEqual:@"true"]) {
        NSLog(@"debugger yes");
    }
    else {
        NSLog(@"debugger no");
    }
    

提交回复
热议问题