Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Infinote Pinboard
for Todos and Notes
(free)

picplz
(free)

poG
($2.99)

VintageChart+
by Wine Spectator
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 08-25-2008, 04:13 AM   #1 (permalink)
macteo
New Member
 
Join Date: Aug 2008
Posts: 5
Default NSURLCredential and HTTPBasic

Hi,
i'm trying to build an application that interact with a Ruby on Rails backend.
Actually i have a problem with authentication.

I've create a controller that populate a table from an xml downloaded file. If you click on edit button you can delete rows and there's a call to the sever to delete the record on the database. Everything works, but if i reactivate the authentication filter on the server i cannot authenticate with the code written below.
Actually the server do a redirect to compile a form with login and password.

The authentication requested is HTTP Basic and i've confurated the server to accept every combination on username and password.

Code:
 
authenticate_or_request_with_http_basic do |username, password|
      true
end

This is my controller code:


Code:
 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (editingStyle == UITableViewCellEditingStyleDelete) {       
       
        int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
        NSString *objectId = [[stories objectAtIndex: storyIndex] objectForKey: @"id"];
       
        NSUInteger index = [objectId intValue];
        NSString *preUrl = [NSString stringWithFormat:@"http://localhost:3000/admin/admin_page/destroy/%d", index];
        NSLog(@"Destroy story at index: %@", preUrl);
        NSURL *url = [NSURL URLWithString: preUrl];
 
       
        NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:@"macteo"
                                                            password:@"password"
                                                            persistence:NSURLCredentialPersistenceForSession];
                                                           
        NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"localhost"
                                                                    port:3000
                                                                    protocol:@"http"
                                                                    realm:nil
                                                                    authenticationMethod: NSURLAuthenticationMethodDefault];
       
       
        [[NSURLCredentialStorage sharedCredentialStorage]   setCredential:userCredentials
                                                            forProtectionSpace:space];
       
        NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
        /* [request setHTTPBody:[[self xml] dataUsingEncoding:NSUTF8StringEncoding]]; */
       
        NSURLResponse * response;
        NSError * error;
       
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        // We're ignoring all the outputs because we don't really care.
       
       
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

This is the Ruby on Rails serve log

Code:
Processing AdminPageController#destroy (for 127.0.0.1 at 2008-08-24 12:50:47) [post]
  Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo
SGFzaHsABjoKQHVzZWR7AA==--94d9d598af90877c999f52867ccabb58fd6b0c74
  Parameters: {"action"=>"destroy", "id"=>"8", "controller"=>"admin_page"}
Filter chain halted as [:http_basic_authentication] rendered_or_redirected.
Completed in 0.00092 (1090 reqs/sec) | Rendering: 0.00063 (68%) | DB: 0.00000 (0%) | 401 Unauthorized [http://localhost/admin/admin_page/destroy/8]
Thanks,
MacTeo
macteo is offline   Reply With Quote
 
» Advertisements
» Stats
Members: 52,419
Threads: 53,155
Posts: 226,643
Top Poster: BrianSlick (3,598)
Welcome to our newest member, Iar
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.