はじめに
URLからWebサイトのタイトルを取得することがあり、実装したので記録しておきます。
サンプルアプリ
実装
ContentView
import SwiftUI
struct ContentView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
VStack(spacing: 20) {
TextField("URLを入力してください", text: $viewModel.url, axis: .vertical)
.textFieldStyle(.roundedBorder)
Text(viewModel.title)
}
.padding(20)
}
}
ViewModel
import Combine
import Foundation
final class ViewModel: ObservableObject {
@Published var url: String = ""
@Published var title: String = ""
private var cancellables = Set<AnyCancellable>()
init() {
$url.sink { [weak self] _ in
guard let self else { return }
self.fetchURLTitle()
}
.store(in: &cancellables)
}
private func fetchURLTitle() {
guard let url = URL(string: url) else { return }
URLSession.shared.dataTaskPublisher(for: url)
.map { data, _ in String(data: data, encoding: .utf8) }
.compactMap { $0 }
.replaceError(with: "")
.receive(on: DispatchQueue.global())
.map { html in
guard let range = html.range(of: "<title>.*?</title>", options: .regularExpression, range: nil, locale: nil) else { return "" }
return html[range].replacingOccurrences(of: "</?title>", with: "", options: .regularExpression, range: nil)
}
.receive(on: DispatchQueue.main)
.sink { [weak self] title in
guard let self else { return }
self.title = title
}
.store(in: &cancellables)
}
}
おわり
次はOGPも取得してみたいです