Tuesday, June 29, 2010

saving xml locally

///////////////////////////////
NSURL *url = [[NSURL alloc] initWithString:@"http:/xml.php"];

NSData *data = [NSData dataWithContentsOfURL:url];
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"icellxml.php"];


NSLog(@"new xml %@",storePath );

// write to file atomically (using temp file)
[data writeToFile:storePath atomically:TRUE];

NSLog(@" saved data is %@",storePath);

// NSData *data = [NSData dataWithBytes:ptr length:len]; to get back


////////////////////////////

NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:storePath]) {
//open it and read it
NSLog(@"data file found. reading into memory");

} else {
NSLog(@"no file found. creating empty array");

}



////////// PULLING DATA IN OFFLINE MODE



NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:storePath]) {
//open it and read it
NSLog(@"data file found. reading into memory");




NSURL *urla = [[[NSURL alloc] init] fileURLWithPath:storePath];





storePath = [storePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSString *urlb = [[NSURL fileURLWithPath:storePath] absoluteString];




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

NSURL *urlc = [NSURL fileURLWithPath:[documentsDirectory
stringByAppendingString:@"/icellxml1.php" ]];


NSLog(@"welcome %@",documentsDirectory );
NSLog(@"werkgwkerwekwegrw %@",urlc);






NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:urlc];

//Initialize the delegate.

XMLParser *parser = [[XMLParser alloc] initXMLParser];

//Set delegate

[xmlParser setDelegate:parser];

//Start parsing the XML file.

BOOL success = [xmlParser parse];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

if(success)
{
NSLog(@"BINGO");
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
else
{
NSLog(@"DAM DAM DAM !!!");
}

No comments:

FEEDJIT Live