Search code examples
objective-cxcodemacosfinder

changecurrentdirectorypath changes the current path to null in objective c


I have tried many times to change current directory to /User/{username} so I can create new folder at /User/{username}/ directory. The code i've used is:

filemgr = [NSFileManager defaultManager];

currentpath = [filemgr currentDirectoryPath];

NSLog (@"from changing directory Current directory is %@", currentpath);

if ([filemgr changeCurrentDirectoryPath: @"/Users/"] == NO)
    NSLog (@"from changing directory Cannot change directory.");
else
    NSLog(@"directory changed is %@",currentpath);

currentpath = [filemgr currentDirectoryPath];

NSLog (@"directory after changed Current directory is %@", currentpath);

and gets the output like:

2015-12-05 11:47:43.450 test2_Finder[970:33618] from changing directory Current directory is /Users/macbookpro/Library/Containers/com.eebs.Test2.test2-Finder/Data 2015-12-05 11:47:43.450 test2_Finder

[970:33618] directory changed is /Users/macbookpro/Library/Containers/com.eebs.Test2.test2-Finder/Data 2015-12-05 11:47:43.458 test2_Finder[970:33618] directory after changed Current directory is (null)

2015-12-05 11:47:43.458 test2_Finder[970:33618] -[FinderSync init] launched from /Users/macbookpro/Library/Developer/Xcode/DerivedData/Test2-hbxwqkjtnzaewlcisyxybyfbzefb/Build/Products/Debug/Test2.app/Contents/PlugIns/test2_Finder.appex ; compiled at 11:47:39


Solution

  • Finally got the solution for creating folders to user directory such as "/Users/macbookpro/folder" NSString *userPath=[NSOpenStepRootDirectory() stringByAppendingString:@"Users/"]; NSString *userPath1=[userPath stringByAppendingString:NSUserName()]; NSString *userPath2=[userPath1 stringByAppendingString:@"/folder/"]; if(![fileManager fileExistsAtPath:userPath2 isDirectory:&isDir]) if(![fileManager createDirectoryAtPath:userPath2 withIntermediateDirectories:YES attributes:nil error:NULL]) NSLog(@"Error: Create folder failed %@",userPath2);