无法在真实设备上更新 plist 文件
Can't update plist file on real device
在我的应用程序中,我想从我的网站下载 men.plist
文件,它是 Pickerview 的数据存储,并替换该 men.plist 文件的先前本地版本。如果我在 iPhone 模拟器上测试它,它就可以工作,如果我在真实设备上 运行 它就不能工作。
这是我在 ViewController.m 中的代码:
NSString *urlPath = [NSString stringWithFormat:@"http://somedomain.com/men.plist"];
NSURL *url = [NSURL URLWithString:urlPath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *responceData, NSError *connectionError) {
if (responceData) {
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"men" ofType:@"plist"];
NSDictionary *replacement = [[NSDictionary alloc] initWithContentsOfURL:url];
[[replacement description] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
在那之后在我的 ClothDataStorage.m class 我做:
- (instancetype)init
{
self = [super init];
if (self) {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"men" ofType:@"plist"];
NSDictionary *clothDataInfo = [NSDictionary dictionaryWithContentsOfFile: plistPath];
_countryInfoStorage = [[CountryInfoStorage alloc] initWithClothDataInfo:clothDataInfo];
_clothInfoStorage = [[ClothInfoStorage alloc] initWithClothDataInfo:clothDataInfo];
_sizeInfoStorage = [[SizeInfoStorage alloc] initWithClothDataInfo:clothDataInfo];
}
return self;
}
我比较了来自 ViewController 的 filePath 和来自 Cloth DataStorage.m 的 plistPath,这两种情况都相同,对于模拟器:
(lldb) po filePath
/Users/someuser/Library/Developer/CoreSimulator/Devices/97AC5070-9286-479A-9C31-78974BA982F4/data/Containers/Bundle/Application/05C8CB5B-9869-4D44-847B-203A237255E5/Size Chart.app/men.plist
(lldb) po plistPath
/Users/someuser/Library/Developer/CoreSimulator/Devices/97AC5070-9286-479A-9C31-78974BA982F4/data/Containers/Bundle/Application/05C8CB5B-9869-4D44-847B-203A237255E5/Size Chart.app/men.plist
对于真实设备:
(lldb) po filePath
/private/var/mobile/Containers/Bundle/Application/47E8272D-1289-468B-B575-4AFF9677FBF0/Size Chart.app/men.plist
(lldb) po plistPath
/private/var/mobile/Containers/Bundle/Application/47E8272D-1289-468B-B575-4AFF9677FBF0/Size Chart.app/men.plist
为什么这个文件更新了,我可以在模拟器上看到更改后的数据,但在真实系统上却看不到 iPhone?提前谢谢你。
您不能写入与您的应用程序捆绑在一起的文件,因为该捆绑包是只读的。您必须将文件复制到应用程序的文档目录,然后根据需要阅读或更新它。
在我的应用程序中,我想从我的网站下载 men.plist
文件,它是 Pickerview 的数据存储,并替换该 men.plist 文件的先前本地版本。如果我在 iPhone 模拟器上测试它,它就可以工作,如果我在真实设备上 运行 它就不能工作。
这是我在 ViewController.m 中的代码:
NSString *urlPath = [NSString stringWithFormat:@"http://somedomain.com/men.plist"];
NSURL *url = [NSURL URLWithString:urlPath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *responceData, NSError *connectionError) {
if (responceData) {
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"men" ofType:@"plist"];
NSDictionary *replacement = [[NSDictionary alloc] initWithContentsOfURL:url];
[[replacement description] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
在那之后在我的 ClothDataStorage.m class 我做:
- (instancetype)init
{
self = [super init];
if (self) {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"men" ofType:@"plist"];
NSDictionary *clothDataInfo = [NSDictionary dictionaryWithContentsOfFile: plistPath];
_countryInfoStorage = [[CountryInfoStorage alloc] initWithClothDataInfo:clothDataInfo];
_clothInfoStorage = [[ClothInfoStorage alloc] initWithClothDataInfo:clothDataInfo];
_sizeInfoStorage = [[SizeInfoStorage alloc] initWithClothDataInfo:clothDataInfo];
}
return self;
}
我比较了来自 ViewController 的 filePath 和来自 Cloth DataStorage.m 的 plistPath,这两种情况都相同,对于模拟器:
(lldb) po filePath /Users/someuser/Library/Developer/CoreSimulator/Devices/97AC5070-9286-479A-9C31-78974BA982F4/data/Containers/Bundle/Application/05C8CB5B-9869-4D44-847B-203A237255E5/Size Chart.app/men.plist
(lldb) po plistPath /Users/someuser/Library/Developer/CoreSimulator/Devices/97AC5070-9286-479A-9C31-78974BA982F4/data/Containers/Bundle/Application/05C8CB5B-9869-4D44-847B-203A237255E5/Size Chart.app/men.plist
对于真实设备:
(lldb) po filePath /private/var/mobile/Containers/Bundle/Application/47E8272D-1289-468B-B575-4AFF9677FBF0/Size Chart.app/men.plist
(lldb) po plistPath /private/var/mobile/Containers/Bundle/Application/47E8272D-1289-468B-B575-4AFF9677FBF0/Size Chart.app/men.plist
为什么这个文件更新了,我可以在模拟器上看到更改后的数据,但在真实系统上却看不到 iPhone?提前谢谢你。
您不能写入与您的应用程序捆绑在一起的文件,因为该捆绑包是只读的。您必须将文件复制到应用程序的文档目录,然后根据需要阅读或更新它。