Skip to content

Commit

Permalink
Replace .host() with manual extraction
Browse files Browse the repository at this point in the history
This fixes an issue with respect to ATProtoKit crashing on versions
earlier than iOS 16 and tvOS 16.
  • Loading branch information
MasterJ93 committed Oct 18, 2024
1 parent 2aef6cb commit f93428a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension ATProtoKit {
}

guard let serviceEndpoint = session?.didDocument?.service[0].serviceEndpoint,
let serviceEndpointHost = serviceEndpoint.host() else {
let serviceEndpointHost = serviceEndpoint.hostname() else {
throw ATRequestPrepareError.invalidRequestURL
}

Expand Down
25 changes: 25 additions & 0 deletions Sources/ATProtoKit/Utilities/ExtensionHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,28 @@ extension Data {
return [UInt8](self)
}
}

// MARK: - URL Extension
extension URL {

/// Returns the host component of the URL.
///
/// This is the same as the standard `func host(percentEncoded: Bool = true) -> String?`
/// method, except for the `percentEncoding` argument. This is simply a method that works for
/// iOS and tvOS versions prior to iOS and tvOS 16.
///
///- Returns: The fragment component of the URL
public func hostname() -> String? {
let url = self

// Manually extract the hostname from the URL without using .host().
let hostname = url.absoluteString.split(separator: "//").last?.split(separator: "/").first

// Convert from Substring to a String.
if let hostname {
return String(hostname)
}

return nil
}
}

0 comments on commit f93428a

Please sign in to comment.