There is no introduction to the sandbox mechanism. If you need it, you can go to the sandbox blog. Thank you
There is the article is in the case of real machine debugging ha
1. Write key code to the sandbox
- (void) redirectNSLogToDocumentFolder {/ / the sandbox path NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [Paths objectAtIndex:0]; NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]]; / / stitching sandbox path + name of the folder to file path nsstrings * logFilePath = [documentsDirectory stringByAppendingPathComponent: fileName]; / / stderr is our NSLog, writes it to file freopen ([logFilePath cStringUsingEncoding: NSASCIIStringEncoding], "a +", stderr); }Copy the code
2. Test code
Note ⚠️ : The function that writes to the sandbox must be called before the corresponding NSLog statement in order to write to the sandbox correctly, see the code below for details
// // ViewController.m // Test01 // // Created by Game - Netease on 2021/6/16. // #import "viewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; / / in front of the NSLog calls redirectNSLogToDocumentFolder function called [self redirectNSLogToDocumentFolder]; NSLog(@"%@",path); } - (void)redirectNSLogToDocumentFolder{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]]; NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName]; freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); } @endCopy the code
3. View the output file
Note ⚠️ : if you write to the sandbox during real machine debugging, the NSLog statement will not be printed in Xcode, you will not see it
(So don’t be like me at the beginning: “Huh? !!!!! Why is there no output statement? What’s going on? !” )
- Open the Devices
And you can see the App that we’re running right here
- The download file
Click Download to Download
- Display package contents open file
You can see our files now!