-2

我有一个 Web 服务来获取JSON表单中的数据。appDidFinishLaunching每次应用程序启动(内部方法)并将其存储在本地文件中时,我都会调用此服务。我只想在第一次启动应用程序时调用该服务,并且从下一次我只想使用我存储JSON数据的文件。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //live json data url

    NSURL *url = [NSURL URLWithString:@"My Url"];
    NSData *urlData = [NSData dataWithContentsOfURL:url];

    //attempt to download live data
    if (urlData)
    {
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
        [urlData writeToFile:filePath atomically:YES];

    }
    //copy data from initial package into the applications Documents folder
    else
    {
        //file to write to
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];

        //file to copy from
        NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ];
        NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil];

        //write file to device
        [jsonData writeToFile:filePath atomically:YES];
    }

    NSError *jsonError = nil;

    NSString *jsonFilePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:jsonFilePath options:kNilOptions error:&jsonError ];
    NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
    self.serverResponseArray = jsonArray;
    NSLog(@"%@",self.serverResponseArray);

通过这段代码,我正在编写JSON对本地文件的响应并将其存储在一个数组中以在整个应用程序中使用它。

4

2 回答 2

2
   static NSString * const someKey = @"haveChecked";
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    NSUserDefaults * stdUserDefaults = [NSUserDefaults standardUserDefaults];
    if(![[stdUserDefaults objectForKey:someKey] boolValue])
    {
        [self trySomething];
    }
}
-(void)gotJson
{
    //async callback
    NSUserDefaults * stdUserDefaults = [NSUserDefaults standardUserDefaults];
    [stdUserDefaults setBool:YES forKey:someKey];
    [stdUserDefaults synchronize];

}

听起来你需要学习处理文件,所以这里有一些堆栈溢出阅读:

什么是文档目录(NSDocumentDirectory)?
在 iOS 上编写文件

于 2013-05-08T21:07:57.877 回答
0

尝试这个

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {

            if ([self fetchFromNSUserDefaults:@"FirstTimeNO"]==nil || [FunctionManager fetchFromNSUserDefaults:@"FirstTimeNO"]==@"") {
                 [self YourFunctionThatCallFirstTime];
            }
        return YES;
    }

    -(id)fetchFromNSUserDefaults:(NSString *)strKey{
             NSUserDefaults *objUserDefaults = [NSUserDefaults standardUserDefaults];
              return [objUserDefaults objectForKey:strKey];
    }

    -(void)addToNSUserDefaults:(id)objValue forKey:(NSString *)strKey{
        NSUserDefaults *objUserDefaults = [NSUserDefaults standardUserDefaults];
        [objUserDefaults setObject:objValue forKey:strKey];
        [objUserDefaults synchronize];
    }

    -(void)YourFunctionThatCallFirstTime {
                 //Your Stuff

           [self addToNSUserDefaults:@"1" forKey:@"FirstTimeNO"];
     }
于 2013-05-09T05:04:00.610 回答