Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 846a790

Browse files
committed
New post 🚀
1 parent d2bbb5f commit 846a790

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

_drafts/2020-06-08-expose-uikit-to-swiftui.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ In this example we will create a simple app that will let the user select a docu
3535
- `makeUIView(context: Self.Context) -> Self.UIViewType`, a method where you create the UIKit to be used in SwiftUI
3636
- `updateUIView(_ uiView: Self.UIViewType, context: Self.Context)` a method called when the view must be redrawn due to external changes (e.g. a State update)
3737

38-
The `Self.UIViewType` type is an [associated type](https://www.hackingwithswift.com/articles/74/understanding-protocol-associated-types-and-their-constraints "swift protocol associated type") of the protocol and he must match the type of the UIKit view wrapped.
38+
The `Self.UIViewType` type is an [associated type](https://www.hackingwithswift.com/articles/74/understanding-protocol-associated-types-and-their-constraints "swift protocol associated type") of the protocol and he must match the type of the UIKit view wrapped.
3939
So for our example we can implement a `DocumentNameLabel` struct that implements the `UIViewRepresentable` protocol. In the `makeUIView` method we create an instance of the customized UILabel that we want to expose. In the `updateUIView` we will update the text shown by the label with the value contained in a `@Binding` var updated from the container view (do you remember [what is a @Binding var](https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-binding-property-wrapper "binding swiftui"), right?).
4040

4141
```swift
@@ -56,7 +56,15 @@ struct DocumentNameLabel: UIViewRepresentable {
5656
}
5757
```
5858

59-
After the view, we need to take care of the UIKit controller used to select the document, `UIDocumentPickerViewController`. We will wrap it in a struct that implements the `UIViewControllerRepresentable`. ...
59+
After the view, we need to take care of the UIKit controller used to select the document, `UIDocumentPickerViewController`. We will wrap it in a struct that implements the `UIViewControllerRepresentable`. This protocol contains four methods:
60+
61+
- `makeUIViewController(context: Self.Context) -> Self.UIViewControllerType`, a method used to create the instance of the view controller to be used in your SwiftUI screen
62+
- `updateUIViewController(Self.UIViewControllerType, context: Self.Context)`, a method called when the view controller must be redrawn/updated due to external changes (e.g. a State update)
63+
- `makeCoordinator() -> Self.Coordinator`, a method used to create the custom instance that you use to communicate changes from your view controller to other parts of your SwiftUI interface
64+
- `static func dismantleUIViewController(Self.UIViewControllerType, coordinator: Self.Coordinator)`, a method called to clean up additional resources used by the view controller when it is dismissed
65+
66+
As for the previous protocol, The `Self.UIViewControllerType` type is an [associated type](https://www.hackingwithswift.com/articles/74/understanding-protocol-associated-types-and-their-constraints "swift protocol associated type") of the protocol and he must match the type of the UIViewController wrapped.
67+
Le't start from the first method reported above, where we will create the instance of the `UIDocumentPickerViewController` that we want to expose to SwiftUI. The delegate of this controller will be the `Coordinator` instance created in the `makeCoodinator` protocol method. So the Coordinator will be responder for all the `UIDocumentPickerDelegate` methods. Remember that the `makeCoordinator` method is called before everything else when we will create our `DocumentPickerViewController` instance in SwiftUI. The `Coordinator` receive a reference to the wrapping struct. This reference is used to trigger a custom callback contained in the `var callback: (URL) -> ()` property that is received at `DocumentPickerViewController` construction time when the `documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])` method of the `UIDocumentPickerDelegate` protocol is invoked. In this way we are able to send back to SwiftUI the result of the interaction with the `UIDocumentPickerViewController`. Last but not least there's also the `updateUIViewController` method, but in this case with an empty implementation because we don't need to update the status of the view controller using some SwiftUI state change.
6068

6169
```swift
6270
struct DocumentPickerViewController: UIViewControllerRepresentable {
@@ -92,11 +100,13 @@ struct DocumentPickerViewController: UIViewControllerRepresentable {
92100
}
93101
```
94102

103+
Now we are ready to use our controller...
104+
95105
```swift
96106
struct ContentView: View {
97107
@State var isDocumentPickerPresented: Bool = false
98108
@State var documentUrl: String = ""
99-
109+
100110
var body: some View {
101111
VStack{
102112
Spacer()

0 commit comments

Comments
 (0)