launching mobile safari with a POST request

Discussion in 'iPhone/iPad Programming' started by drivefast, Aug 21, 2008.

  1. macrumors regular

    Joined:
    Mar 13, 2008
    #1
    was anybody successful in creating a POST request to be submitted to mobile safari? i feel like i'm doing the right thing, yet the server receives a GET instead of a POST.

    Code:
    #define BASE_URL @"http://m.someserver.com"
    
    // create url object
    NSString *urlString; NSURL *url;
    urlString = [NSString stringWithFormat: @"%@/showpost.php", BASE_URL];
    url = [NSURL URLWithString: urlString];
    
    // create the request
    NSMutableURLRequest *postRq = [NSMutableURLRequest requestWithURL: url];
    [postRq setHTTPMethod: @"POST"];
    NSLog(@"method: %@", [postRq HTTPMethod]);
    		
    // call safari with the url thus built
    [[UIApplication sharedApplication] openURL: url];
    
    my showpost.php test page is set to display the request method, the headers, and all this background stuff. it shows as it received a GET. i unsuccessfully tried adding data to the request body, forcing the content-type header to look like the request comes in from an html form, and PUT for the sake of it. do POST requests at least work with NSURLConnections, in your experience?

    thanks everybody.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I don't think it's possible to send a post request to Safari. If you need that you'd need to use UIWebView or NSURLConnection in your own app.
     
  3. macrumors 6502a

    Joined:
    Nov 9, 2007
    #3
    Yes, it doesn't make much sense to send somebody to Safari using a POST - they should be GETting something. If you just need to submit data to a server, create a POST request inside of your app as suggested above.
     
  4. thread starter macrumors regular

    Joined:
    Mar 13, 2008
    #4
    well, adding a web view was pretty straightforward. now the delicate part is that every link in the web view will also open in the web view. at one point i need to get out of my app and use safari. anybody knows how to do that? i guess hunting for <a href=...> tags in the response, but how can you get the response?... hmm.
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  6. thread starter macrumors regular

    Joined:
    Mar 13, 2008
    #6
    whoa. good idea. thanks. :cool:
     

Share This Page