Rails:redirect_to“myapp://”从移动Safari浏览器调用iOS应用程序

我有一个原生的iOS应用程序,可以通过myiosapp://从iOS移动Safari中调用。 我也有一个简单的Rails应用程序,当请求来自移动时,应该redirect到本地应用程序。 这是我的问题 – 我不能redirect_to 'myiosapp://

我想尽可能简短地描述这个问题,所以我创build了一个示例应用程序来删除不相关的信息,但重复了同样的问题。

这是我的routes.rb:

 MyRailsApp::Application.routes.draw do root :to => 'redirect#index' end 

这里是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 ,我得到这个:

 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中实现:

如何从移动Safariredirect到本机iOS应用程序(如Quora)?

iPhonenetworking应用程序自动redirect到应用程序

结果发现有一个简单的解决scheme对于我的应用程序的上下文就足够了。 我只需要处理JavaScript中的redirect。 其他解决scheme仍然受欢迎。 🙂

 <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>