Loading NSData into a UIWebView - iphone

In my web browser, I am trying to load a UIWebView with NSData obtained from a NSURLConnection. When I try to load it into the UIWebView, instead of the site, it comes up with the HTML plain text.
Here is my code:
in viewDidLoad:
NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:#"http://www.msn.com"]];
[NSURLConnection connectionWithRequest: request delegate:self];
later in the code:
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
webdata = [NSMutableData dataWithData: data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[webview loadData:webdata MIMEType: #"text/html" textEncodingName: #"UTF-8" baseURL:nil];
}

You are not appending data that you are receiving. Use this piece of code
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if (webdata == nil) {
webdata = [[NSMutableData alloc] init];
}
[webdata appendData:data];
}
This method might be called once or more times depending upon your data length. So instead of assigning new data to your ivar, append your data to it so that you have the full response not the last packet of data received.
------------------------------------------------------------------------------------------------------------------------------------
Updated
Or use like this.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
webdata = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webdata appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[mWebView loadData:webdata MIMEType: #"text/html" textEncodingName: #"UTF-8" baseURL:nil];
}

Related

Not able to get responce from the json url+iphone sdk

I am requesting for the response which have alot of data in it.
But again and again my connection get lost.my internet is working properly.But still the connection get lost
The error :-Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
NSLog(#"Connection Failed!");
appDel.Internet_connected=FALSE;
[MainHandler performSelector:self.targetSelector_loseconenction];
}
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[receivedData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//[connection release];
NSLog(#"Connection Failed!");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
appDel.Internet_connected=TRUE;
NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSString *valueToSave = responseString;
[[NSUserDefaults standardUserDefaults]
setObject:valueToSave forKey:#"responce"];
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"responce"];
NSDictionary *result = [savedValue JSONValue];
}
For Better controller with Networking, use this class:
https://github.com/AFNetworking/AFNetworking.
If your connection is bad, you will need to handle this connection failed
with #try #catch #finally

Returning a string from an NSURLRequest

Is anybody able to help me get a string returned from an NSURLRequest on an iPhone app? I have to send some user credentials to a URL and it will return a customer ID number. That is what I need to be pulled in as a string, I am not used to doing working with servers or HTTP requests, so any help will be great. I have already read the Apple Docs about it and am a bit lost on this part.
This is a standard pattern async downloader:
In .h file:
NSMutableData *responseData;
And .m file:
-(void) execute {
NSString *urlString = #"http://www.google.com";
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[responseData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responseData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
[connection release];
NSString *data = [[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] autorelease];
NSLog(#"%#", data);
[responseData release];
}
This will download Google and outprint the content.

Inconsistent results from NSURL

Using the following code to make my requests, what i've noticed is that data will sometimes not be loaded or fully loaded, this always happens when the app first starts. Any thoughts? I've tried putting in [tableView refresh] in the connectionDidFinishLoading, but that doesn't seem to help, any ideas? Thanks for looking
.h:
NSMutableData *responseData;
.m:
- (void)load {
NSURL *myURL = [NSURL URLWithString:#""];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];
[textView setString:#"Unable to fetch data"];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[responseData
length]);
NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
}
Are you sure your data did not load? Try [tableView reloadData]; instead of refresh.

how to handle the responseString from json output and parse it

I am using the following code and i am getting a response string in JSON. I don't know how to parse it. What should I do next if I want to call any method on JSON output
// source code
- (void)viewDidLoad
{
[super viewDidLoad];
dataWebService = [[NSMutableData data] retain];
NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://www.googleapis.com/customsearch/v1?key=AIzaSyDzl0Ozijg2C47iYfKgBWWkAbZE_wCJ-2U&cx=017576662512468239146:omuauf_lfve&q=lectures&callback=handleResponse"]]retain];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[dataWebService appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
NSLog(#"Response: %#",responseString);
[responseString release];
[dataWebService release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Error during connection: %#", [error description]);
}
Use a JSON parsing framework: http://code.google.com/p/json-framework/

NSURL Connection will/won't load data

So here's my issue:
I followed the NSURLConnection tutorial in the iPhone Developer Documentation almost to the T, and it only kinda works.
Here's where it all goes wrong:
The object seems to be created correctly and delegates to connectionDidFinishLoading, but with any URL I try to load the response data always ends up only being 0 bytes. I am running in the simulator if that makes any difference.
Here's my relevant code:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"WVFS Player";
//create a request
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://wvfs.josh-kaplan.com/nowPlaying.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create a connection
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection) {
// create the datum
responseData=[[NSMutableData data] retain];
} else {
// code this later
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// make it work
NSLog(#"Succeeded! Received %d bytes of data:",[responseData length]);
// release it
[connection release];
[responseData release];
}
And here's my log output:
[Session started at 2010-03-14 09:01:09 -0400.]
2010-03-14 09:01:14.784 WVFS[19571:207] Succeeded! Received 0 bytes of data:
Any ideas?
You forgot to implement connection:didReceiveData:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}