简体   繁体   English

如何下载xml文件并保存在本地iphone应用程序中

[英]How to download xml file and save in local iphone application

I have a magazine application i want that it load file from server and store it in application when application starts first time only and then use that locally file to save time i am getting data which is located on server it takes alot time 我有一个杂志应用程序我希望它从服务器加载文件并在应用程序第一次启动时将其存储在应用程序中,然后使用该本地文件以节省时间我获取位于服务器上的数据需要很多时间

     NSURL*myurl=url; 

      myurl = [myurl stringByReplacingOccurrencesOfString:@"\n" withString:@""];
     myurl = [myurl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 



     NSURL*urlloaded= [[NSURL alloc]initWithString:myurl];

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

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

//Initialize the delegate. //初始化委托。

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

//Set delegate //设置委托

     [xmlParser setDelegate:parser];   
    BOOL success = [xmlParser parse];

  if(success)

  NSLog(@"No Errors");

   else

   NSLog(@"Error Error Error!!!");

You can download the initial data ie the XML file by using something like this 您可以使用类似的东西下载初始数据,即XML文件

- (void)downloadInitialData {
    NSUserDefaults* userDefaults =  [NSUserDefaults standardUserDefaults];
    if ([userDefaults boolForKey:@"DATA_DOWNLOAD_KEY"] == NO) {
        [self showWaitViewWithText:@"Downloading Data..."];
        [self fetchDataFromServer];
    }
}



- (void)fetchDataFromServer {

    //Call to server to downlaod data
    //When Data is successfully downloaded

    //Stop loading when data save completes
    [self stopLoading];

    //Update USerDefaults
    NSUserDefaults* userDefaults =  [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:@"DATA_DOWNLOAD_KEY"];
    [userDefaults synchronize];
}

You can make call to [self downloadInitialData]; 你可以拨打[self downloadInitialData]; which will ensure that Data is downloaded only once when the application starts . 这将确保在应用程序启动时仅下载一次数据。 You will have to fix it according to your requirements to download data by resetting the @"DATA_DOWNLOAD_KEY" key. 您必须根据您的要求通过重置@“DATA_DOWNLOAD_KEY”键来修复它以下载数据。

What you can do is:- 你能做的是: -

Suppose you have parsed your xml and stored data in array say ' dataArray ' 假设你已经在数组中解析了你的xml和存储的数据,说' dataArray '

Now you have to save your dataArray in NSUserDefaults 现在,您必须在NSUserDefaults保存dataArray

NSUserDefaults *pref1=[NSUserDefaults standardUserDefaults];
[pref1 setObject:dataArray forKey:@"parseData"];    
[pref1 synchronize];

Whenever you have to use this data you can extract it like:- 每当您必须使用此数据时,您可以将其提取为: -

NSUserDefaults *pref1=[NSUserDefaults standardUserDefaults];
NSArray *dataArray=[pref1 objectForKey:@"parseData"];

查看Apples自己的SeismicXML示例 - 可用的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM