Loading NSMutableDictionary from URL - iphone

I'm currently doing this:
NSMutableDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL: [NSURL URLWithString:#"http://mysite/mypage.php"]];
Which is great, apart from when the data being returned is quite large, and it appears to time out. How could I get around this?
Thanks in advance.

I'm not a big fan of using NSDictionary to manage downloads. I'd probably try something like:
NSURL *url = [NSURL URLWithString:#"http://mysite/mypage.php"];
NSURLRequest *request = [NSURLRequest requestWintURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
Now, if data is not NULL then save to local file. Then load the dictionary using the contents of that file using the initWithContentsOfFile: method.
If you still get the timeouts you can try larger timeoutIntervals.

NSURLRequest (or NSMutableURLRequest) and NSURLConnection.

In convenience methods like initWithContentsOfURL you have no control over things like timeouts. They are fine in my cases but it sounds like you will need to use the more low-level NSURLConnection and NSURLRequest to load data from the server. There are many examples on the net.

Related

how to load xml into memory in faster way?

I am new to objective-c. I load the whole xml string from server to an nsxml parser like below: but the problem is loding from url to memory takes much more time than parsing it. how can i solve that? (my app is very slow)
xmlString = [[NSMutableString alloc] initWithContentsOfURL:url usedEncoding:&NSUTF8StringEncoding error:&error];
NSData *xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
Run your XML Parser in different Thread.(this will not make your parsing faster)
How To Choose The Best XML Parser for Your iPhone Project
Basically NSXMLParser is slow. There is a possibility of finding a better parser for the specific need. A true stream parser might help if you do not need the entire DOM and validation.
Don't use
xmlString = [[NSMutableString alloc] initWithContentsOfURL:url usedEncoding:&NSUTF8StringEncoding error:&error];
Use a network library like ASIHTTPRequest to load the content from a URL and then parse it.
Loading directly from URL like you've done will hang your app till the data is loaded.
Download and install that library as given in the instructions (it's easy), then you can do
{
//in some function
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous]; //this will load URL in background
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
//parse the XML here
[self parseXML:responseString];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
You cannot load the xml faster from the url as it totally depends on your internet connection.
To keep your application smooth, you need to parse the xml in another thread. I answered a similar question in the following url
How to handle freezing programm at loading xml from bad url?
Also there are faster XML parser than the defaul NSXMLParser. There is a good introduction about them in the following blog post. So you can use any of these parser in place of NSXMLParser to improve the performance of your application.
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
I hope that it may clarify a little bit of your question.

how to pass value through NSURL

i was trying to pass value to a native html page from my web view application
I try
url=[NSString stringWithFormat:#"%#?bob=123&frank=321&tom=213",url];
but when i build and debugging program, application crashes showing “EXC_BAD_ACCESS”
Please help me if anyone knows how to do this.
The UIWebView class only supports loading of URLs using the NSURLRequest class. To do what you want to do, you'll need this:
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:#"%#?bob=123&frank=321&tom=213", base]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];

Why UIWebView Eating so many Memory?

I'm using a UIWebView to load a pure Text HTML page for my iPad app. The size of the HTMP page is only 40KB. But when I use the instrument to monitor the memory use for loading the UIWebView, I found down it consumes like 20MB memory, if I scroll the web view, the memory is even getting higher. Finally I get a level 1 memory warning.
Could anyone help me with this? How could I reduce the memory for this? (I need to use the HTML to show the text here).
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"SPC"];
NSURL *url = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
htmlPath
url
request
release all of them after this line
[webView loadRequest:request];
then release webview in dealloc and use webview as ivar

How can I create a single HTTP Get request for iPhone?

First of all, sorry for my posibly bad english...
I got a surely big stupid question...
In my enterprise have an automatic door system, that is opened with a HTTP GET request to a file.
Example:
http://ipaddress/rc.cgi?o=1,50
Where the o=number indicates the amount of seconds that the automatic door will run.
The is no need for authentification or nothing (that is made by LAN Radius).
So, the question is...
How can I make a single button (for example in the springboard) that when you touch it, runs the GET request?
You thing that it should be possible with NSURLConection ?
Thanks for all
I'm not sure if this is the best way of going about it, but this is how I've achieved something similar in my own app. Just create a new NSData object that hits the required URL, then release it if you don't need to do anything with the returned data:
NSURL *theURL = [[NSURL alloc] initWithString:#"http://ipaddress/rc.cgi?o=1,50"];
NSData *theData = [[NSData alloc] initWithContentsOfURL:theURL];
[theData release];
[theURL release];
Or just create an NSURLConnection to run asynchronously, then you don't have to worry about the UI hanging and if the delegate is set to nil, you can pretty much forget about it after you've run it.
NSURL * url = [NSURL URLWithString:#"http://ipaddress/rc.cgi?o=1,50"];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:nil];
[request release];
[theConnection release];

How do I make UIWebView show a pdf file from its own app?

I really need to make a UIWebView open up a specific PDF document that is in my project directory. I can't seem to find any tutorials on this. I found one, but the author was not specific about some of the code.
I have to assume it's got to be quite a simple bit of code.
Can someone help me code UIWebView to load a PDF?
Thanks.
Use pathForResource and fileURLWithPath:
NSString *path = [[NSBundle mainBundle] pathForResource:#"FileNameHere"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];