Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-13-2008, 12:58 AM
Administrator
 
Join Date: Jun 2008
Location: Arizona
Posts: 410
Send a message via AIM to mxweas Send a message via Yahoo to mxweas Send a message via Skype™ to mxweas
Default NSURLConnection POST to send & receive data.

This is just a quick example of how to send and receive data using Cocoa's NSURLConnection class.

First the request must be built, if you do not plan to send any data besides the URL this should be sufficient:
Code:
NSString *myURLString = @"http://google.com/";
NSURLRequest *request  = [ [ NSURLRequest alloc ] initWithURL: [ NSURL URLWithString: myURLString ] ];
NSURLRequest's -initWithURL: cachePolicy: timeoutInterval: method can be used also if you need to set how the data will be chached and how long before the connection should timeout.

If you do plan on sending data via POST, an NSData must be made with the request body. It can then be added to an NSMutableURLRequest.
Code:
NSString *myRequestString = @"&myVariable1=Hello%20World&myVariable2=Ohai2u";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ]
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://www.google.com/" ] ]; 
[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: myRequestData ];
Now that the NSURLRequest has been made it's time to send the request and get the return response.

Code:
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
An autoreleased NSData containing the return data is now in returnData. If the connection timed out or some other error occursed nil will be returned.

Note: -sendSynchronousRequest: returningResponse: error: will block until the request either times out or data is returned. If you want to stop the interface from freezing I suggest using this on a secondary thread.

Max
Reply With Quote
  #2 (permalink)  
Old 08-13-2008, 01:19 AM
Junior Member
 
Join Date: Aug 2008
Posts: 1
Default Awesome!

Thanks a million. I'm completely new to both iPhone and Mac development, and this is just what I was looking for.
Reply With Quote
  #3 (permalink)  
Old 08-13-2008, 04:09 AM
Junior Member
 
Join Date: Jun 2008
Posts: 20
Default feeling ...

I have a feeling that I will be using code similar to this in a LOT of things.

Thanks again!

Scott
Reply With Quote
  #4 (permalink)  
Old 08-14-2008, 05:37 AM
Junior Member
 
Join Date: Aug 2008
Posts: 18
Default

Quick question - if I break off a secondary thread to use with this method, will the NSURLConnection method still be able to call delegate methods on an object in the main thread?
Reply With Quote
  #5 (permalink)  
Old 08-14-2008, 06:43 PM
Junior Member
 
Join Date: Jul 2008
Posts: 13
Default

Quote:
Originally Posted by zig_zag_love View Post
Quick question - if I break off a secondary thread to use with this method, will the NSURLConnection method still be able to call delegate methods on an object in the main thread?
After you're done with the synchronous request you can call a delegate method, even if you're in another thread.
Reply With Quote
  #6 (permalink)  
Old 08-15-2008, 01:51 AM
Administrator
 
Join Date: Jun 2008
Location: Arizona
Posts: 410
Send a message via AIM to mxweas Send a message via Yahoo to mxweas Send a message via Skype™ to mxweas
Default

This method is synchronous and does not call delegate methods, but if you decide to have NSURLConnection use a method that calls delegate methods they will be called on the same thread the NSURLConnection was alloc/init'd in.

Max
Reply With Quote
  #7 (permalink)  
Old 08-15-2008, 06:41 AM
Junior Member
 
Join Date: Aug 2008
Posts: 18
Default

It's all good - I ended up using this method without delegate methods

thanks again for the tutorials man
Reply With Quote
  #8 (permalink)  
Old 08-20-2008, 03:44 PM
dun dun is offline
Junior Member
 
Join Date: Aug 2008
Posts: 3
Default

It works! Thanks a lot. But i have a question. I have also a uiwebview. How do i do the POST request in the web view? So the page where i do the post request to, loads in webView?

Thanks in advance!
Reply With Quote
  #9 (permalink)  
Old 08-20-2008, 05:06 PM
Junior Member
 
Join Date: Jul 2008
Posts: 13
Default

Well that's a more interesting case and may require some serverside manipulation on your end, or an understanding of the Web Document's DOM.

But if you are familiar with dom scripting you could do something like:

[webView stringByEvaluatingJavaScriptFromString:@"document. getElementById('formId').submit()"];

That is assuming your form element looked like <form id="formId"> Replace that javascript with anything, or if you could create a javascript method that did that for you and just call the javascript method in that NSString of the stringByEvaluatingJavaScriptFromString method.
Reply With Quote
  #10 (permalink)  
Old 08-20-2008, 05:26 PM
dun dun is offline
Junior Member
 
Join Date: Aug 2008
Posts: 3
Default

Hello, thanks for your reply, but i don't understand that really. Isn't that for getting POST data from the loaded web page?

What i want to do is the next thing. Bit hard to explain, so i made a little picture.
http://farm4.static.flickr.com/3267/...e0e3fcec_o.jpg

GET variables are by the way too okay. It doesn't NEEDS to be with post.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 12:10 AM.