|
|||
|
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 ] ]; 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 ]; Code:
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; 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 |
|
|||
|
After you're done with the synchronous request you can call a delegate method, even if you're in another thread.
|
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|