NSUserName()似乎在开发iOS应用时在Xcode 6中为我工作。 我正在使用它来获取桌面目录,以便在开发期间将核心数据种子保存到我的桌面。
NSString* deskStorePath = [NSString stringWithFormat:@"/Users/%@/Desktop/newMySQL.CDBStore", NSUserName()];
我不得不将代码更改为:
#define USER_NAME @"myusername"
NSString* deskStorePath02 = [NSString stringWithFormat:@"/Users/%@/Desktop/newMySQL.CDBStore", USER_NAME];
现在NSUserName()在Xcode 6中没有返回任何内容,但在Xcode 5中运行良好。
NSString* myname = NSUserName();
NSString* myfullname = NSFullUserName();
NSLog(@"name : %@ - %@",myname,myfullname);
这是一个错误吗? 我现在需要导入一个库吗? 或者在应用程序开发期间是否有替代方法可以获取当前的OSX用户名?
编辑:(这是我用来将核心数据种子存储保存到桌面的方法,然后我将其添加到我的开发环境中)
- (void) saveSeedToDesktop
{
[self testNSUser];
NSString* myDBName = [NSString stringWithFormat:@"%@.sqlite",CD_FILE_NAME];
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent : myDBName];
NSString* deskStorePath = [NSString stringWithFormat:@"/Users/%@/Desktop/newMySQL.CDBStore", USER_NAME];
NSError*error;
if (![[NSFileManager defaultManager] copyItemAtPath:storePath toPath: deskStorePath error:&error]){
NSLog(@"error with path : %@",error);
}
else {
NSLog(@"Copied");
}
}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
}
这是一个测试类:
- (void) testNSUser {
NSString* myname = NSUserName();
NSString* myfullname = NSFullUserName();
NSLog(@"name : %@ - %@",myname,myfullname);
__unused NSString* deskStorePathTest01 = [NSString stringWithFormat:@"/Users/this_is_my_name/Desktop/test.txt"]; //works
__unused NSString* deskStorePathTest02 = [NSString stringWithFormat:@"~/Desktop/test.txt"];
__unused NSString* deskStorePathTest03 = [NSString stringWithFormat:@"/Users/~/Desktop/test.txt"];
__unused NSString* deskStorePathTest04 = [NSString stringWithFormat:@"/~/Desktop/test.txt"];
[self fileExistsTest:deskStorePathTest01];
[self fileExistsTest:deskStorePathTest02];
[self fileExistsTest:deskStorePathTest03];
[self fileExistsTest:deskStorePathTest04];
}
- (void) fileExistsTest : (NSString*)storePath {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathForFile = storePath;
if ([fileManager fileExistsAtPath:pathForFile]){
NSLog(@"exists : %@",storePath);
}
else {
NSLog(@"doesnt exist");
}
}
答案 0 :(得分:2)
我们在模拟器中运行应用程序产生的资产存在同样的问题。我意识到应用程序本身存储在我的主目录下,因此我可以从中访问我的用户名:
NSString *userName = NSUserName();
#if TARGET_IPHONE_SIMULATOR
if (userName.length == 0)
{
NSArray *bundlePathComponents = [NSBundle.mainBundle.bundlePath pathComponents];
if (bundlePathComponents.count >= 3
&& [bundlePathComponents[0] isEqualToString:@"/"]
&& [bundlePathComponents[1] isEqualToString:@"Users"])
{
userName = bundlePathComponents[2];
}
}
#endif
显然只是为了发展。
答案 1 :(得分:0)
无论如何,它在设备上对我有用。它似乎不适用于模拟器。
-(id) init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if(self)
{
//Custom initialization.
NSString *u = NSUserName();
NSLog(@"%@", u);
}
return self;
}
输出是:
2014-10-05 20:45:38.451 [my app business] mobile
如果我理解你在这里要完成的任务,我认为使用默认数据初始化Core Data存储的规定方法可能是以编程方式添加它。至少,这就是我从文档中删除的内容。我很确定他们会说你不应该在API之外操纵Core Data存储文件。