iOS,AFNetworking和Rails/Devise

Chr*_*ris 4 ruby-on-rails devise ios afnetworking

我已经尝试过搜索关于这个主题的例子和答案,但所有这些似乎都更具体或与我的问题不完全相关.我是Rails和中级iOS开发人员的新手.我希望使用Rails作为后端来驱动基于用户登录的应用程序.我试图找到如何使用AFHTTPClient进行设计验证的示例代码,但没有成功.我使用的iOS代码是:

- (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");
}];
}
Run Code Online (Sandbox Code Playgroud)

这是我在Rails中设置的控制器,其中/ login匹配活动#login:

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
Run Code Online (Sandbox Code Playgroud)

我知道我不需要身份验证的标准请求正在通过但是我假设认证标头没有被Devise识别或者其他类似的东西.

任何响应都将受到高度赞赏,尤其是类似代码完成相同任务的示例.

谢谢!

小智 6

尝试在Rails App的config/initializers/devise.rb中启用基本的http身份验证:

config.http_authenticatable = true
Run Code Online (Sandbox Code Playgroud)

有关https://github.com/plataformatec/devise/wiki/How-To:-Use-HTTP-Basic-Authentication的更多信息.