i am using a rhomobile/rhodes app to talk to a web service and display content in a WebView, when i send the login information in a Rho::AsyncHttp.post with the login details and a callback i can see a successful login on the web service and the application gets a cookie that i can puts and view. so开发者_开发知识库 far so good.
however, the next thing i try is to use that cookie to authenticate the WebView, so in the callback:
WebView.set_cookie("10.0.1.190", @params['cookies'])
WebView.navigate("10.0.1.190")
but this redirects me to the login page. the cookie that is sent does not match the cookie in @params['cookies']
, is there a different way to set cookies for ip addresses? or am i doing something wrong, or is this broken in rhodes at the moment? i tried 3.0.0 and 3.0.1 for both android and iphone and they had the same behavior.
turns out WebView.set_cookie doesn't currently work for 3.0.1, but you can set the cookie in javascript... this is the hack i wound up with that seems to work:
def login
WebView.navigate("http://www.mysite.com/blank.html")
Rho::AsyncHttp.post(
:url => "http://www.mysite.com/login?user[username]=joeblow&user[password]=supersecret",
:callback => url_for(:action => :login_callback)
)
render :action => :wait
end
def login_callback
WebView.execute_js("document.cookie='#{@params["cookies"]};expires=Fri, June 10 12:00:00 UTC;path=/;domain=.mysite.com'")
WebView.navigate("http://www.mysite.com")
end
blank.html is just an empty file to get the WebView loaded without setting any cookies.
This is what worked for me without using js. I think it was probably because I was adding multiple cookies, so I ended up adding them one by one instead of all of them at once in the callback.
def login
WebView.navigate("http://www.mysite.com/blank.html")
Rho::AsyncHttp.post(
:url => "http://www.mysite.com/login?user[username]=joeblow&user[password]=supersecret",
:callback => url_for(:action => :login_callback)
)
render :action => :wait
end
def login_callback
cookies = @params["cookies"].split(';');
cookie_str = ""
cookies.each do |c|
c = c + ';'
WebView.set_cookie("10.0.1.190", c)
end
WebView.navigate("http://www.mysite.com")
end
精彩评论