-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentView.swift
69 lines (56 loc) · 2.18 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
//
// ContentView.swift
// SwiftUI-SharingScreen
//
// Created by Will Dale on 19/11/2020.
//
import SwiftUI
struct ContentView: View {
@State var isSharePresented : Bool = false
var body: some View {
Button(action: {
isSharePresented.toggle()
}, label: {
Text("Test Sharing")
})
.padding()
.sheet(isPresented: $isSharePresented, onDismiss: {
isSharePresented = false
}, content: {
ActivityViewController(itemsToShare: [createTestFile()!])
.edgesIgnoringSafeArea(.bottom)
})
}
func createTestFile() -> NSURL? {
let fileName = "Test Export"
let ext = ".txt"
let saveName = "\(fileName)\(ext)"
guard let path = try? FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false).appendingPathComponent(saveName) as NSURL else { return nil }
let textFile = "Hello World"
do {
try textFile.write(to: path as URL, atomically: true, encoding: String.Encoding.utf8)
} catch {
print("Failed to create file")
print("\(error)")
}
return path
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ActivityViewController: UIViewControllerRepresentable {
var itemsToShare : [Any]
var applicationActivities : [UIActivity]? = nil
func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityViewController>) -> UIActivityViewController {
let controller = UIActivityViewController(activityItems: itemsToShare, applicationActivities: applicationActivities)
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ActivityViewController>) {}
typealias UIViewControllerType = UIActivityViewController
}