• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Open safari and send post variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Open safari and send post variables


  • Subject: Re: Open safari and send post variables
  • From: Conor Dearden <email@hidden>
  • Date: Fri, 09 Jun 2006 14:57:46 +0200

> Your code of course works... but I need to open safari and print the variables
> I sent (with php).
> I know how to do it with php, but i write:

What is it exactly your trying to achieve? From your post I am guessing you
want to open the resulting page from the post in Safari.  Do the post with
NSURLConnection as mentioned before. Gather all the data that comes back,
when the connection is done loading, write the data to a temp file on disk
and load that file into Safari with openURL:

Here is the code: theConnection, receivedData are instance variables. You
have to add to your header.

- (void)startPost {

  NSURL *someUrl = [NSURL URLWithString:@"http://www.test.org/test.php";];

NSMutableURLRequest* postURLRequest = [NSMutableURLRequest requestWithURL:
someUrl];

//Make it a post
    [postURLRequest setHTTPMethod:@"POST"];

//Set the body for the post
    [postURLRequest setHTTPBody:[@"test=foo"
dataUsingEncoding:NSISOLatin1StringEncoding]];

    //Start the connection with the post
    theConnection=[[NSURLConnection alloc] initWithRequest:postURLRequest
delegate:self];

    if (theConnection) {
        receivedData=[[NSMutableData data] retain];
    }

}


- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
    //redirects and such
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData
*)data
{
    // append the new data to the receivedData
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{

    // release the connection, and the data object
    [connection release];
    [receivedData release];
    receivedData = nil;

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //get a temp file path
    NSString *tempPath = [NSString stringWithFormat:@"%@/myFile.html",
NSTemporaryDirectory()];

    //write the data out
    [receivedData writeToFile:tempPath options:nil error:nil];

//laod it in safari
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:tempPath]];

/*
    //if you want to debug the htmlsource code you can use this functions
    NSString *htmlSource = [[[NSString alloc] initWithData:receivedData
encoding:NSISOLatin1StringEncoding] autorelease];
    NSLog(@"%@", htmlSource);
*/

    // release the connection, and the data object
    [connection release];
    [receivedData release];
    receivedData = nil;
}


Conor
http://www.bruji.com/


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Open safari and send post variables
      • From: Yorh <email@hidden>
References: 
 >Re: Open safari and send post variables (From: Yorh <email@hidden>)

  • Prev by Date: launch cocoa app with argument
  • Next by Date: Re: Open safari and send post variables
  • Previous by thread: Re: Open safari and send post variables
  • Next by thread: Re: Open safari and send post variables
  • Index(es):
    • Date
    • Thread