This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let webArchiveDataManager = WebArchiveDataManager() | |
// Save the web archive | |
webArchiveDataManager.saveWebArchive(from: webView, withName: "example") | |
// Load the web archive | |
webArchiveDataManager.loadWebArchive(named: "example", into: webView) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func loadWebArchive(named name: String, into webView: WKWebView) { | |
let fileURL = self.webArchiveDirectoryURL.appendingPathComponent("\(name).webarchive") | |
do { | |
let archiveData = try Data(contentsOf: fileURL) | |
webView.load(archiveData, mimeType: "application/x-webarchive", characterEncodingName: "", baseURL: fileURL) | |
print("Web archive loaded from: \(fileURL.path)") | |
} catch { | |
print("Error loading web archive: \(error.localizedDescription)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func saveWebArchive(from webView: WKWebView, withName name: String) { | |
webView.createWebArchiveData { result in | |
switch result { | |
case .success(let archiveData): | |
let fileURL = self.webArchiveDirectoryURL.appendingPathComponent("\(name).webarchive") | |
do { | |
try archiveData.write(to: fileURL) | |
print("Web archive saved at: \(fileURL.path)") | |
} catch { | |
print("Error saving web archive: \(error.localizedDescription)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WebArchiveDataManager { | |
let webArchiveDirectoryURL: URL | |
init() { | |
let fileManager = FileManager.default | |
guard let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { | |
fatalError("Unable to access document directory.") | |
} | |
webArchiveDirectoryURL = documentDirectory.appendingPathComponent("WebArchives") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.alert(item: $connectivityManager.notificationMessage) { message in | |
Alert(title: Text(message.text), | |
dismissButton: .default(Text("Dismiss"))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// WatchConnectivityManager.swift | |
// WeatherApp | |
// | |
// Created by Bahalek on 2022-01-04. | |
// | |
import Foundation | |
import WatchConnectivity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private let kMessageKey = "message" | |
func send(_ message: String) { | |
guard WCSession.default.activationState == .activated else { | |
return | |
} | |
#if os(iOS) | |
guard WCSession.default.isWatchAppInstalled else { | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import WatchConnectivity | |
final class WatchConnectivityManager: NSObject { | |
static let shared = WatchConnectivityManager() | |
private override init() { | |
super.init() | |
if WCSession.isSupported() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// WeatherApp | |
// | |
// Created by Bahalek on 2021-12-24. | |
// | |
import SwiftUI | |
struct ContentView: View { |
NewerOlder