0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】URLからタイトルを取得する

Last updated at Posted at 2023-02-13

はじめに

URLからWebサイトのタイトルを取得することがあり、実装したので記録しておきます。

サンプルアプリ

Simulator Screen Recording - iPhone 14 - 2023-02-13 at 22.21.21.gif

実装

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も取得してみたいです

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?