I'm wondering how to open a linked pdf file with the turbolinks-ios framework in iOS.
Currently, I'm experiencing the issue that when a turbolinks page links to a pdf or other file, then the link will open in safari rather than the embedded view.
Background
The turbolinks-5 library together with the turbolinks-ios framework provide a way to connect a web application to the native navigation controllers of the corresponding mobile app.
The screenshot is taken from the turbolinks README.
Desired behavior
When clicking a link that refers to a pdf, a seaparate view controller should be pushed to the current navigation controller, such that the user can read the pdf and easily navigate back to the document index.
Observed behavior
The linked pdf is opened in safari rather than within the app. Unfortunately, safari asks for authentication, again. Furthermore, the user has to leave the application.
Intercept the click of the pdf link
For a link to a pdf file, the
didProposeVisitToURL
mechanism is not triggered for the session delegate. Thus, one can't decide from there how to handle the linked pdf.Instead, one could intercept clicking the link by becoming turbolinks' web view's navigation delegate as shown in the README:
Present the pdf view controller
Similarly to presenting the visitable view as shown in the turbolinks-ios demo application, present the pdf view controller:
Or, if you'd like to show other file types as well, call it
fileViewController
rather thanpdfViewController
.PdfViewController
The new view controller inherits from turbolinks' VisitableViewController to make use of the initialization by url.
To get the web view to the correct size, I used
bindFrameToSuperviewBounds
as shown in this stackoverflow answer, but I'm sure there are other methods.Optional: Sharing cookies
If loading the pdf needs authentication, it's convenient to share the cookies with the turbolinks-ios webview as described in the README.
For example, create a
webViewConfiguration
which can be passed to thepdfViewController
:The same
webViewConfiguration
needs to be passed to thesession
(shown above) as well as to the new pdf view controller.Demo