2012-09-21 99 views
0

我有一個本機iOS應用程序,可以通過myiosapp://從iOS移動Safari中調用。我還有一個簡單的Rails應用程序,當請求來自移動設備時,應該重定向到本機應用程序。這是我的地方我有問題 - 我不能redirect_to 'myiosapp://Rails:redirect_to'myapp://'從移動Safari瀏覽器調用iOS應用程序

我想描述這個問題儘可能短,所以我做了一個示例應用程序,削減不相關的信息,但複製相同的問題。

這裏是我的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中實現這一點:

How to redirect from Mobile Safari to Native iOS app (like Quora)?

iphone web app to automatically redirect to app

回答

2

原來有一個簡單的解決方案是不夠的時刻我的應用程序的情況下。我只需要處理JavaScript中的重定向。其他解決方案仍然受歡迎。 :)

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

什麼是關於應用程序未安裝? – chris

+0

它在PHP中工作嗎? –

相關問題