Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How To Download Plist File From Server In Cocos2d

    • 0
    • 4
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 311
    Comment on it

    In an IOS app development,user can have two methods to download file from server i.e Synchronous and Asynchronous . So before going ahead ,please take a look to the basic knowledge of these methods .

    A Synchronous method is preferable in case of transferring small size data and an Asynchronous is used to transfer large size data.Asynchronous is more preferable as it downloads file asynchronously and allows the user to interact with the application while files are downloading.


    Here is an example of making an Asynchronous request to the server.Before starting download ASIHttpRequest classes from here http://labs.grupow.com/blog/2011/03/28/using-asihttprequest and add it to the project.

    // Initialise an array that will contains all your files,
    NSMutableArray *filesToLoad = [[NSMutableArray alloc] init];
    
    //take strings that contain file name
    
     NSString *fileName1;
    
     NSString *fileName2;
    
    //Pass file name to the string
    
      fileName1 = @"spacescene_assets-Ipad.plist";
    
      fileName2 = @"spacescene_assets-Ipad.pvr.ccz";
    
    //check whether the file with the same name exist on server or not uzing checkFileExistOrNot 
    function given below
    
    if(![self checkFileExixtOrNot:fileName1])
      {
    
           NSURL* url1 = [NSURL URLWithString:@"http://froganator.com/froganator/files/v2/
        iPad/spacescene_assets-Ipad.plist"];
    
        //insert file url in an array
    
           [filesToLoadabc insertObject:url1 atIndex:fileToLoad];
    
      }
    
    //similarly for second file
    
    if(![self checkFileExixtOrNot:fileName2])
      {
    
           NSURL* url2 = [NSURL URLWithString:@"http://froganator.com/froganator/files/v2/ 
         iPad /spacescene_assets-Ipad.pvr.ccz"];
    
        //insert file url in an array
    
           [filesToLoadabc insertObject:url1 atIndex:fileToLoad];
    
      }
    
    //Then traverse on "fileToLoad" array
    
    for(int i=0;i<fileToLoad;i++)
        {
            NSURL *urlfinal = [filesToLoadabc objectAtIndex:i];
    
            CCLOG(@"final url - - - -- - - %@",urlfinal);
    
            //Making request to server and passing files url to it
    
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlfinal];
    
            [request setDelegate:self];
    
            [request startAsynchronous];
        }
    
    
    //This function checks whether the file exist or not and return a success or failure boolean
    
    -(BOOL) checkFileExixtOrNot:(NSString *)fileNameIs
    {
        BOOL success;
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
    
        NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,      YES);
    
        NSString *path = [paths objectAtIndex:0];
    
        NSString* fileName =
        [path stringByAppendingPathComponent:fileNameIs];
    
        success = [fileManager fileExistsAtPath:fileName];
    
        return success;
    }
    

    Lets go through with the code :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: