Search code examples
phpiphoneobjective-cforum

PHP cross Objective C iPhone


I have a forum here http://forum.castoff.net

I am part way through writing my own iPhone App to go with it..

I am stuck on the Posting Data, Getting Data and logging in stage..

I think i have figured out the posting data to php section... I am not sure how to get data from php page

in PHP I would store a session and cookie so that the browser knows a user is logged in... How would i do this in objective-C ?

Thanks in advance

Lee


Solution

  • This is actually quite easy if you decide to go with UIWebView, but from your description I understand that this is not the solution you're going for. If you want to retrieve the information from the page you can use NSMutableURLRequest, get the page content dumped into NSString and process it any way you want there. Take a look at this example (it covers loading page you're sending POST data):

    NSURL *url = [NSURL URLWithString: [NSString stringWithFormat: @"%@/page.php", appURL]];
    NSString *body = [NSString stringWithFormat: @"uid=%@", uID];
    NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: data];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    [request release];
    NSString *htmlString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    

    And if you decide to go with UIWebView all you have to do is:

    [YourUIWebView loadRequest: request];
    

    The easiest solution would be to make an App with UIWebView and make a mobile version of the theme. That way all you really have to do is launch an app, load the forum main page and let WebView handle everything else from that point on (authorization, maintaining cookies and such).