Skip to content

Commit

Permalink
Fix: avoid double percent-encode for url scheme, #4861 (#4860)
Browse files Browse the repository at this point in the history
* fix: avoid double percent-encode for url scheme

Remove manual percent-encoding for URL opened in URL scheme. This will
make URLs with special characters no longer be encoded twice and thus
openable.

See: https://developer.apple.com/documentation/foundation/url/3126806-init

* fix: make URL parsing backward compatible with older macos

Co-authored-by: low-batt <86170219+low-batt@users.noreply.github.com>
  • Loading branch information
xfoxfu and low-batt committed Apr 1, 2024
1 parent 3f02bf0 commit 73c9608
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions iina/PlayerCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,26 @@ class PlayerCore: NSObject {
if str.first == "/" {
openURL(URL(fileURLWithPath: str))
} else {
guard let pstr = str.addingPercentEncoding(withAllowedCharacters: .urlAllowed), let url = URL(string: pstr) else {
Logger.log("Cannot add percent encoding for \(str)", level: .error, subsystem: subsystem)
// For apps built with Xcode 15 or later the behavior of the URL initializer has changed when
// running under macOS Sonoma or later. The behavior now matches URLComponents and will
// automatically percent encode characters. Must not apply percent encoding to the string
// passed to the URL initializer if the new new behavior is active.
var performPercentEncoding = true
#if compiler(>=5.9)
if #available(macOS 14, *) {
performPercentEncoding = false
}
#endif
var pstr = str
if performPercentEncoding {
guard let encoded = str.addingPercentEncoding(withAllowedCharacters: .urlAllowed) else {
print("Cannot add percent encoding for \(str)")
return
}
pstr = encoded
}
guard let url = URL(string: pstr) else {
Logger.log("Cannot parse url for \(pstr)", level: .error, subsystem: subsystem)
return
}
openURL(url)
Expand Down

0 comments on commit 73c9608

Please sign in to comment.