How to get content from URL asynchronously? - objective-c

I'm trying here to create NSXMLParser from content of URL, it work perfectly well but is there way I can make URL content to be received asynchronously and later create NSXMLParser?
NSURL *url = [[NSURL alloc] initWithString: #"http://www.Xmlfile.com"];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[url release];

Use NSURLConnection to fetch the data asynchronously into an NSData, then use initWithData instead of initWithContentsOfURL.

Related

Loading NSData into WebView

I have an NSData object and I want to display it in a WebView. Can WebView handle/display NSData objects?
NSData *data = ... // some data I've gotten from NSURLConnection
WebView *webView = ... [[WebView alloc] init];
Yes!
NSData is handled by UIWebView when properly supplied with MIME type.
This snippet loads a .docx file in a WebView
NSString *path = [urlFileInView path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
webViewForDocsView.delegate = self;
[webViewForDocsView loadData:data MIMEType:#"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:#"UTF-8" baseURL:nil];
Without the Content-Type header, the web view has no way to know how to interpret the data. That's why the Content-Type header was added.
If for some reason you want an NSData capture of network traffic that you can replay whenever you want, consider writing a custom NSURLProtocol. If you write a handler for http then it will override the built-in one; give it a fall-through memory and it can redirect those requests to the real one.
Do it like this
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
[self.webView loadHTMLString: newStr baseURL:nil];

How can i parse local xml file instead of Web xml in Xcode?

Hii it's easy for me to parse a XML file from a web URL using NSXML Parser but i found little bit difficulty in parsing the local XML file.?
consider this,
my parser.m has a method for
-(void)parseRssFeed:(NSString *)url withDelegate:(id)aDelegate {
[self setDelegate:aDelegate];
responseData = [[NSMutableData data] retain];
NSURL *baseURL = [[NSURL URLWithString:url] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];}
and i used this method for calling a RSS Feed in one of my viewcontroller.m as
- (void)loadData {
if (items == nil) {
[activityIndicator startAnimating];
Parser *rssParser = [[Parser alloc] init];
[rssParser parseRssFeed:#"http://--some RssFeed url---" withDelegate:self];
[rssParser release];
} else {
[self.tableView reloadData];
}
}
and now instead of the some web Rss Feed Url i have to load my local XML file.
Please help me in this coding where i have to change and include NSBundlePath Resource for my local XML FIle.
Thanks in advance!!!
For a local file you don't need the NSURLConnection to get the data. The following will get a local XML file named "local.xml" into an NSData object:
NSString *path = [[NSBundle mainBundle] pathForResource:#"local" ofType:#"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
At this point you should be able to call the same parsing code you use once you have the data from the remote xml file.

passing a NSArray to NSURLRequest

I would like to pass a NSArray containing NSstrings to NSURLRequest, is it possible ? I'm not quite sure how to approach this, I'm getting it to work fine with just one URL but I can't seem to be able to pass an Array or urls . Any ideas ?
This is the code I'm using which is obviously wrong calling url1, I would like to call an array instead :
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
NSString *url1 = #"http://www.apple.com/";
NSString *url2 = #"http://www.google.com/";
urls = [[NSArray alloc] initWithObjects:url1,url2,nil];
NSURL *url = [NSURL URLWithString:url1];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
Thanks !
So you want to load multiple webpages, right?
urls = [[NSArray alloc] initwithObjects:url1, url2, nil];
for (NSURL *url in urls) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
}
However, I'm not sure why you would want to do this? This may send a request to url1, but will instantly cancel that request and start loading url2.

How to parse an XML file with NSXMLParser

I have a file called sample.xml and I need to retrieve the contents in that file, using NSXMLParser. I tried parsing the XML file by way of a URL earlier, but now I have an XML file. Can somebody tell me how to parse when I have a file? I am Using Xcode4 and iOS SDK 4.3 Thanks in advance.
When you parsed your data with an URL you should have done something like that:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfURL:<#(NSURL *)#>]];
// Tell NSXMLParser that this class is its delegate
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
// Kick off file parsing
[parser parse];
//[parser setDelegate:nil];
[parser release];
To parse a file, you simply have to transform the first line and replace [NSData dataWithContentsOfURL:<#(NSURL *)#>] with [NSData dataWithContentsOfFile:<#(NSString *)#>] where the NSString is the path to your file.
PS: Increase your acceptance rate
Instead of the url you can use this
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"temp" ofType:#"xml"]];
NSURL *baseURL=[NSURL URLWithString:url];
You can check the following tutorials for reading and parsing XML files in Objective-C.
Reading from a XML file in iOS.
Objective C: Parsing an XML file

Objective-C how to get an image from a url

Am having some difficulty getting an Image from a url, and then displaying it in an image well in the interface.
This is the code I'm currently using, it doesn't work and compiles with no errors:
NSURL *url = [NSURL URLWithString:#"http://www.nataliedee.com/061105/hey-whale.jpg"];
NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[imageView setImage:[NSImage imageNamed:newIMAGE]];
Any ideas as to what is wrong here?
You are passing in image data as a string to a method that is trying to use that string as the name of the image, which of course doesn't exist.
What you need to do is create an NSData object from your URL dataWithContentsOfURL:, once you have the NSData object use this to create the UIImage via imageWithData:.