├── .github └── workflows │ └── build.yml ├── .gitignore ├── Configuration └── SampleCode.xcconfig ├── LICENSE ├── OfflineDataAsyncExample.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcshareddata │ └── xcschemes │ └── OfflineDataAsyncExample.xcscheme ├── OfflineDataAsyncExample ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── ContentViewModel.swift ├── OfflineDataAsyncExample.entitlements ├── OfflineDataAsyncExampleApp.swift ├── Preview Content │ └── Preview Assets.xcassets │ │ └── Contents.json ├── URL+Identifiable.swift ├── Views │ ├── Alert.swift │ ├── ContentView.swift │ ├── ModalView.swift │ ├── PDFContentView.swift │ ├── SFSymbol.swift │ ├── Sheet.swift │ ├── SnapshotContentView.swift │ └── WebArchiveContentView.swift ├── WKWebView+WebArchive.swift └── WebDataManager.swift └── README.md /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | schedule: 5 | - cron: "0 2 1 * *" 6 | push: 7 | branches: [main] 8 | pull_request: 9 | branches: [main] 10 | 11 | jobs: 12 | build: 13 | runs-on: macOS-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: Build 17 | run: xcodebuild clean build CODE_SIGNING_ALLOWED=NO 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /Configuration/SampleCode.xcconfig: -------------------------------------------------------------------------------- 1 | // 2 | // See LICENSE for this sample’s licensing information. 3 | // 4 | // SampleCode.xcconfig 5 | // 6 | 7 | // The `SAMPLE_CODE_DISAMBIGUATOR` configuration is to make it easier to build 8 | // and run a sample code project. Once you set your project's development team, 9 | // you'll have a unique bundle identifier. This is because the bundle identifier 10 | // is derived based on the 'SAMPLE_CODE_DISAMBIGUATOR' value. Do not use this 11 | // approach in your own projects—it's only useful for sample code projects because 12 | // they are frequently downloaded and don't have a development team set. 13 | SAMPLE_CODE_DISAMBIGUATOR=${DEVELOPMENT_TEAM} 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Artem Novichkov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 06F3EBC52742CED8001CFE37 /* OfflineDataAsyncExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBC42742CED8001CFE37 /* OfflineDataAsyncExampleApp.swift */; }; 11 | 06F3EBC92742CED9001CFE37 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 06F3EBC82742CED9001CFE37 /* Assets.xcassets */; }; 12 | 06F3EBCC2742CED9001CFE37 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 06F3EBCB2742CED9001CFE37 /* Preview Assets.xcassets */; }; 13 | 06F3EBD42742CF0F001CFE37 /* SampleCode.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 06F3EBD32742CF0F001CFE37 /* SampleCode.xcconfig */; }; 14 | 06F3EBD62742CF61001CFE37 /* WebDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBD52742CF61001CFE37 /* WebDataManager.swift */; }; 15 | 06F3EBD82742CF6A001CFE37 /* URL+Identifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBD72742CF6A001CFE37 /* URL+Identifiable.swift */; }; 16 | 06F3EBDA2742CF71001CFE37 /* ContentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBD92742CF71001CFE37 /* ContentViewModel.swift */; }; 17 | 06F3EBE42742CF95001CFE37 /* SnapshotContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBDC2742CF95001CFE37 /* SnapshotContentView.swift */; }; 18 | 06F3EBE52742CF95001CFE37 /* Sheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBDD2742CF95001CFE37 /* Sheet.swift */; }; 19 | 06F3EBE62742CF95001CFE37 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBDE2742CF95001CFE37 /* Alert.swift */; }; 20 | 06F3EBE72742CF95001CFE37 /* PDFContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBDF2742CF95001CFE37 /* PDFContentView.swift */; }; 21 | 06F3EBE82742CF95001CFE37 /* ModalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBE02742CF95001CFE37 /* ModalView.swift */; }; 22 | 06F3EBE92742CF95001CFE37 /* WebArchiveContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBE12742CF95001CFE37 /* WebArchiveContentView.swift */; }; 23 | 06F3EBEA2742CF95001CFE37 /* SFSymbol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBE22742CF95001CFE37 /* SFSymbol.swift */; }; 24 | 06F3EBEB2742CF95001CFE37 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBE32742CF95001CFE37 /* ContentView.swift */; }; 25 | 06F3EBEE2742D9D5001CFE37 /* WKWebView+WebArchive.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F3EBED2742D9D5001CFE37 /* WKWebView+WebArchive.swift */; }; 26 | /* End PBXBuildFile section */ 27 | 28 | /* Begin PBXFileReference section */ 29 | 06F3EBC12742CED8001CFE37 /* OfflineDataAsyncExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OfflineDataAsyncExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | 06F3EBC42742CED8001CFE37 /* OfflineDataAsyncExampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OfflineDataAsyncExampleApp.swift; sourceTree = ""; }; 31 | 06F3EBC82742CED9001CFE37 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 32 | 06F3EBCB2742CED9001CFE37 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 33 | 06F3EBD32742CF0F001CFE37 /* SampleCode.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = SampleCode.xcconfig; sourceTree = ""; }; 34 | 06F3EBD52742CF61001CFE37 /* WebDataManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebDataManager.swift; sourceTree = ""; }; 35 | 06F3EBD72742CF6A001CFE37 /* URL+Identifiable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "URL+Identifiable.swift"; sourceTree = ""; }; 36 | 06F3EBD92742CF71001CFE37 /* ContentViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentViewModel.swift; sourceTree = ""; }; 37 | 06F3EBDC2742CF95001CFE37 /* SnapshotContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnapshotContentView.swift; sourceTree = ""; }; 38 | 06F3EBDD2742CF95001CFE37 /* Sheet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Sheet.swift; sourceTree = ""; }; 39 | 06F3EBDE2742CF95001CFE37 /* Alert.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = ""; }; 40 | 06F3EBDF2742CF95001CFE37 /* PDFContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PDFContentView.swift; sourceTree = ""; }; 41 | 06F3EBE02742CF95001CFE37 /* ModalView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModalView.swift; sourceTree = ""; }; 42 | 06F3EBE12742CF95001CFE37 /* WebArchiveContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebArchiveContentView.swift; sourceTree = ""; }; 43 | 06F3EBE22742CF95001CFE37 /* SFSymbol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SFSymbol.swift; sourceTree = ""; }; 44 | 06F3EBE32742CF95001CFE37 /* ContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 45 | 06F3EBEC2742D1C6001CFE37 /* OfflineDataAsyncExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = OfflineDataAsyncExample.entitlements; sourceTree = ""; }; 46 | 06F3EBED2742D9D5001CFE37 /* WKWebView+WebArchive.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WKWebView+WebArchive.swift"; sourceTree = ""; }; 47 | /* End PBXFileReference section */ 48 | 49 | /* Begin PBXFrameworksBuildPhase section */ 50 | 06F3EBBE2742CED8001CFE37 /* Frameworks */ = { 51 | isa = PBXFrameworksBuildPhase; 52 | buildActionMask = 2147483647; 53 | files = ( 54 | ); 55 | runOnlyForDeploymentPostprocessing = 0; 56 | }; 57 | /* End PBXFrameworksBuildPhase section */ 58 | 59 | /* Begin PBXGroup section */ 60 | 06F3EBB82742CED8001CFE37 = { 61 | isa = PBXGroup; 62 | children = ( 63 | 06F3EBD22742CF0F001CFE37 /* Configuration */, 64 | 06F3EBC32742CED8001CFE37 /* OfflineDataAsyncExample */, 65 | 06F3EBC22742CED8001CFE37 /* Products */, 66 | ); 67 | sourceTree = ""; 68 | }; 69 | 06F3EBC22742CED8001CFE37 /* Products */ = { 70 | isa = PBXGroup; 71 | children = ( 72 | 06F3EBC12742CED8001CFE37 /* OfflineDataAsyncExample.app */, 73 | ); 74 | name = Products; 75 | sourceTree = ""; 76 | }; 77 | 06F3EBC32742CED8001CFE37 /* OfflineDataAsyncExample */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 06F3EBEC2742D1C6001CFE37 /* OfflineDataAsyncExample.entitlements */, 81 | 06F3EBDB2742CF95001CFE37 /* Views */, 82 | 06F3EBC42742CED8001CFE37 /* OfflineDataAsyncExampleApp.swift */, 83 | 06F3EBD52742CF61001CFE37 /* WebDataManager.swift */, 84 | 06F3EBD72742CF6A001CFE37 /* URL+Identifiable.swift */, 85 | 06F3EBD92742CF71001CFE37 /* ContentViewModel.swift */, 86 | 06F3EBED2742D9D5001CFE37 /* WKWebView+WebArchive.swift */, 87 | 06F3EBC82742CED9001CFE37 /* Assets.xcassets */, 88 | 06F3EBCA2742CED9001CFE37 /* Preview Content */, 89 | ); 90 | path = OfflineDataAsyncExample; 91 | sourceTree = ""; 92 | }; 93 | 06F3EBCA2742CED9001CFE37 /* Preview Content */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 06F3EBCB2742CED9001CFE37 /* Preview Assets.xcassets */, 97 | ); 98 | path = "Preview Content"; 99 | sourceTree = ""; 100 | }; 101 | 06F3EBD22742CF0F001CFE37 /* Configuration */ = { 102 | isa = PBXGroup; 103 | children = ( 104 | 06F3EBD32742CF0F001CFE37 /* SampleCode.xcconfig */, 105 | ); 106 | path = Configuration; 107 | sourceTree = ""; 108 | }; 109 | 06F3EBDB2742CF95001CFE37 /* Views */ = { 110 | isa = PBXGroup; 111 | children = ( 112 | 06F3EBDC2742CF95001CFE37 /* SnapshotContentView.swift */, 113 | 06F3EBDD2742CF95001CFE37 /* Sheet.swift */, 114 | 06F3EBDE2742CF95001CFE37 /* Alert.swift */, 115 | 06F3EBDF2742CF95001CFE37 /* PDFContentView.swift */, 116 | 06F3EBE02742CF95001CFE37 /* ModalView.swift */, 117 | 06F3EBE12742CF95001CFE37 /* WebArchiveContentView.swift */, 118 | 06F3EBE22742CF95001CFE37 /* SFSymbol.swift */, 119 | 06F3EBE32742CF95001CFE37 /* ContentView.swift */, 120 | ); 121 | path = Views; 122 | sourceTree = ""; 123 | }; 124 | /* End PBXGroup section */ 125 | 126 | /* Begin PBXNativeTarget section */ 127 | 06F3EBC02742CED8001CFE37 /* OfflineDataAsyncExample */ = { 128 | isa = PBXNativeTarget; 129 | buildConfigurationList = 06F3EBCF2742CED9001CFE37 /* Build configuration list for PBXNativeTarget "OfflineDataAsyncExample" */; 130 | buildPhases = ( 131 | 06F3EBBD2742CED8001CFE37 /* Sources */, 132 | 06F3EBBE2742CED8001CFE37 /* Frameworks */, 133 | 06F3EBBF2742CED8001CFE37 /* Resources */, 134 | ); 135 | buildRules = ( 136 | ); 137 | dependencies = ( 138 | ); 139 | name = OfflineDataAsyncExample; 140 | productName = OfflineDataAsyncExample; 141 | productReference = 06F3EBC12742CED8001CFE37 /* OfflineDataAsyncExample.app */; 142 | productType = "com.apple.product-type.application"; 143 | }; 144 | /* End PBXNativeTarget section */ 145 | 146 | /* Begin PBXProject section */ 147 | 06F3EBB92742CED8001CFE37 /* Project object */ = { 148 | isa = PBXProject; 149 | attributes = { 150 | BuildIndependentTargetsInParallel = 1; 151 | LastSwiftUpdateCheck = 1320; 152 | LastUpgradeCheck = 1320; 153 | TargetAttributes = { 154 | 06F3EBC02742CED8001CFE37 = { 155 | CreatedOnToolsVersion = 13.2; 156 | }; 157 | }; 158 | }; 159 | buildConfigurationList = 06F3EBBC2742CED8001CFE37 /* Build configuration list for PBXProject "OfflineDataAsyncExample" */; 160 | compatibilityVersion = "Xcode 13.0"; 161 | developmentRegion = en; 162 | hasScannedForEncodings = 0; 163 | knownRegions = ( 164 | en, 165 | Base, 166 | ); 167 | mainGroup = 06F3EBB82742CED8001CFE37; 168 | productRefGroup = 06F3EBC22742CED8001CFE37 /* Products */; 169 | projectDirPath = ""; 170 | projectRoot = ""; 171 | targets = ( 172 | 06F3EBC02742CED8001CFE37 /* OfflineDataAsyncExample */, 173 | ); 174 | }; 175 | /* End PBXProject section */ 176 | 177 | /* Begin PBXResourcesBuildPhase section */ 178 | 06F3EBBF2742CED8001CFE37 /* Resources */ = { 179 | isa = PBXResourcesBuildPhase; 180 | buildActionMask = 2147483647; 181 | files = ( 182 | 06F3EBCC2742CED9001CFE37 /* Preview Assets.xcassets in Resources */, 183 | 06F3EBD42742CF0F001CFE37 /* SampleCode.xcconfig in Resources */, 184 | 06F3EBC92742CED9001CFE37 /* Assets.xcassets in Resources */, 185 | ); 186 | runOnlyForDeploymentPostprocessing = 0; 187 | }; 188 | /* End PBXResourcesBuildPhase section */ 189 | 190 | /* Begin PBXSourcesBuildPhase section */ 191 | 06F3EBBD2742CED8001CFE37 /* Sources */ = { 192 | isa = PBXSourcesBuildPhase; 193 | buildActionMask = 2147483647; 194 | files = ( 195 | 06F3EBD62742CF61001CFE37 /* WebDataManager.swift in Sources */, 196 | 06F3EBE42742CF95001CFE37 /* SnapshotContentView.swift in Sources */, 197 | 06F3EBEB2742CF95001CFE37 /* ContentView.swift in Sources */, 198 | 06F3EBE92742CF95001CFE37 /* WebArchiveContentView.swift in Sources */, 199 | 06F3EBC52742CED8001CFE37 /* OfflineDataAsyncExampleApp.swift in Sources */, 200 | 06F3EBE52742CF95001CFE37 /* Sheet.swift in Sources */, 201 | 06F3EBD82742CF6A001CFE37 /* URL+Identifiable.swift in Sources */, 202 | 06F3EBDA2742CF71001CFE37 /* ContentViewModel.swift in Sources */, 203 | 06F3EBE82742CF95001CFE37 /* ModalView.swift in Sources */, 204 | 06F3EBEE2742D9D5001CFE37 /* WKWebView+WebArchive.swift in Sources */, 205 | 06F3EBE62742CF95001CFE37 /* Alert.swift in Sources */, 206 | 06F3EBE72742CF95001CFE37 /* PDFContentView.swift in Sources */, 207 | 06F3EBEA2742CF95001CFE37 /* SFSymbol.swift in Sources */, 208 | ); 209 | runOnlyForDeploymentPostprocessing = 0; 210 | }; 211 | /* End PBXSourcesBuildPhase section */ 212 | 213 | /* Begin XCBuildConfiguration section */ 214 | 06F3EBCD2742CED9001CFE37 /* Debug */ = { 215 | isa = XCBuildConfiguration; 216 | buildSettings = { 217 | ALWAYS_SEARCH_USER_PATHS = NO; 218 | CLANG_ANALYZER_NONNULL = YES; 219 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 220 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 221 | CLANG_CXX_LIBRARY = "libc++"; 222 | CLANG_ENABLE_MODULES = YES; 223 | CLANG_ENABLE_OBJC_ARC = YES; 224 | CLANG_ENABLE_OBJC_WEAK = YES; 225 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 226 | CLANG_WARN_BOOL_CONVERSION = YES; 227 | CLANG_WARN_COMMA = YES; 228 | CLANG_WARN_CONSTANT_CONVERSION = YES; 229 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 230 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 231 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 232 | CLANG_WARN_EMPTY_BODY = YES; 233 | CLANG_WARN_ENUM_CONVERSION = YES; 234 | CLANG_WARN_INFINITE_RECURSION = YES; 235 | CLANG_WARN_INT_CONVERSION = YES; 236 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 237 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 238 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 239 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 240 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 241 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 242 | CLANG_WARN_STRICT_PROTOTYPES = YES; 243 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 244 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 245 | CLANG_WARN_UNREACHABLE_CODE = YES; 246 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 247 | COPY_PHASE_STRIP = NO; 248 | DEBUG_INFORMATION_FORMAT = dwarf; 249 | ENABLE_STRICT_OBJC_MSGSEND = YES; 250 | ENABLE_TESTABILITY = YES; 251 | GCC_C_LANGUAGE_STANDARD = gnu11; 252 | GCC_DYNAMIC_NO_PIC = NO; 253 | GCC_NO_COMMON_BLOCKS = YES; 254 | GCC_OPTIMIZATION_LEVEL = 0; 255 | GCC_PREPROCESSOR_DEFINITIONS = ( 256 | "DEBUG=1", 257 | "$(inherited)", 258 | ); 259 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 260 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 261 | GCC_WARN_UNDECLARED_SELECTOR = YES; 262 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 263 | GCC_WARN_UNUSED_FUNCTION = YES; 264 | GCC_WARN_UNUSED_VARIABLE = YES; 265 | IPHONEOS_DEPLOYMENT_TARGET = 15.2; 266 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 267 | MTL_FAST_MATH = YES; 268 | ONLY_ACTIVE_ARCH = YES; 269 | SDKROOT = iphoneos; 270 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 271 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 272 | }; 273 | name = Debug; 274 | }; 275 | 06F3EBD02742CED9001CFE37 /* Debug */ = { 276 | isa = XCBuildConfiguration; 277 | baseConfigurationReference = 06F3EBD32742CF0F001CFE37 /* SampleCode.xcconfig */; 278 | buildSettings = { 279 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 280 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 281 | CODE_SIGN_ENTITLEMENTS = OfflineDataAsyncExample/OfflineDataAsyncExample.entitlements; 282 | CODE_SIGN_STYLE = Automatic; 283 | CURRENT_PROJECT_VERSION = 1; 284 | DEVELOPMENT_ASSET_PATHS = "\"OfflineDataAsyncExample/Preview Content\""; 285 | ENABLE_PREVIEWS = YES; 286 | GENERATE_INFOPLIST_FILE = YES; 287 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 288 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 289 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 290 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 291 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 292 | LD_RUNPATH_SEARCH_PATHS = ( 293 | "$(inherited)", 294 | "@executable_path/Frameworks", 295 | ); 296 | MARKETING_VERSION = 1.0; 297 | PRODUCT_BUNDLE_IDENTIFIER = com.artemnovichkov.OfflineDataAsyncExample; 298 | PRODUCT_NAME = "$(TARGET_NAME)"; 299 | SUPPORTS_MACCATALYST = YES; 300 | SWIFT_EMIT_LOC_STRINGS = YES; 301 | SWIFT_VERSION = 5.0; 302 | TARGETED_DEVICE_FAMILY = "1,2"; 303 | }; 304 | name = Debug; 305 | }; 306 | /* End XCBuildConfiguration section */ 307 | 308 | /* Begin XCConfigurationList section */ 309 | 06F3EBBC2742CED8001CFE37 /* Build configuration list for PBXProject "OfflineDataAsyncExample" */ = { 310 | isa = XCConfigurationList; 311 | buildConfigurations = ( 312 | 06F3EBCD2742CED9001CFE37 /* Debug */, 313 | ); 314 | defaultConfigurationIsVisible = 0; 315 | defaultConfigurationName = Debug; 316 | }; 317 | 06F3EBCF2742CED9001CFE37 /* Build configuration list for PBXNativeTarget "OfflineDataAsyncExample" */ = { 318 | isa = XCConfigurationList; 319 | buildConfigurations = ( 320 | 06F3EBD02742CED9001CFE37 /* Debug */, 321 | ); 322 | defaultConfigurationIsVisible = 0; 323 | defaultConfigurationName = Debug; 324 | }; 325 | /* End XCConfigurationList section */ 326 | }; 327 | rootObject = 06F3EBB92742CED8001CFE37 /* Project object */; 328 | } 329 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample.xcodeproj/xcshareddata/xcschemes/OfflineDataAsyncExample.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 44 | 46 | 52 | 53 | 54 | 55 | 61 | 63 | 69 | 70 | 71 | 72 | 74 | 75 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/ContentViewModel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import SwiftUI 6 | 7 | @MainActor 8 | final class ContentViewModel { 9 | 10 | //MARK: - Button Data 11 | 12 | struct ButtonData: Identifiable { 13 | let title: String 14 | let url: URL 15 | let sheet: Sheet 16 | 17 | var id: String { 18 | url.absoluteString 19 | } 20 | 21 | init(title: String, sheet: Sheet) { 22 | self.title = title 23 | self.sheet = sheet 24 | switch sheet { 25 | case .png(let url): 26 | self.url = url 27 | case .pdf(let url): 28 | self.url = url 29 | case .webarchive(let url): 30 | self.url = url 31 | } 32 | } 33 | } 34 | 35 | var buttonsData: [ButtonData] { 36 | [.init(title: "Open Snapshot", sheet: .png(pngURL)), 37 | .init(title: "Open PDF", sheet: .pdf(pdfURL)), 38 | .init(title: "Open WebArchive", sheet: .webarchive(webarchiveURL))] 39 | } 40 | 41 | //MARK: - Managers 42 | 43 | private lazy var webDataManager: WebDataManager = .init() 44 | private lazy var fileManager: FileManager = .default 45 | 46 | //MARK: - URLs 47 | 48 | private var documentsURL: URL { 49 | fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] 50 | } 51 | 52 | private var webarchiveURL: URL { 53 | documentsURL 54 | .appendingPathComponent("data") 55 | .appendingPathExtension("webarchive") 56 | } 57 | 58 | private var pdfURL: URL { 59 | documentsURL 60 | .appendingPathComponent("data") 61 | .appendingPathExtension("pdf") 62 | } 63 | 64 | private var pngURL: URL { 65 | documentsURL 66 | .appendingPathComponent("data") 67 | .appendingPathExtension("png") 68 | } 69 | 70 | //MARK: - Lifecycle 71 | 72 | func createData(url: URL, type: WebDataManager.DataType) async throws { 73 | let data = try await webDataManager.createData(url: url, type: type) 74 | self.fileManager.createFile(atPath: self.path(for: type), 75 | contents: data, 76 | attributes: nil) 77 | } 78 | 79 | func fileExists(for data: ButtonData) -> Bool { 80 | fileManager.fileExists(atPath: data.url.path) 81 | } 82 | 83 | //MARK: - Private 84 | 85 | private func path(for type: WebDataManager.DataType) -> String { 86 | switch type { 87 | case .webArchive: 88 | return webarchiveURL.path 89 | case .pdf: 90 | return pdfURL.path 91 | case .snapshot: 92 | return pngURL.path 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/OfflineDataAsyncExample.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.network.client 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/OfflineDataAsyncExampleApp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import SwiftUI 6 | 7 | @main 8 | struct OfflineDataAsyncExampleApp: App { 9 | var body: some Scene { 10 | UITextField.appearance().clearButtonMode = .whileEditing 11 | return WindowGroup { 12 | ContentView() 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/URL+Identifiable.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import SwiftUI 6 | 7 | extension URL: @retroactive Identifiable { 8 | 9 | public var id: String { 10 | absoluteString 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Views/Alert.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import SwiftUI 6 | 7 | enum Alert: Identifiable { 8 | 9 | case success(String) 10 | case wrongURL 11 | case error(Error) 12 | 13 | var id: String { 14 | switch self { 15 | case .success: 16 | return "success" 17 | case .wrongURL: 18 | return "wrongURL" 19 | case .error: 20 | return "error" 21 | } 22 | } 23 | 24 | var alert: SwiftUI.Alert { 25 | .init(title: Text(title), 26 | message: Text(message), 27 | dismissButton: .default(Text("OK"))) 28 | } 29 | 30 | var title: String { 31 | switch self { 32 | case .success: 33 | return "Success" 34 | case .wrongURL, .error: 35 | return "Error" 36 | } 37 | } 38 | 39 | var message: String { 40 | switch self { 41 | case .success(let message): 42 | return message 43 | case .wrongURL: 44 | return "Fail to get URL" 45 | case .error(let error): 46 | return error.localizedDescription 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Views/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | import SwiftUI 5 | 6 | struct ContentView: View { 7 | 8 | //MARK: - States 9 | 10 | @State private var text: String = "https://www.artemnovichkov.com" 11 | @State private var alert: Alert? 12 | @State private var actionSheetIsPresented = false 13 | @State private var sheet: Sheet? 14 | @State private var isLoading = false 15 | 16 | //MARK: - ViewModel 17 | 18 | private let viewModel: ContentViewModel = .init() 19 | 20 | //MARK: - Body 21 | 22 | var body: some View { 23 | ZStack { 24 | ProgressView("Loading...") 25 | .opacity(isLoading ? 1 : 0) 26 | VStack(spacing: 16) { 27 | TextField("", text: $text) 28 | .textFieldStyle(RoundedBorderTextFieldStyle()) 29 | HStack(spacing: 32, content: buttons) 30 | } 31 | .padding() 32 | .opacity(isLoading ? 0 : 1) 33 | .alert(item: $alert) { $0.alert } 34 | .actionSheet(isPresented: $actionSheetIsPresented, content: actionSheet) 35 | .sheet(item: $sheet) { $0 } 36 | } 37 | } 38 | 39 | @ViewBuilder 40 | private func buttons() -> some View { 41 | Button("Save") { 42 | actionSheetIsPresented = true 43 | } 44 | .disabled(URL(string: text) == nil) 45 | ForEach(viewModel.buttonsData) { data in 46 | Button(action: { 47 | sheet = data.sheet 48 | }, label: { 49 | Text(data.title) 50 | .multilineTextAlignment(.center) 51 | }) 52 | .disabled(viewModel.fileExists(for: data) == false) 53 | } 54 | } 55 | 56 | private func actionSheet() -> ActionSheet { 57 | var buttons: [ActionSheet.Button] = WebDataManager.DataType.allCases.map { type in 58 | .default(Text(type.rawValue)) { 59 | save(with: type) 60 | } 61 | } 62 | buttons.append(.cancel()) 63 | return ActionSheet(title: Text("Select a type"), message: nil, buttons: buttons) 64 | } 65 | 66 | //MARK: - Private 67 | 68 | private func save(with type: WebDataManager.DataType) { 69 | guard let url = URL(string: text) else { 70 | alert = .wrongURL 71 | return 72 | } 73 | Task { 74 | do { 75 | isLoading = true 76 | try await viewModel.createData(url: url, type: type) 77 | isLoading = false 78 | alert = .success("\(type.rawValue) is saved successfully") 79 | } 80 | catch { 81 | alert = .error(error) 82 | } 83 | } 84 | } 85 | } 86 | 87 | struct ContentView_Previews: PreviewProvider { 88 | static var previews: some View { 89 | ContentView() 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Views/ModalView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import SwiftUI 6 | 7 | struct ModalView: View where Content: View { 8 | 9 | @Environment(\.presentationMode) private var presentationMode 10 | 11 | private let content: Content 12 | 13 | init(@ViewBuilder content: () -> Content) { 14 | self.content = content() 15 | } 16 | 17 | var body: some View { 18 | VStack { 19 | #if targetEnvironment(macCatalyst) 20 | HStack() { 21 | Spacer() 22 | Button(action: close) { 23 | SFSymbol.xmark 24 | } 25 | } 26 | .padding() 27 | #endif 28 | content 29 | } 30 | } 31 | 32 | //MARK: - Private 33 | 34 | private func close() { 35 | presentationMode.wrappedValue.dismiss() 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Views/PDFContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import SwiftUI 6 | import PDFKit 7 | 8 | struct PDFContentView: UIViewRepresentable { 9 | 10 | let url: URL 11 | 12 | func makeUIView(context: Context) -> PDFView { 13 | let view = PDFView() 14 | view.autoScales = true 15 | view.document = PDFDocument(url: url) 16 | return view 17 | } 18 | 19 | func updateUIView(_ pdfView: PDFView, context: Context) { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Views/SFSymbol.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | import SwiftUI 5 | 6 | enum SFSymbol: String, CaseIterable, View, Identifiable { 7 | 8 | case xmark = "xmark" 9 | 10 | var id: String { 11 | rawValue 12 | } 13 | 14 | var body: Image { 15 | Image(systemName: rawValue) 16 | } 17 | } 18 | 19 | struct SFSymbol_Previews: PreviewProvider { 20 | static var previews: some View { 21 | VStack(spacing: 10) { 22 | ForEach(SFSymbol.allCases) { symbol in 23 | symbol 24 | } 25 | } 26 | .padding() 27 | .previewLayout(.sizeThatFits) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Views/Sheet.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | import SwiftUI 5 | 6 | enum Sheet: View, Identifiable { 7 | 8 | case webarchive(URL) 9 | case pdf(URL) 10 | case png(URL) 11 | 12 | var id: String { 13 | switch self { 14 | case .webarchive: 15 | return "webarchive" 16 | case .pdf: 17 | return "pdf" 18 | case .png: 19 | return "png" 20 | } 21 | } 22 | 23 | var body: some View { 24 | ModalView { 25 | switch self { 26 | case .webarchive(let url): 27 | WebArchiveContentView(url: url) 28 | case .pdf(let url): 29 | PDFContentView(url: url) 30 | case .png(let url): 31 | SnapshotContentView(url: url) 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Views/SnapshotContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | import SwiftUI 5 | 6 | struct SnapshotContentView: View { 7 | 8 | let url: URL 9 | 10 | var body: some View { 11 | if let image = UIImage(contentsOfFile: url.path) { 12 | ScrollView { 13 | Image(uiImage: image) 14 | .resizable() 15 | .scaledToFit() 16 | } 17 | } 18 | else { 19 | Text("Fail to load image") 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/Views/WebArchiveContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import SwiftUI 6 | import WebKit 7 | 8 | struct WebArchiveContentView: UIViewRepresentable { 9 | 10 | let url: URL 11 | 12 | func makeUIView(context: Context) -> WKWebView { 13 | WKWebView() 14 | } 15 | 16 | func updateUIView(_ webView: WKWebView, context: Context) { 17 | webView.loadFileURL(url, allowingReadAccessTo: url) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/WKWebView+WebArchive.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import WebKit 6 | 7 | extension WKWebView { 8 | 9 | func webArchiveData() async throws -> Data { 10 | try await withCheckedThrowingContinuation { continuation in 11 | createWebArchiveData { result in 12 | continuation.resume(with: result) 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /OfflineDataAsyncExample/WebDataManager.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2021 Artem Novichkov. All rights reserved. 3 | // 4 | 5 | import WebKit 6 | 7 | @MainActor 8 | final class WebDataManager: NSObject { 9 | 10 | enum DataError: Error { 11 | case noImageData 12 | } 13 | 14 | enum DataType: String, CaseIterable { 15 | case snapshot = "Snapshot" 16 | case pdf = "PDF" 17 | case webArchive = "Web Archive" 18 | } 19 | 20 | private var type: DataType = .webArchive 21 | private var continuation: CheckedContinuation? 22 | 23 | private lazy var webView: WKWebView = { 24 | let webView = WKWebView() 25 | webView.navigationDelegate = self 26 | return webView 27 | }() 28 | 29 | func createData(url: URL, type: DataType) async throws -> Data { 30 | try await load(url) 31 | switch type { 32 | case .snapshot: 33 | let config = WKSnapshotConfiguration() 34 | config.rect = .init(origin: .zero, size: webView.scrollView.contentSize) 35 | let image = try await webView.takeSnapshot(configuration: config) 36 | guard let pngData = image.pngData() else { 37 | throw DataError.noImageData 38 | } 39 | return pngData 40 | case .pdf: 41 | let config = WKPDFConfiguration() 42 | config.rect = .init(origin: .zero, size: webView.scrollView.contentSize) 43 | return try await webView.pdf(configuration: config) 44 | case .webArchive: 45 | return try await webView.webArchiveData() 46 | } 47 | } 48 | 49 | //MARK: - Private 50 | 51 | private func load(_ url: URL) async throws { 52 | return try await withCheckedThrowingContinuation { continuation in 53 | self.continuation = continuation 54 | self.webView.load(.init(url: url)) 55 | } 56 | } 57 | } 58 | 59 | extension WebDataManager: WKNavigationDelegate { 60 | 61 | func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 62 | continuation?.resume(returning: ()) 63 | } 64 | 65 | func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { 66 | continuation?.resume(throwing: error) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Offline Data Async Example 3 | 4 | An example project for [Async/await for existing iOS apps](https://www.artemnovichkov.com/blog/async-await-offline) article. 5 | 6 | [![Build](https://github.com/artemnovichkov/OfflineDataAsyncExample/actions/workflows/build.yml/badge.svg)](https://github.com/artemnovichkov/OfflineDataAsyncExample/actions/workflows/build.yml) 7 | 8 | ## Author 9 | 10 | Artem Novichkov, https://www.artemnovichkov.com/about 11 | 12 | ## License 13 | 14 | The project is available under the MIT license. See the [LICENSE](./LICENSE) file for more info. 15 | --------------------------------------------------------------------------------