Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ShareExtension/Sources/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ extension CustomShareViewController: ShareExtensionBackGroundViewDelegate {
return
}

let urlString = "iBox://url?data=\(sharedURL)"
let urlString = "iBox://url?data=\(sharedURL)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""

if let openUrl = URL(string: urlString) {
if self.openURL(openUrl) {
Expand All @@ -143,6 +143,7 @@ extension CustomShareViewController: ShareExtensionBackGroundViewDelegate {
}
} else {
print("url error")
// 해당 url은 사용할 수 없음을 보여주는 뷰를 만들어야함.
}
}
}
Expand Down
28 changes: 19 additions & 9 deletions iBox/Sources/Shared/AddBookmarkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class AddBookmarkManager {

private func update(with data: (title: String?, data: String?, faviconUrl: String?)) {
DispatchQueue.main.async {
self.incomingTitle = data.title
self.incomingData = data.data
self.incomingFaviconUrl = data.faviconUrl
self.incomingTitle = data.title?.removingPercentEncoding
self.incomingData = data.data?.removingPercentEncoding
self.incomingFaviconUrl = data.faviconUrl?.removingPercentEncoding
}
}

Expand All @@ -40,21 +40,31 @@ class AddBookmarkManager {
}

private func extractDataParameter(from url: URL) -> String? {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
let queryItems = components.queryItems else {
let urlString = url.absoluteString

guard let range = urlString.range(of: "url?data=") else {
return nil
}
return queryItems.first { $0.name == "data" }?.value

let dataParameter = urlString[range.upperBound...]
return String(dataParameter).removingPercentEncoding
}

private func fetchWebsiteDetails(from url: URL) {
let task = URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
guard let data = data, error == nil,
let html = String(data: data, encoding: .utf8) else {
guard let data = data, error == nil else {
print("Error downloading HTML: \(String(describing: error))")
return
}

let encodingName = (response as? HTTPURLResponse)?.textEncodingName ?? "utf-8"
let encoding = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding(encodingName as CFString)))

guard let html = String(data: data, encoding: encoding) else {
print("Failed to decode data with encoding: \(encodingName)")
return
}

self?.parseHTML(html, url)
}
task.resume()
Expand All @@ -63,7 +73,7 @@ class AddBookmarkManager {
func navigateToAddBookmarkView(from url: URL, in tabBarController: UITabBarController) {
guard url.scheme == "iBox", let urlString = extractDataParameter(from: url) else { return }
guard let url = URL(string: urlString) else {
print("Invalid URL")
print("Invalid URL \(urlString)")
return
}

Expand Down
4 changes: 2 additions & 2 deletions iBox/Sources/Web/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class WebViewController: BaseViewController<WebView>, BaseViewControllerProtocol
extension WebViewController: WebViewDelegate {

func pushAddBookMarkViewController(url: URL) {
AddBookmarkManager.shared.incomingData = url.absoluteString
let encodingURL = url.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""

if let iBoxUrl = URL(string: "iBox://url?data=" + url.absoluteString) {
if let iBoxUrl = URL(string: "iBox://url?data=" + encodingURL) {
if let tabBarController = findMainTabBarController() {
AddBookmarkManager.shared.navigateToAddBookmarkView(from: iBoxUrl, in: tabBarController)
}
Expand Down