mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-09-30 07:21:54 +00:00
Fix handling of Twitter link
This commit is contained in:
parent
6aea60b644
commit
11a7ade82d
2 changed files with 11 additions and 16 deletions
|
@ -23,6 +23,16 @@ private struct SafariRouter: ViewModifier {
|
||||||
})
|
})
|
||||||
.onAppear {
|
.onAppear {
|
||||||
routerPath.urlHandler = { url in
|
routerPath.urlHandler = { url in
|
||||||
|
if url.absoluteString.contains("@twitter.com"), url.absoluteString.hasPrefix("mailto:") {
|
||||||
|
let username = url.absoluteString
|
||||||
|
.replacingOccurrences(of: "@twitter.com", with: "")
|
||||||
|
.replacingOccurrences(of: "mailto:", with: "")
|
||||||
|
let twitterLink = "https://twitter.com/\(username)"
|
||||||
|
if let url = URL(string: twitterLink) {
|
||||||
|
UIApplication.shared.open(url)
|
||||||
|
return .handled
|
||||||
|
}
|
||||||
|
}
|
||||||
guard preferences.preferredBrowser == .inAppSafari, !ProcessInfo.processInfo.isiOSAppOnMac else { return .systemAction }
|
guard preferences.preferredBrowser == .inAppSafari, !ProcessInfo.processInfo.isiOSAppOnMac else { return .systemAction }
|
||||||
// SFSafariViewController only supports initial URLs with http:// or https:// schemes.
|
// SFSafariViewController only supports initial URLs with http:// or https:// schemes.
|
||||||
guard let scheme = url.scheme, ["https", "http"].contains(scheme.lowercased()) else {
|
guard let scheme = url.scheme, ["https", "http"].contains(scheme.lowercased()) else {
|
||||||
|
|
|
@ -13,22 +13,7 @@ public struct HTMLString: Decodable, Equatable, Hashable {
|
||||||
public init(from decoder: Decoder) {
|
public init(from decoder: Decoder) {
|
||||||
do {
|
do {
|
||||||
let container = try decoder.singleValueContainer()
|
let container = try decoder.singleValueContainer()
|
||||||
var tempHtmlValue = try container.decode(String.self)
|
htmlValue = try container.decode(String.self)
|
||||||
|
|
||||||
let twitterUrl = "@twitter.com"
|
|
||||||
let twitPattern = #"@+[a-zA-Z0-9(_).-]{1,}@twitter.com"#
|
|
||||||
let regex = try Regex(twitPattern)
|
|
||||||
|
|
||||||
for twitterRange in tempHtmlValue.ranges(of: regex).reversed() {
|
|
||||||
let unparsedHandle = tempHtmlValue[twitterRange.lowerBound...twitterRange.upperBound]
|
|
||||||
let twitterHandle = unparsedHandle.dropFirst().dropLast(twitterUrl.count + 1)
|
|
||||||
let twitterUrlString = "<a href=\"https://twitter.com/\(twitterHandle)\">\(unparsedHandle)</a>"
|
|
||||||
|
|
||||||
tempHtmlValue = tempHtmlValue.replacingOccurrences(of: unparsedHandle, with: twitterUrlString, options: .caseInsensitive,
|
|
||||||
range: twitterRange.lowerBound..<tempHtmlValue.endIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
htmlValue = tempHtmlValue
|
|
||||||
} catch {
|
} catch {
|
||||||
htmlValue = ""
|
htmlValue = ""
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue