1

iOSのモバイルSafariからを介して呼び出すことができるネイティブiOSアプリがありますmyiosapp://。また、リクエストがモバイルから送信されたときにネイティブアプリにリダイレクトする必要があるシンプルなRailsアプリもあります。これは私が問題を抱えているところです-私はできませんredirect_to 'myiosapp://

この問題をできるだけ短く説明したいので、無関係な情報を削り取るサ​​ンプルアプリを作成しましたが、同じ問題を再現しています。

これが私の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でこれを実装する方法にこれほど具体的なものはないようです。

モバイルSafariからネイティブiOSアプリ(Quoraなど)にリダイレクトするにはどうすればよいですか?

アプリに自動的にリダイレクトするiPhoneウェブアプリ

4

1 に答える 1

4

現時点で私のアプリのコンテキストに十分な単純なソリューションがあることがわかりました。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>
于 2012-09-24T04:05:47.447 に答える