iOS “Add to Home Screen” - 文件下载将用户踢出

标签 ios ruby-on-rails download carrierwave iphone-standalone-web-app

我需要一个有效的 session 才能从我的网络应用程序下载文件。

当我在我的 iOS 设备上打开我的网络应用程序、登录并尝试从我的网站下载文件 (PDF) 时,我被踢出并改为在 Safari 中打开链接。用户必须从 Safari 重新登录才能下载页面。

如何强制文件在网络应用程序中打开并触发下载表单?

我正在使用 Carrierwave 并且在 Controller 中我有这个来触发下载:

class DocumentsController < ApplicationController

  # .. code omitted
  def download
    if @document.name.file.exists?
      send_file @document.name.path, x_sendfile: true
    else
      redirect_to documents_url, notice: t(:file_not_found)
    end
  end
end

更新:我找到了直接在浏览器中打开文件的半工作解决方案:

$(document).ready ->
  if 'standalone' of window.navigator and window.navigator.standalone
    # For iOS Apps
    $('a').on 'click', (e) ->
      e.preventDefault()
      new_location = $(this).attr('href')
      if new_location != undefined and new_location.substr(0, 1) != '#' and $(this).attr('data-method') == undefined
        window.location = new_location
      return
  return

通过使用这段代码,我们强制以全屏模式(网络应用程序)运行的 iOS 用户打开应用程序内的所有 a 链接。但问题是,如果用户打开文件,将没有“后退”按钮。由于 iPhone 用户缺少物理后退按钮,他们将不得不强制重启整个应用程序才能退出显示的文件。

最佳答案

使用您发布的代码,而不是将整个页面内容写入正文,您可以将其输入到一个单独的 div 中,并确保有一个仅适用于 iOS 设备的“后退”按钮:

$(document).ready ->
  if 'standalone' of window.navigator and window.navigator.standalone
    # For iOS Apps
    $('a').on 'click', (e) ->
      e.preventDefault()
      new_location = $(this).attr('href')
      if new_location != undefined and new_location.substr(0, 1) != '#' and $(this).attr('data-method') == undefined
        window.location = new_location
      return
  return

关于iOS “Add to Home Screen” - 文件下载将用户踢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332917/

相关文章:

asp.net - 下载文件/另存为.Net 不工作

ios - 如何从自定义 CollectionViewController 的 didSelectItemAtIndexPath 实例化 ViewController,它是 tableViewCell 的一部分

ios - 不兼容的指针类型 Xcode

ruby-on-rails - Prawn::Errors::IncompatibleStringEncoding:您的文档包含与 Windows-1252 字符集不兼容的文本

ruby-on-rails - kEND和$ end有什么区别?

audio - 获取YouTube的所有媒体链接,而无需使用flashgot,youtube-dl或任何工具

python - 下载网站中的所有文件

ios - 从 UIActivity Controller 中删除保存视频选项

ios - TextView和TextField的单个扩展以添加工具栏

ruby-on-rails - rspec & Rails 3 找不到模型对象