How to download plist from server and store locally forever?

I have a simple plist on my server that I want to download to the app's sandbox every time the view is loaded and replace the older one if it exists. After some research I got this code:

[[NSDatadataWithContentsOfURL:@"http://<yourfile>.plist"] writeToFile:@"pathforthefile.plist"atomically:YES];

The problem with this code is xcode says Incompatible pointer types sending 'NSString *' to parameter of type 'NSUrl *'. Is this code even right in the first place? If it is, how can I fix it so it works? (I changed the urls and file name so that's not the error.)

Posted on Jun 13, 2012 10:05 PM

Reply
13 replies

Jun 14, 2012 8:09 AM in response to TechGeniusApps

TechGeniusApps wrote:


I have a simple plist on my server that I want to download to the app's sandbox every time the view is loaded and replace the older one if it exists.


Why? That pretty much goes against the design of the sandbox. It is designed to put the user in control and prevent developers from doing what you are attempting. I think you are risking a rejecting for something like this.


After some research I got this code:

[[NSDatadataWithContentsOfURL:@"http://<yourfile>.plist"] writeToFile:@"pathforthefile.plist"atomically:YES];

The problem with this code is xcode says Incompatible pointer types sending 'NSString *' to parameter of type 'NSUrl *'. Is this code even right in the first place? If it is, how can I fix it so it works? (I changed the urls and file name so that's not the error.)

Maybe convert the string to a URL?

Jun 14, 2012 9:12 AM in response to etresoft

The reason I want to download the plist is to enable offline functionality. I guess I could just run it off the server. So if I delete that line of code above, I try this code, but point it to my server but the table is still blank:

NSString *path = [[NSBundlemainBundle] pathForResource:@"http://appdata.techgenius.webatu.com/Therapy_App/samplePatients"ofType:@"plist"];

Don't worry about the fact that I'm storing these kind of stuff insecurely, and anyone can access it. I will add https later, I just need it to load first.

Jun 14, 2012 9:40 AM in response to TechGeniusApps

TechGeniusApps wrote:


The reason I want to download the plist is to enable offline functionality.

How would requiring the user to download a file each time a view is loaded enable offline functionality? It seems like that would require a active connection. Furthermore, it would require additional code to ensure that the connection was valid before loading the views. Otherwise, your application would become corrupted.


Most apps have everything they need inside the app bundle. You should consider adopting that strategy.

Jun 14, 2012 11:49 AM in response to TechGeniusApps

TechGeniusApps wrote:


This app is completely dynamic. There will be a different plist for every user and I need the same plist for other devices and computer web version too. Therefore, I host the plist on my server and let each device connect and change that file instead of having a copy on each device. Sorry for any confusion.

I'm not Apple and I don't make these decisions, but I can't see how your app is going to get approved with an architecture like that.


You include whatever you need inside the application bundle. If you need any customization, then at first launch you copy the appropriate data from the application bundle into the private documents area. No internet connection needed. That is the way you do that. Any other method is just playing with fire.

Jun 14, 2012 2:19 PM in response to TechGeniusApps

Are you hoping for a lack of success then? That architecture is not scalable.


Is this something you can store in iCloud? I expect Apple would be suspicious of storing it on your own server. What happens when your server is down?


In any event, if you have a legitiate need for this, create a web server that feeds XML content and download that. For testing, you can just send back static files. If you have lots of users, you will definitely want to store the data in a database and generate the XML dynamically. On the client side, update your XML file with data from the server. You could just overwrite the local file if there haven't been any changes locally. Create a simple XML schema and Core Data can read that directly with key-value observing.

Jun 14, 2012 7:27 PM in response to TechGeniusApps

TechGeniusApps wrote:


Can you break down the iPhone part of those steps down? This is my first app with server side processing involved.

Your first step is to decide how you are going to serve the data. I suggest using JSON. There still doesn't appear to be a DOM XML parser on iOS yet. JSON is easier anyway.


Write a server in PHP or similar to provide a JSON document with all the information you need. It should be database driven but you can just serve static files at first.


In your code, use your same method to read the JSON data into an NSData object. Just provide an NSURL instead of an NSString. Then you can create a Cocoa object with something like "JSONObjectWithData:options:error:" Then set/update your user defaults with that value. Just stay away from plist files. You don't need to deal with that at all. Everything should be in NSUserDefaults.

Jun 15, 2012 5:43 AM in response to TechGeniusApps

TechGeniusApps wrote:


I already have an almost complete full-fleged online version for computer written in php. Its just missing extra features. I want to make a mobile app because its easier for the user to use a native app then browsing a web version.


Then you should be able to publish data for your app in a structured format you can easily parse.


About the DOM XML parser, how do apps read xml rss feeds?


They either use a 3rd party DOM parser or the included SAX parser. Either one would work. XML is one of those things that sounded like a good idea at the time but as turned into a ridiculous mess. JSON is far superior.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to download plist from server and store locally forever?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.