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

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 03-11-2009, 03:31 AM   #2 (permalink)
navneetaron
New Member
 
Join Date: Mar 2009
Posts: 1
Default Iphone native app integration with Ruby on Rails backend

Hi,
I'm trying to build an iphone app that works with my ruby on rails web server. I've https based authentication working from web browser.
Using NSDownloadURL , I'm trying to get some data from the server, however the call fails because the credentials have not been provided. Can you please me some pointers/examples of changes on the Rails app as well as on iphone to authenticate with server and get the data

Thanks a lot
Navneet

Quote:
Originally Posted by macteo View Post
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
navneetaron is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,840
Threads: 89,211
Posts: 380,651
Top Poster: BrianSlick (7,129)
Welcome to our newest member, saralo56
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.