2012-08-01 57 views
0

我已经参加了Michael Hartl的Rails 3 Tutorial应用程序,并在几个领域对其进行了扩展。但是,我保持登录和会话处理相同。我想与iPhone应用程序接口,但我不知道如何。我看过RestKit和Objective Resource,但认为我会推出自己的产品。我一直在用cURL进行测试,但迄今为止没有运气。我一直在使用这个命令Rails 3教程cURL/iOS登录

curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://www.example.com/signin -d "{'session' : { 'email' : '[email protected]', 'password' : 'pwd'}}" -c cookie 

正如在3教程中,我使用的会话Rails的。

这些路线:

match '/signin', :to => 'sessions#new' 
match '/signout', :to => 'sessions#destroy' 

这是控制器:

class SessionsController < ApplicationController 
def new 
@title = "Sign in" 
end 

def create 
user = User.authenticate(params[:session][:email], 
         params[:session][:password]) 
if user.nil? 
    flash.now[:error] = "Invalid email/password combination." 
    @title = "Sign in" 
    render 'new' 
else 
    sign_in user 
    redirect_back_or user 
end 
end 

def destroy 
sign_out 
redirect_to root_path 
end 
end 

没有模型和你的表单登录。这里是表格的HTML:

<h1>Sign In</h1> 
<%= form_for(:session, :url => sessions_path) do |f| %> 
<div class="field"> 
<%= f.label :email %></br> 
<%= f.text_field :email %> 
</div> 
<div class="field"> 
<%= f.label :password %></br> 
<%= f.password_field :password %> 
</div> 
<div class="actions"> 
<%= f.submit "Sign in" %> 
</div> 
<% end %> 

<p> New user? <%= link_to "Sign up now!", signup_path %></p> 

对不起,如果这是太多的信息,我想尽可能地给。

基本上,我想能够从本地iPhone应用程序访问我的Rails数据库。如果有人有关于如何登录,存储会话,然后拨打其他网站的建议,我将不胜感激。

但是,如果这是不可能的,一个工作cURL请求可能会让我朝着正确的方向前进。谢谢!

回答

1

我正面临着类似的情况,这使我得出了这个计算器职位:

[http://stackoverflow.com/questions/7997009/rails-3-basic-http-authentication-vs-authentication-token-with-iphone][1] 

基本上,你可以使用基本的HTTP认证与铁轨把事情简单化。

这里的控制器的例子:

class PagesController < ApplicationController 

    def login 
    respond_to do |format| 
     format.json { 
     if params[:user] and 
      params[:user][:email] and 
      params[:user][:password] 
      @user = User.find_by_email(params[:user][:email]) 
      if @user.valid_password?(params[:user][:password]) 
      @user.ensure_authentication_token! 
      respond_to do |format| 
       format.json { 
       render :json => { 
        :success => true, 
        :user_id => @user.id, 
        :email => @user.email 
        }.to_json 
       } 
      end 
      else 
      render :json => {:error => "Invalid login email/password.", :status => 401}.to_json 
      end 
     else 
      render :json => {:error => "Please include email and password parameters.", :status => 401}.to_json 
     end 
     } 
    end 
    end 

然后对事物的iPhone/Objective-C的一面,你可以使用ASIHTTPRequest库和JSONKit库:

http://allseeing-i.com/ASIHTTPRequest/ 

https://github.com/johnezang/JSONKit/ 

一旦你有所有前面提到的安装在xcode中,然后访问rails控制器,得到响应为json,并在objective-c中处理它很简单:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/pages/login.json", RemoteUrl]]; 
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request addRequestHeader:@"Content-Type" value:@"application/json"]; 
[request setRequestMethod:@"POST"]; 
[request appendPostData:[[NSString stringWithFormat:@"{\"user\":{\"email\":\"%@\", \"password\":\"%@\"}}", self.emailField.text, self.passwordField.text] dataUsingEncoding:NSUTF8StringEncoding] ]; 
[request startSynchronous]; 

//start 
[self.loginIndicator startAnimating]; 

//finish 
NSError *error = [request error]; 
[self setLoginStatus:@"" isLoading:NO]; 

if (error) { 
    [self setLoginStatus:@"Error" isLoading:NO]; 
    [self showAlert:[error description]]; 
} else { 
    NSString *response = [request responseString]; 

    NSDictionary * resultsDictionary = [response objectFromJSONString]; 


    NSString * success = [resultsDictionary objectForKey:@"success"]; 


    if ([success boolValue]) { 
     .... 

我刚刚完成了一个Rails/iphone应用程序,并带有大量的Rails调用,所以它绝对是可行的,并且是一次学习体验。

+0

这可能是一个愚蠢的问题,但我还是比较新的Rails。 format.json到底做了什么?这是否允许我将参数作为JSON发送?如果我现在format.js,将其改为format.json拧什么了? – NSchulze 2012-08-01 18:27:15

+0

是的,它发送参数为JSON。 Json是JavaScript的一个子集,尽管不是所有的JavaScript响应都是json响应。 Json可以被JavaScript读取,并且它是一种将数据从服务器传递到JavaScript的快速而有效的方法。 – JohnMerlino 2012-08-01 18:31:13

+0

很酷。那么是我的cURL命令不工作的原因,因为它是format.js?还是我在那里的基地? – NSchulze 2012-08-01 21:14:31