9

I have a situation in which I am using a POST method to host a URL in the safari browser in the simulator. The web page is opened in safari after launching my iOS app in the simulator. To launch safari i have used [[UIApplication sharedApplication] openURL: ...

The post method is as follows:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://***some client url***.jsp"]];

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", userName, password];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];`


NSURLConnection* _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_urlConnection start];

this piece of code works fine when using UIWebView. But i want to launch Safari using this "request" which has appended username & password data from the POST method.

To launch Safari i must call this code:

[[UIApplication sharedApplication] openURL:request];

But this throws a warning obviously because "request" is of type NSMutableURLRequest. "Semantic Issue: Incompatible pointer types sending 'NSMutableURLRequest *' to the parameter of type 'NSURL *"...

I cannot even use [[UIApplication sharedApplication] openURL:request.URL]; since this will give me the URL which is not appended with username & password(with out POST method).

I want to know how to typecast/convert this request(NSMutableURLRequest) so that I can accommodate it in the [[UIApplication sharedApplication] openURL...

Hope i am quite clear with my question. Any help will be highly appreciated. Thanks in advance !!

taus-iDeveloper
  • 681
  • 2
  • 8
  • 22

3 Answers3

5

The recommended approach is to use UIWebView in your application and create post request using NSURLRequest and NSURLConnection. It is pretty easy to create a post request and fire the request. Since you have already figured this out and you dont want to use this approach, only alternative is by creating a temporary html file as mentioned here. You can try with the approach they are following and check if it is working in your case.

As per that link, you have to write a temporary html file. This html file will have an onLoad() javascript which immediately sends the POST, and a button for users that don't have javascript enabled. You can create NSURL object as [NSURL fileURLWithPath:path]; from this file.

Another solution is by creating a one time key from the webserver and pass it to safari via HTTPS request as mentioned here.

Community
  • 1
  • 1
iDev
  • 23,310
  • 7
  • 60
  • 85
3

You cannot pass POST data when launching safari through the openURL method, however you could pass POST data if you use your own UIWebView inside your application instead, like so:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://***some client url***.jsp"]];

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", userName, password];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];`

[myUIWebView loadRequest:request];
foggzilla
  • 1,705
  • 11
  • 15
2

From browser you cant make POST request while adding data into Body. If it is an URL request, where you can add authentication as parameter in header of request, That URL you can open in safari.

Hope i am clear.

iCreative
  • 1,499
  • 1
  • 9
  • 22
  • this is correct, `openURL` only takes a `URL` as a parameter and therefore there is no way to tack on extra data that you've placed in your request – yfrancis Nov 05 '12 at 04:17
  • @iCreative: Its a URL request to which i am adding the POST method so that the username & password are not visible in the URL after log-in. This is not user authentication, its for data encoding. I wanna know how to call the NSMutableURLRequest variable from "[[UIApplication sharedApplication] openURL: " .. hope i am quite clear. – taus-iDeveloper Nov 05 '12 at 04:36
  • I want to tell you that, you can use OpenURL method only to open URLs. It just like you open google.com in your browser and you open it using your application and call safari browser to open "google.com". It is not passing parameter of any type in request body... Hope i am clear. – iCreative Nov 05 '12 at 12:03
  • 1
    UIApp::openURL only does 'GET requests', no matter what you set! :) try @foggzilla's solution or rethink if your solution is ok – Daij-Djan Nov 09 '12 at 21:58
  • It cannot be done. As everyone else has mentioned, you can no more make a POST request through `openURL` than you can by typing one into the address bar of a browser. @ACB's "temporary web page" solution is clever, and frankly the only way I could envision this working. However, it would make for a pretty poor user experience even so, and I'd be more apt to go with the offered solutions that employ a `UIWebView` approach. – KevinH Nov 12 '12 at 02:59