From 1e07bbc505462d12a649c46e38a99b4b490fe932 Mon Sep 17 00:00:00 2001 From: chan Date: Thu, 14 Mar 2024 00:33:26 +0900 Subject: [PATCH 1/5] fix: change directory share extension --- .../ShareViewController.swift | 0 .../View/ShareExtensionBackGroundView.swift | 0 iBox/iBox.entitlements | 10 ---------- 3 files changed, 10 deletions(-) rename {iBox/ShareExtension => ShareExtension}/ShareViewController.swift (100%) rename {iBox/ShareExtension => ShareExtension}/View/ShareExtensionBackGroundView.swift (100%) delete mode 100644 iBox/iBox.entitlements diff --git a/iBox/ShareExtension/ShareViewController.swift b/ShareExtension/ShareViewController.swift similarity index 100% rename from iBox/ShareExtension/ShareViewController.swift rename to ShareExtension/ShareViewController.swift diff --git a/iBox/ShareExtension/View/ShareExtensionBackGroundView.swift b/ShareExtension/View/ShareExtensionBackGroundView.swift similarity index 100% rename from iBox/ShareExtension/View/ShareExtensionBackGroundView.swift rename to ShareExtension/View/ShareExtensionBackGroundView.swift diff --git a/iBox/iBox.entitlements b/iBox/iBox.entitlements deleted file mode 100644 index ef36994..0000000 --- a/iBox/iBox.entitlements +++ /dev/null @@ -1,10 +0,0 @@ - - - - - com.apple.security.application-groups - - group.com.ibox - - - From 385954d187932464338402673dafecd6bb890191 Mon Sep 17 00:00:00 2001 From: chan Date: Thu, 14 Mar 2024 02:09:05 +0900 Subject: [PATCH 2/5] feat: add metadata model --- ShareExtension/Model/Metadata.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 ShareExtension/Model/Metadata.swift diff --git a/ShareExtension/Model/Metadata.swift b/ShareExtension/Model/Metadata.swift new file mode 100644 index 0000000..8f95deb --- /dev/null +++ b/ShareExtension/Model/Metadata.swift @@ -0,0 +1,12 @@ +// +// Metadata.swift +// iBoxShareExtension +// +// Created by 김찬희 on 2024/03/14. +// + +struct Metadata { + var title: String? + var faviconUrl: String? + var url: String? +} From fff4a6945c87fd0c7129f6a990b14d6113a2a1d4 Mon Sep 17 00:00:00 2001 From: chan Date: Thu, 14 Mar 2024 02:10:12 +0900 Subject: [PATCH 3/5] build: add swiftsoup framework in tuist --- Project.swift | 3 ++- Tuist/Dependencies.swift | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Project.swift b/Project.swift index dd16153..8a7750c 100644 --- a/Project.swift +++ b/Project.swift @@ -22,7 +22,8 @@ class iBoxFactory: ProjectFactory { ] let iBoxShareExtensionDependencies: [TargetDependency] = [ - .external(name: "SnapKit") + .external(name: "SnapKit"), + .external(name: "SwiftSoup") ] private let appInfoPlist: [String: Plist.Value] = [ diff --git a/Tuist/Dependencies.swift b/Tuist/Dependencies.swift index 2c83d41..98b323e 100644 --- a/Tuist/Dependencies.swift +++ b/Tuist/Dependencies.swift @@ -8,9 +8,9 @@ import ProjectDescription let spm = SwiftPackageManagerDependencies([ - .remote(url: "https://github.com/SnapKit/SnapKit.git", requirement: .upToNextMinor(from: "5.0.1")) -], - productTypes: ["SnapKit": .framework] + .remote(url: "https://github.com/SnapKit/SnapKit.git", requirement: .upToNextMinor(from: "5.0.1")), + .remote(url: "https://github.com/scinfu/SwiftSoup.git", requirement: .upToNextMajor(from: "2.7.1")), +], productTypes: ["SnapKit": .framework, "SwiftSoup": .framework] ) let dependencies = Dependencies( From fed560447a9a9a95d01a1646444bb3feaad5c827 Mon Sep 17 00:00:00 2001 From: chan Date: Thu, 14 Mar 2024 02:10:42 +0900 Subject: [PATCH 4/5] feat: title and faviconUrl --- ShareExtension/ShareViewController.swift | 78 ++++++++++++++++++++---- 1 file changed, 67 insertions(+), 11 deletions(-) diff --git a/ShareExtension/ShareViewController.swift b/ShareExtension/ShareViewController.swift index 1815159..adc99de 100644 --- a/ShareExtension/ShareViewController.swift +++ b/ShareExtension/ShareViewController.swift @@ -8,13 +8,15 @@ import UIKit import Social import UniformTypeIdentifiers + import SnapKit +import SwiftSoup @objc(CustomShareViewController) class CustomShareViewController: UIViewController { var backgroundView = ShareExtensionBackGroundView() - var dataURL: String = "" + var dataURL: String? // MARK: - Life Cycle @@ -64,7 +66,7 @@ class CustomShareViewController: UIViewController { self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) }) } - + @objc func openURL(_ url: URL) -> Bool { self.hideExtensionWithCompletionHandler(completion: { _ in self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil) @@ -100,6 +102,51 @@ class CustomShareViewController: UIViewController { } } } + + func fetchAndParseMetadata(from url: URL, completion: @escaping (Metadata) -> Void) { + URLSession.shared.dataTask(with: url) { data, response, error in + guard let data = data, error == nil else { + print("Failed to fetch data: \(String(describing: error))") + return + } + + if let htmlContent = String(data: data, encoding: .utf8) { + do { + let doc: Document = try SwiftSoup.parse(htmlContent) + let title: String? = try doc.title() + + let faviconSelectors = ["link[rel='shortcut icon']", "link[rel='icon']", "link[rel='apple-touch-icon']"] + var faviconUrl: String? = nil + + for selector in faviconSelectors { + if let faviconLink: Element = try doc.select(selector).first() { + if var href = try? faviconLink.attr("href"), !href.isEmpty { + if href.starts(with: "/") { + href = url.scheme! + "://" + url.host! + href + } else if !href.starts(with: "http") { + href = url.scheme! + "://" + url.host! + "/" + href + } + faviconUrl = href + break + } + } + } + + if faviconUrl == nil { + faviconUrl = url.scheme! + "://" + url.host! + "/favicon.ico" + } + + let metadata = Metadata(title: title, faviconUrl: faviconUrl, url: url.absoluteString) + + DispatchQueue.main.async { + completion(metadata) + } + } catch { + print("Failed to parse HTML: \(error.localizedDescription)") + } + } + }.resume() + } } extension CustomShareViewController: ShareExtensionBackGroundViewDelegate { @@ -109,16 +156,25 @@ extension CustomShareViewController: ShareExtensionBackGroundViewDelegate { } func didTapOpenApp() { - let sharedData = dataURL - let url = URL(string: "iBox://\(sharedData)")! - - if openURL(url) { - print("iBox 앱이 성공적으로 열렸습니다.") - } else { - print("iBox 앱을 열 수 없습니다.") + guard let sharedURL = dataURL, let url = URL(string: sharedURL) else { + print("Share extension error") + return } - print(url) + fetchAndParseMetadata(from: url) { metadata in + let encodedTitle = metadata.title?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? "" + let encodedData = metadata.url?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? "" + let encodedFaviconUrl = metadata.faviconUrl?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? "" + let urlString = "iBox://url?title=\(encodedTitle)&data=\(encodedData)&faviconUrl=\(encodedFaviconUrl)" + + print(urlString) + if let openUrl = URL(string: urlString) { + if self.openURL(openUrl) { + print("iBox 앱이 성공적으로 열렸습니다.") + } else { + print("iBox 앱을 열 수 없습니다.") + } + } + } } - } From e26f8f71520a5a9cb050330185adad239d72e704 Mon Sep 17 00:00:00 2001 From: chan Date: Thu, 14 Mar 2024 02:15:11 +0900 Subject: [PATCH 5/5] feat: url decoder --- iBox/Sources/SceneDelegate.swift | 4 +++- iBox/Sources/Shared/URLdecoder.swift | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 iBox/Sources/Shared/URLdecoder.swift diff --git a/iBox/Sources/SceneDelegate.swift b/iBox/Sources/SceneDelegate.swift index ccba580..8d5e068 100644 --- a/iBox/Sources/SceneDelegate.swift +++ b/iBox/Sources/SceneDelegate.swift @@ -31,7 +31,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { print("Opened URL: \(url)") - // 앱이 실행되기 전에 url이 들어오는 경우 Logic + URLdecoder.handleCustomURL(url) } } @@ -62,6 +62,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { print("Opened URL: \(url)") // 앱 실행 중에 url이 들어오는 경우 Logic + + URLdecoder.handleCustomURL(url) } } diff --git a/iBox/Sources/Shared/URLdecoder.swift b/iBox/Sources/Shared/URLdecoder.swift new file mode 100644 index 0000000..bf6e2b7 --- /dev/null +++ b/iBox/Sources/Shared/URLdecoder.swift @@ -0,0 +1,24 @@ +// +// URLdecoder.swift +// iBoxShareExtension +// +// Created by 김찬희 on 2024/03/14. +// + +import Foundation + +class URLdecoder { + + static func handleCustomURL(_ url: URL) { + guard let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return } + + let title = urlComponents.queryItems?.first(where: { $0.name == "title" })?.value + let data = urlComponents.queryItems?.first(where: { $0.name == "data" })?.value + let faviconUrl = urlComponents.queryItems?.first(where: { $0.name == "faviconUrl" })?.value + + print("Title: \(title ?? "N/A")") + print("Data URL: \(data ?? "N/A")") + print("Favicon URL: \(faviconUrl ?? "N/A")") + } + +}