turbolinks / turbolinks-ios

Native iOS adapter for building hybrid apps with Turbolinks 5
MIT License
881 stars 92 forks source link

Display PDF attachments in VisitableView #54

Closed fiedl closed 8 years ago

fiedl commented 8 years ago

When a turbolinks page links to a pdf or other file that's not handled by turbolinks, currently the link is handled as external link and will open in safari rather than the embedded VisitableView.

Scenario

Consider a documents index. The document links refer to the actual pdfs.

# app/views/documents/index.html.haml
# ...
  = link_to document.title, document.file_url

The user clicks on such a link.

Observed behavior

The linked pdf is opened in safari rather than an embedded VisitableView. Unfortunately, safari asks for authentication, again. Furthermore, the user leaves the application.

Desired behavior

The pdf is displayed within the app, preferably within a VisitableView of turbolinks-ios.

Is this currently possible? Is this something, you would consider to implement?

Workaraounds?

  1. The README describes how to open the external link in a SFSafariViewController. Is this the way to go? Does this work without showing the url or other safari ui elements?
  2. I could have the link send a message to the mobile app via javascript as shown in the README: webkit.messageHandlers.show_pdf.postMessage(pdf_url). But how would I tell the VisitableView to just open that url?

    If I use presentVisitableForSession(session, URL: pdf_url, action: .Advance) as shown in the demo application, it takes a while, and then the pdf is shown as raw:

    bildschirmfoto 2016-08-17 um 13 06 01

  3. I've tried to use an iframe within a documents#show page rendered by turbolinks. But unfortunately, the iframe just shows the first page of the pdf. I suspect this might be a scrolling issue.

Thanks very much in advance for any hint you might have!

fiedl commented 8 years ago

I've posted my workaround at stackoverflow in the hope that somebody knows a better solution.

Please let me know if you consider the problem resolved or if you'd consider implementing a "built-in solution" for the issue.

openpdf

zachwaugh commented 8 years ago

@fiedl I think that's a good solution and close to what we do in Basecamp. We send all URLs that Turbolinks doesn't handle through our Router that decides how to handle them (open a native view controller, open in Safari, etc..). For files, we have a native view controller that downloads them and provides previewing/sharing options.

One note, your FilesViewController doesn't need to have have any connection to Turbolinks, so it can be a plain UIViewController subclass instead of inheriting from Turbolinks.VisitableViewController. Just need to use the same processPool to share cookies if needed, as you noted in your answer.

fiedl commented 8 years ago

Thanks, @zachwaugh

scruff311 commented 6 years ago

@zachwaugh I'm having this same issue, but our Rails app uses anchor tags with an href like /files/[id]/download which gets redirected to an s3.amazonaws url. The turbolinks iOS app is receiving the original url (and failing) but not the AWS one we are interested in (which comes afterward). Any advice on waiting for the redirected url and using that one instead?