-2

I am new to developing iOS apps and I wish to enable my app to download and read an XML file that will update each time the app is opened.

This is what I have so far:

NSURL * url = @"http://192.168.100.161/UploadWhiteB/wh.txt";
NSData * data = [NSData dataWithContentsOfURL:url];

if (data != nil) {
NSLog(@"\nis not nil");
NSString *readdata = [[NSString alloc] initWithContentsOfURL:(NSData *)data ];
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    You have to post what you attempted to do so far. You cannot put a complex topic "in the open" and expect people to write code for you. – Léo Natan Feb 21 '14 at 18:08

1 Answers1

0

Maybe you can use this code in your AppDelegate didFinishLaunchingWithOptions method:

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

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *localPath = [path objectAtIndex:0]; //The apps document directory

    NSString* fileURL = @"http://192.168.100.161/UploadWhiteB/wh.txt";
    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];

    if (!(data==nil)) {
        [data writeToFile:[localPath stringByAppendingPathComponent:@"wh.txt"] atomically:YES];
        NSLog(@"Did download file: wh.txt");
    }
}

Then you can get the data by using this code in another class:

-(NSString *) getDocumentDirectory {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [path objectAtIndex:0]; //The apps document directory
}

NSData *xmlData = [NSData dataWithContentsOfFile:[[self getDocumentDirectory] stringByAppendingPathComponent:@"wh.txt"]];
if(!(xmlData==nil)) {
    //Do your stuff
}

The didFinishLaunchingWithOptions method will run every time the app starts and download (and replace) the file wh.txt from your server to the file wh.txt on your device (in your apps document folder).

Then if you wan't to read the file you can use the NSXMLParser. See:

How to Parse XML Files in Xcode and NSXMLParser example

Does it solve your problem?

If not, provide more information and I will edit my answer.

Community
  • 1
  • 1
Jonathan Gurebo
  • 1,089
  • 1
  • 11
  • 21