我創建了一個方法,首先將json數據保存到文件 ,然後我正在讀取文件中的數據。無法將數據保存到iPhone上的文件
我能夠在模擬器上運行時保存和讀取數據,但是當我嘗試在iPhone上運行應用程序並調試通過時,它不保存或檢索數據。
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableData *retrievedData = [NSMutableData data];
[retrievedData appendData:data];
NSMutableString *allInfo = [[NSMutableString alloc] initWithData:retrievedData encoding:NSASCIIStringEncoding];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"credentials" ofType:@"json"];
NSData *userCredentials = allInfo;
[userCredentials writeToFile:filePath atomically:YES];
NSError *error;
NSData* JSONData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:&error];
//get the log in information from the credentials file
NSDictionary *login = [JSONDictionary objectForKey:@"login"];
//get the auth_token
NSDictionary *loginInfo = login;
if(loginInfo == NULL)
{
errorMessage.text = @"Invalid username or password";
errorMessage.hidden = NO;
}
else
{
NSString *authCode = [loginInfo objectForKey:@"auth_token"];
[self saveUserCredentials:authCode];
}
}
你確定你的pathForResource:參數不是憑證大寫C的憑證嗎?我認爲模擬器不區分大小寫,而設備不是。 – rdelmar