Skip to content

RMET-5110 ::: added fix do allow open url InAppBrowser and download a file - #46

Closed
OS-ruimoreiramendes wants to merge 1 commit into
mainfrom
fix/RMET-5110/open-url-download
Closed

OS-ruimoreiramendes wants to merge 1 commit into
mainfrom
fix/RMET-5110/open-url-download

Conversation

@OS-ruimoreiramendes

Copy link
Copy Markdown
Contributor

Description

Handle window.open() and file downloads in WKWebView on iOS
On iOS, WKWebView silently ignores window.open() calls and cannot handle file download responses by default. This fix addresses both issues:

  • Injects a WKUserScript at document start that overrides window.open() to navigate within the same WebView, preserving the custom User Agent
  • Implements decidePolicyFor navigationResponse to detect non-displayable MIME types and trigger WKDownload (iOS 14.5+), with a UIActivityViewController to allow the user to save or share the downloaded file

Context

Addresses: https://outsystemsrd.atlassian.net/browse/RMET-5110

Type of changes

  • Fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Refactor (cosmetic changes)
  • Breaking change (change that would cause existing functionality not to work as expected)

Tests

Screenshots (if appropriate)

Checklist

  • Pull request title follows the format RNMT-XXXX <title>
  • Code follows the code style of this project
  • Changes require an update to the documentation
    • Documentation has been updated accordingly

@OS-ruimoreiramendes
OS-ruimoreiramendes requested a review from a team as a code owner April 7, 2026 12:22
@alexgerardojacinto

Copy link
Copy Markdown
Contributor

Injects a WKUserScript at document start that overrides window.open() to navigate within the same WebView, preserving the custom User Agent

@OS-ruimoreiramendes Before it was already possible to navigate within the same web view right?

@alexgerardojacinto

Copy link
Copy Markdown
Contributor

Implements decidePolicyFor navigationResponse to detect non-displayable MIME types and trigger WKDownload (iOS 14.5+), with a UIActivityViewController to allow the user to save or share the downloaded file

@OS-ruimoreiramendes how does the same use case work in Android? Did the downloads already work?

@alexgerardojacinto

Copy link
Copy Markdown
Contributor

@OS-ruimoreiramendes if we're adding any new functionality, which I believe we might be, then we need to consider adding it on Android as well (if it doesn't already exist)

@OS-ruimoreiramendes

Copy link
Copy Markdown
Contributor Author

@alexgerardojacinto as discussed on the call, since this PR introduces download functionality which was not previously supported (and would need to be considered for Android as well), I'll keep this PR open for future reference in case we decide to introduce this feature.
In the meantime, I've created a new PR that addresses the actual issue reported in the support ticket and tracked under https://outsystemsrd.atlassian.net/browse/RPM-6714.

/// Indicates if a download is in progress, to suppress navigation errors caused by download redirects.
private var isDownloadInProgress: Bool = false
/// Retains the active WKDownload to prevent it from being deallocated mid-download.
private var activeDownload: AnyObject?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way these variables are declared suggests that only one download can be in progress at a time. Is it guaranteed that multiple simultaneous downloads can never occur?


func downloadDidFinish(_ download: WKDownload) {
guard let fileURL = downloadDestinationURL else { return }
activeDownload = nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the isDownloadInProgress property here? Currently, it is only set to false when an error occurs.

print("webView: \(delegateName) - \(error.localizedDescription)")
if (error as NSError).code != NSURLErrorCancelled {
let nsError = error as NSError
if isDownloadInProgress && nsError.code == 102 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we replace the magic number 102 with a named constant or add a comment explaining its purpose?

if FileManager.default.fileExists(atPath: fileURL.path) {
try? FileManager.default.removeItem(at: fileURL)
}
downloadDestinationURL = fileURL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we clean up the downloaded files at some point?

@OS-ruimoreiramendes
OS-ruimoreiramendes marked this pull request as draft June 17, 2026 08:29
@OS-pedrogustavobilro

Copy link
Copy Markdown
Contributor

Hey @OS-ruimoreiramendes as the internal JIRA items have been discarded, we're closing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants