Alex Pelletier
Alex Pelletier

Reputation: 461

Xcode Call Web PHP File with parameters?

I have been working on networking my ios app. I need to call a php file on my website and then get information back. I have been using this tutorial but it is out of date and unsupported now.

http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

I have been looking at the MKNetworkKit for a bit and i think that is what i need to use but i am not sure how to implement it.

Upvotes: 1

Views: 2328

Answers (2)

Gabe Rainbow
Gabe Rainbow

Reputation: 3738

Type in "NSURLConnection" into help. Some good examples there.

The easiest is doing a synchronous call with NSURLConnection.

Which is a blocking call until your server responds and closes.

    NSURL *url = [NSURL URLWithString:@"http://makemyday.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSError *error;
    NSURLResponse *response;
    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

Better methods for user experience are asynch using a delegate. I found the following satisfying, since one can add a time out of 5, 10, 20, 60 seconds whatever.

NSURLRequest *theRequest = [NSURLRequest requestWithURL: url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:**20.0**];

NSURLDownload  *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:**self**]; // NOTE THE SELF FOR THE CALLBACks

if (!theDownload) {
   //OOPS
}

That use the following callback methods for fail, success/finish and filename

- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename;
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
- (void)downloadDidFinish:(NSURLDownload *)download;
- (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path

Upvotes: 0

user511
user511

Reputation: 154

I think most people agree that AFNetworking is the best suited library for this. There's even a really nice raywenderlich.com tutorial on how to use it.

For instance, I use it this way to get the contents of a link with parameters (makemyday.com/web/export.php?id=345) into a UIWebView:

- (IBAction)searchForNearbyButtonPressed:(id)sender {
    NSURL *baseURL = [NSURL URLWithString:@"http://makemyday.com"];
    NSDictionary *parameters = @{@"id": @"345"};
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
    [client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [client getPath:@"/web/export.php"
         parameters:parameters
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"response=%@", [operation.response allHeaderFields]);
                [self.webView loadData:responseObject  MIMEType:operation.response.MIMEType textEncodingName:operation.response.textEncodingName baseURL:operation.response.URL];
            }
            failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"error=%@", error.description);
            }
     ];

}

Upvotes: 1

Related Questions