Rails:redirect_to'myapp://'从移动safari调用iOS应用程序 [英] Rails: redirect_to 'myapp://' to call iOS app from mobile safari
问题描述
我有一个原生的iOS应用程序,可以通过 myiosapp://
从iOS中的Mobile Safari调用。我还有一个简单的Rails应用程序,当请求来自移动设备时应该重定向到本机应用程序。这就是我遇到问题的地方 - 我不能 redirect_to'myiosapp://
I have a native iOS app that can be called from Mobile Safari in iOS via myiosapp://
. I also have a simple Rails app that SHOULD redirect to the native app when the request is coming from mobile. This is where I am I having problems - I can't redirect_to 'myiosapp://
我想要尽可能简短地描述这个问题,所以我制作了一个样本应用程序来消除不相关的信息,但复制了同样的问题。
I want to describe this problem as short as possible, so I made a sample app that shaves away the irrelevant information, but replicates the same problem.
这是我的routes.rb:
Here's my routes.rb:
MyRailsApp::Application.routes.draw do
root :to => 'redirect#index'
end
这里是redirect_controller.rb:
And here's redirect_controller.rb:
class RedirectController < ApplicationController
def index
if request_from_mobile?
redirect_to "myiosapp://"
else
redirect_to "/default.html"
end
end
private
def request_from_mobile?
request.user_agent =~ /Mobile|webOS/
end
end
每当我运行 rails server
并转到 localhost :: 3000
时,我明白了:
Whenever I run rails server
and go to localhost::3000
, I get this:
Started GET "/" for 127.0.0.1 at 2012-09-21 14:00:52 +0800
Processing by RedirectController#index as HTML
Redirected to motionapp://
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
[2012-09-21 14:00:52] ERROR URI::InvalidURIError: bad URI(absolute but no path): motionapp://
/Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:1202:in `rescue in merge'
/Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:1199:in `merge'
/Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpresponse.rb:220:in `setup_header'
/Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpresponse.rb:150:in `send_response'
/Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:110:in `run'
/Users/dev/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
在发布之前,我已经看过很多类似的问题,但似乎没有更具体的我如何在Rails中实现这一点:
Before posting this, I've already seen a number of similar problems, but none seemed to be more specific to how I can implement this in Rails:
如何从Mobile Safari重定向到原生iOS应用程序(如Quora)?
推荐答案
原来有一个简单的解决方案,足以满足我的应用程序的上下文。我只需要在javascript中处理重定向。其他解决方案仍然受欢迎。 :)
Turns out there's a simple solution that is enough for the context of my app as of the moment. I just needed to handle the redirect in javascript. Other solutions are still welcome. :)
<html><head>
<script type="text/javascript">
var userAgent = window.navigator.userAgent;
if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
window.location = "myiosapp://"
}
</script>
</head>
<body>
Some html page
</body>
</html>
这篇关于Rails:redirect_to'myapp://'从移动safari调用iOS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!