gpt4 book ai didi

ios - rails : redirect_to 'myapp://' to call iOS app from mobile safari

转载 作者:行者123 更新时间:2023-11-28 22:48:43 25 4
gpt4 key购买 nike

我有一个 native iOS 应用程序,可以通过 myiosapp:// 从 iOS 中的 Mobile Safari 调用。我还有一个简单的 Rails 应用程序,当请求来自移动设备时,它应该重定向到 native 应用程序。这就是我遇到问题的地方 - 我不能 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 中实现它:

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

iphone web app to automatically redirect to app

最佳答案

事实证明,目前有一个简单的解决方案足以满足我的应用程序的上下文。我只需要在 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>

关于ios - rails : redirect_to 'myapp://' to call iOS app from mobile safari,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12525257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com