使用ASIHTTPRequest下载plist只能部分工作

时间:2012-05-28 22:57:09

标签: iphone objective-c save plist asihttprequest

我有一个可以玩不同类别的游戏。这些类别基本上是plist文件。所以,如果在将来,我想添加plists,用户应该能够下载一个plist并发挥新的水平。所以我的目标是:

  • 从网址
  • 下载.plist
  • 将.plist文件保存在iphone上的文档目录中(永远)
  • 使用.plist

这是我做的,它的工作原理(控制台日志“完成!!”),但是如果我检查文件是否在文件目录中,则没有响应(布尔文件存档保持为NO)。

-(void)startGame:(id)sender{
    NSArray *categoriesArray = [[GameStateSingleton sharedMySingleton] categoriesArray];
    NSString *category = [NSString stringWithFormat:[categoriesArray objectAtIndex:[sender tag]]];


    NSString *thePath = [mainPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",category]];
    NSDictionary *categoryPlist = [NSDictionary dictionaryWithContentsOfFile:thePath];

    if(categoryPlist != nil){
        [[GameStateSingleton sharedMySingleton]setCurrentCategory:category];
        [[GameStateSingleton sharedMySingleton]updateHexCountAndInitalTime];
        [[CCDirector sharedDirector]replaceScene:[CCTransitionFade transitionWithDuration:TRANSITIONDURATION scene:[LevelSets scene]]]; 
    }
    else{

        [self plistForCategory:category];
        NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *myCategory= [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",category]];
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myCategory];
        if(fileExists == YES){
            [[GameStateSingleton sharedMySingleton]setCurrentCategory:category];
            [[GameStateSingleton sharedMySingleton]updateHexCountAndInitalTime];
            [[CCDirector sharedDirector]replaceScene:[CCTransitionFade transitionWithDuration:TRANSITIONDURATION scene:[LevelSets scene]]]; 
            NSLog(@"category exists now");
        }
    }


}



-(void)plistForCategory:(NSString*)category
{

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

    NSURL *url = [NSURL URLWithString:[ NSString stringWithFormat:@"http://www.tinycles.com/wannaplay/plists/%@.plist",category]];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDownloadDestinationPath:documentsDirectory];
    [request setDelegate:self];
    [request startAsynchronous];


}


- (void)requestFinished:(ASIHTTPRequest *)request 
{
    NSLog(@"finished !!");
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    // Download failed. This is why.
    NSError *error = [request error];
    NSLog(@"%@", error);
}

1 个答案:

答案 0 :(得分:0)

也许问题是因为你正在启动异步请求,所以如果请求还没有完成,你的if(fileExist==YES)仍会返回NO;我建议你在这种情况下启动同步请求:

- (IBAction)startRequest:(id)sender
{
  NSURL *url = [NSURL URLWithString:LINK_TO_PLIST];
  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  [request setDownloadDestinationPath:DOC_PATH];
  [request startSynchronous];
  NSError *error = [request error];
  if (!error) {
      //start your game after the file is downloaded
  }
}