Skip to content

Instantly share code, notes, and snippets.

@Bahallack
Bahallack / WebViewController.swift
Created June 19, 2023 15:47
using webArchiveDataManager
let webArchiveDataManager = WebArchiveDataManager()
// Save the web archive
webArchiveDataManager.saveWebArchive(from: webView, withName: "example")
// Load the web archive
webArchiveDataManager.loadWebArchive(named: "example", into: webView)
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)")
}
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)")
@Bahallack
Bahallack / WebArchiveDataManager.swift
Created June 19, 2023 15:28
WebArchiveDataManager init
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")
.alert(item: $connectivityManager.notificationMessage) { message in
Alert(title: Text(message.text),
dismissButton: .default(Text("Dismiss")))
}
//
// WatchConnectivityManager.swift
// WeatherApp
//
// Created by Bahalek on 2022-01-04.
//
import Foundation
import WatchConnectivity
Button("Hello World!", action: {
WatchConnectivityManager.shared.send("Hello World!\n\(Date().ISO8601Format())")
})
private let kMessageKey = "message"
func send(_ message: String) {
guard WCSession.default.activationState == .activated else {
return
}
#if os(iOS)
guard WCSession.default.isWatchAppInstalled else {
return
}
@Bahallack
Bahallack / WatchConnectivityManager.swift
Last active January 5, 2022 02:02
WatchConnectivityManager empty
import Foundation
import WatchConnectivity
final class WatchConnectivityManager: NSObject {
static let shared = WatchConnectivityManager()
private override init() {
super.init()
if WCSession.isSupported() {
//
// ContentView.swift
// WeatherApp
//
// Created by Bahalek on 2021-12-24.
//
import SwiftUI
struct ContentView: View {