Reading preferences in your application
Video: Reading preferences in your applicationNow, we have a working settings bundle. We need to be able to read the values in our code. In this movie we'll look at how that is done. We'll go ahead and open the BWRSS-preferences that I created in the last movie. I'm going to open that in Xcode by double-clicking on the .xcode project file. So, this is the version that has these preferences Root.plist in it. I'm going to come up here into RSSDB and open the RSSDB.m.
Viewers: in countries Watching now:
The iOS software development kit (SDK) includes the popular SQLite library, a lightweight yet powerful relational database engine that is easily embedded into an application. In this course, Bill Weinman teaches you how to build an RSS reader for iOS devices, integrating XML data and a streamlined interface. He explains how to use the SQLite database, display information in a table view, code view controllers, and create a preferences pane for your app. The resulting application is optimized for all iPhone and iPad displays.
- Prototyping the app
- Coding and working with a testbed
- Creating an Objective-C interface for SQLite
- Designing a database schema
- Creating the view controllers
- Reading and writing to the database
- Parsing the RSS feed with NSXMLParser
- Updating the item view with feed items
- Implementing the pull to refresh gesture for iOS 6
- Creating a universal application with multiple views
Reading preferences in your application
Now, we have a working settings bundle. We need to be able to read the values in our code. In this movie we'll look at how that is done. We'll go ahead and open the BWRSS-preferences that I created in the last movie. I'm going to open that in Xcode by double-clicking on the .xcode project file. So, this is the version that has these preferences Root.plist in it. I'm going to come up here into RSSDB and open the RSSDB.m.
So, this is our data base routines, and I just closed the Utilities panel because we don't need that, it's taking up space here. If we look down here at the deleteOldItems, we'll see rather along SQL Query it takes up two strings. Two strings right next to each other in C or C++ or in Objective-C concatenates these two strings together. Objective-C has borrowed this even with its NSString literals. So, this is really all one string delete from blah, blah, blah, all the way up to this closing quote here.
Now, what we have here is a subquery. Everything in the parenthesis is a subquery. So, it's selecting items from the items table and ordering them by publication date with a limit of getMaxItemsPerFeed and then it's deleting everything that is not in that list. It's taking the newest so many items, and it's deleting everything else. So, this getMaxItemsPerFeed is the number of items that's being retained when we delete old items.
So, where is that defined? So, getMaxItemsPerFeed is up here, and this is actually reading from the user default. NSUserDefaults grabs our UserDefaults, and we can get our MaxItemsPerFeed by using objectForKey and using this string max_items_per_feed, and if we remember, down here in our Settings Bundle, our identifier MaxItemsPerFeed is used for that Multi Value where we select either 25, 50, 100 or 250.
You'll also notice that if we don't have a MaxItemsPerFeed, we set it to a default value, and that kDefault, that's defined in our header file and the default is 50. So let's run a little experiment here. I'm going to grab the MaxItemsPerFeed value, and I'm going to display it with NSlog. And I'm also going to display it after we've tested it for default. Press Command+S to save. I'm going to go ahead and run this in the simulator.
It's an interesting test because it's going to show one of the limitations of this settings interface. Now, the first thing I'm going to do when I run this in the simulator, I'm going to come over here to our iOS Simulator menu, and I'm going to say Reset Contents and Setting, and I'm going to press this Reset button. That resets my simulator to its default as if it's a brand-new phone out of the box, and you'll notice that it's also finish running and so I have to run it again. Now, we're running this fresh having never gone to the settings before.
When I select one of these feeds, this MaxItemsPerFeed gets called, and you'll notice that the value from the settings is null and after the test for default is 50. So, we get a null the first time we call it. Until you've actually gone to the settings app and selected something, you're not going to get a value and so you have to have a default value and set it to that default value, in case what you get from your settings is actually null. Now, when I come out here to my settings-- I'm going to press the Home button here.
I'm pressing Shift+Command+H on my keyboard for the Home button because my screen doesn't have one because this is getting scaled down. I'm going to slide over here and grab the settings app and come over here to BW RSS. If I select 50 again, and I double tap my Home button and come back to my BW RSS app, and I'll select this again, you'll see that I'm still getting that null, but if I come out here to my settings app, and I select a different value.
I'll select 100 and press, double tap my Home button and come out here and select my Feed again. Now, you'll notice that I'm getting that 100 value from the settings and after the test because it's not null, I go ahead and I leave that value alone. So, when you run your app, you're not going to actually get values from your settings until somebody goes to the settings app and actually changes something. Even just looking at the value or selecting the default value again, doesn't change something.
Now, when I bring it back to 50 again, now I'll pick up that 50. So, there we have the 50 before and after the test. So, we'll go ahead and delete my NSLog items here. Now, we can see that using the iOS Setting Interface is really quite easy, you just create the settings bundle, you put it in your settings, and you use the NSUserDefaults class to read those values.
Find answers to the most frequently asked questions about iOS SDK and SQLite: Building Data-Driven Apps (2013) .
Here are the FAQs that matched your search "" :
- Q: Why is the RSSDB library in the exercise files different than the one in the videos?
A: The RSSDB library had to be updated to work around a bug in the iOS 7 SDK.
There is a bug in the iOS 7 SDK that prevents the BWDB fast enumeration implementation from working on a device. The symptom is code that runs fine on the emulator, but not on a device. iOS devices use an ARM processor, while the emulator runs on your Mac's Intel processor. This points to the LLVM ARM code generator as the source of the bug. Because the bug appears to be in the LLVM compiler, it may be some time before it is fixed.
As a workaround we have changed the getFeedIDs and getItemIDs methods in the RSSDB library so they don't use Objective C fast enumeration.
Please note that this same bug also affects some of the BWDB testbed code in Chapter 2. The result is that it will run on the emulator but not on a device.
- Q: After upgrading to Xcode 5.1 I get an error that says:
"Used type va_list (aka_builtin_va_list) where arithmetic or pointer type is required"
- A: Please download the exercise files again to get the latest version of the BWDB library.
- Q: I'm using Xcode 6. Why am I getting error messages with the exercise files?
- A: A lot has changed in iOS since this course was released. The author is in the process of rewriting the code and updating the course for iOS 8. In the meantime he has prepared a version of the app that works in iOS 8 and Xcode 6. Download it here:http://ios.bw.org/
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.