-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentView.swift
117 lines (109 loc) · 3.43 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// ContentView.swift
// BundleSample
//
// Created by Alexander von Below on 06.09.22.
//
import SwiftUI
struct ContentView: View {
@State var image: UIImage?
@AppStorage("path") var defaultsPath: String = ""
var body: some View {
VStack {
if let display = image {
Image(uiImage: display)
.padding()
} else {
Image(systemName: "gear")
.padding()
}
Button("Test Resource Bundle") {
guard let url = bundlePath() else {
NSLog("Unable to find bundle")
return
}
testBundle(url: url)
}
.padding()
Button("Copy Bundle") {
guard let url = bundlePath() else {
NSLog ("Unable to find bundle")
return
}
guard let documentDirURL = self.bundleDocumentsPath() else {
NSLog ("Unable to find document dir")
return
}
do {
try FileManager.default.copyItem(
atPath: url.path(),
toPath: documentDirURL.path)
}
catch let error {
NSLog ("Unable to copy file \(error)")
}
}
.padding()
Button ("Test Documents Bundle") {
guard let documentDirURL = self.bundleDocumentsPath() else {
NSLog ("Unable to find document dir")
return
}
defaultsPath = documentDirURL.absoluteString
guard let newURL = URL(string: defaultsPath) else {
NSLog("Could not create path")
return
}
testBundle(url: newURL)
}
.padding()
Button ("Clear Image") {
self.image = nil
}
.padding()
}
.padding()
.onAppear() {
guard let newURL = URL(string: defaultsPath) else {
NSLog("No path in defaults")
return
}
testBundle(url: newURL)
}
}
private func bundlePath() -> URL? {
let bundleURL = Bundle.main.url(
forResource: "Sample",
withExtension: "quizlist")
return bundleURL
}
private func bundleDocumentsPath() -> URL? {
guard var documentDirURL = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask).first else {
return nil
}
documentDirURL.append(component: "Sample.quizlist")
return documentDirURL
}
private func testBundle(url: URL) {
guard let bundle = Bundle(url: url) else {
NSLog ("Unable to create Bundle at \(url)")
return
}
guard let imageURL = bundle.url(forResource: "Image", withExtension: "jpeg") else {
NSLog ("Unable to find image")
return
}
guard let bundleImage = UIImage(contentsOfFile: imageURL.path()) else {
NSLog("Unable to create image")
return
}
self.image = bundleImage
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}