Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-25-2008, 04:13 AM   #1 (permalink)
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
Old 03-11-2009, 02:31 AM   #2 (permalink)
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
Old 10-26-2009, 10:37 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: India
Posts: 136
Default

Hi navneetaron,

I am also trying to make an iphone app for ruby on rails backend website. Could you please help me out how you managed to save the user data in session. That is, I want user to remain logged-in throughout the session. How could it be done?
anurag is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,350
Threads: 39,144
Posts: 171,614
Top Poster: smasher (2,577)
Welcome to our newest member, 17make
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:36 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0