问题
I'm working on an app that would require to update a .plist file every couple of months, i don't what to resubmit the app everytime for that, so is their a way to host the .plist file online and have the app call it for updates? I tried using NSURLConnection (or maybe i didn't setup it right) but that didn't work... any other ideas? Here is example file that i'm using http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist as always thanks in advance, here is my code:
NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NewsSources" ofType:@"plist"]];
How can i change the code so i can access the file that is hosted online?
回答1:
The .plist from your address example is a NSArray at top level, not a NSDictionary. If you try and try with it, you'll fail if you want a NSDictionary.
Its content clearly shows it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array> <================= *** HERE ***
    <dict>
        <key>title</key>
        <string>Batman Begins</string>
        <key>coverImage</key>
        <string>batman_begins.png</string>
        <key>featureLength</key>
        <integer>140</integer>
        <key>releaseDate</key>
        <date>2008-07-08T23:32:07Z</date>
    </dict>
    <dict>
        <key>title</key>
        <string>The Dark Knight</string>
        <key>coverImage</key>
        <string>dark_knight.png</string>
        <key>featureLength</key>
        <integer>152</integer>
        <key>releaseDate</key>
        <date>2008-12-10T00:32:07Z</date>
    </dict>
    <dict>
        <key>title</key>
        <string>The Prestige</string>
        <key>coverImage</key>
        <string>prestige.png</string>
        <key>featureLength</key>
        <integer>130</integer>
        <key>releaseDate</key>
        <date>2007-02-21T00:32:07Z</date>
    </dict>
</array>
</plist>
You can use:
NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist"]];
If the plist (you want to use) is really a NSDictionary at top level, you can use a similar approach:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"linkToNSDictionaryTopLevel.plist"]];
来源:https://stackoverflow.com/questions/30281206/reading-a-plist-file-thats-hosted-online