vendredi 8 mai 2015

Check if text file in directory to show direct

I am developing an IOS app..Firstly download the text file from url and save into directory..and file save into directory..But check if the file has been directory to not download the file direct show the text..Else file not in the directory to download the file than show the text fie..But Running the project file download again and again..

code..

if ([[NSFileManager defaultManager] fileExistsAtPath:localfile]) {
         content = [NSString stringWithContentsOfFile:localfile
                                            encoding:NSUTF8StringEncoding
                                               error:NULL];
    }else
    {
        NSURL* url = [NSURL URLWithString:@"http://ift.tt/1InmjaW"];

    NSArray* pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
    NSString* documentsDir = [pathArray objectAtIndex:0];
    localfile =[documentsDir stringByAppendingPathComponent:@"data"];
        NSData* data = [NSData dataWithContentsOfURL:url];
    [data writeToFile:localfile atomically:YES];
    content = [NSString stringWithContentsOfFile:localfile
                                        encoding:NSUTF8StringEncoding
                                         error:NULL];
}
_textfield.text=content;

Aucun commentaire:

Enregistrer un commentaire