IOS, AFNetworking and Rails / Devise

I tried to find examples and answers on this topic, but everything seemed to be more specific or not quite relevant to my problem. I am new to Rails and the iOS middleware. I am looking to use Rails as a backend for driving a login application. I tried to find sample code for authentication using AFHTTPClient, but was unsuccessful. The iOS code I use is:

- (IBAction)submitLogin:(id)sender { NSString *username = usernameField.text; NSString *password = passwordField.text; [[AFTestClient sharedClient] setAuthorizationHeaderWithUsername:username password:password]; [[AFTestClient sharedClient] postPath:@"login/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success"); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure"); }]; } 

and this is the controller that I installed in Rails with / login corresponding to # login actions:

 class ActivitiesController < ApplicationController before_filter :authenticate_user!, :only => [:login, :index] def login @user = current_user respond_to do |format| format.html {render :text => "#{@user.id}"} format.xml {render :text => "#{@user.id}" } end end def index @user = current_user respond_to do |format| format.html {render :text => "Current user is logged in."} format.xml {render :text => "#{@user.id}" } format.json end end end 

I know that my standard requests that do not require authentication pass, but I assume that the authentication header is not recognized by Devise or anything near these lines.

Any answers would be very helpful, especially examples for similar code performing the same task.

Thanks!

+4
source share
1 answer

Try enabling basic HTTP authentication in your Rails application configuration / initializers / devise.rb:

 config.http_authenticatable = true 

More information on https://github.com/plataformatec/devise/wiki/How-To:-Use-HTTP-Basic-Authentication .

+6
source

All Articles