Thursday, May 9, 2013

NSDictionary initWithContentsOfURL example ios


initWithContentsOfURL:

Initializes a newly allocated dictionary using the keys and values found at a given URL.
- (id)initWithContentsOfURL:(NSURL *)aURL
Parameters
aURL
An URL that identifies a resource containing a string representation of a property list whose root object is a dictionary.
Return Value of [NSDictionary initWithContentsOfURL]
An initialized dictionary—which might be different than the original receiver—that contains the dictionary ataURL, or nil if there is an error or if the contents of the resource are an invalid representation of a dictionary.
Discussion of [NSDictionary initWithContentsOfURL]
The dictionary representation in the file identified by path must contain only property list objects (NSString,NSDataNSDateNSNumberNSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this dictionary are immutable, even if the dictionary is mutable.
Example of [NSDictionary initWithContentsOfURL]
- (void)viewDidLoad
{ 

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TermsConditions" withExtension:@"html"];

    NSStringEncoding *encoding;
    NSError *error;

    NSString *myHtml = [[NSString alloc] initWithContentsOfURL:modelURL usedEncoding:&encoding error:&error];
    [self.TermsWebView loadHTMLString:myHtml baseURL:modelURL];
    [myHtml release];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
Example of [NSDictionary initWithContentsOfURL]
- (void)viewDidLoad
{
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TermsConditions" withExtension:@"html"];

NSStringEncoding *encoding = NULL;
NSError *error;

NSString *myHtml = [[NSString alloc] initWithContentsOfURL:modelURL usedEncoding:encoding error:&error];
[self.TermsWebView loadHTMLString:myHtml baseURL:modelURL];
[myHtml release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Example of [NSDictionary initWithContentsOfURL]
NSURL * plist = [NSURL URLWithString: @ "http://www.example.com/example.plist"];
NSDictionary * dict = [[[NSDictionary alloc] initWithContentsOfURL:plist] autorelease];
  if ([dict count] == 0) {
      //no connection
  }