<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Swift Productions - Medium]]></title>
        <description><![CDATA[Introducing Swift Productions: weekly articles and tutorials to learn how to build an App using Swift, SwiftUI &amp; Firebase. - Medium]]></description>
        <link>https://medium.com/swift-productions?source=rss----acb6f01f2100---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Swift Productions - Medium</title>
            <link>https://medium.com/swift-productions?source=rss----acb6f01f2100---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:48:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/swift-productions" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Grid View / SwiftUI]]></title>
            <link>https://medium.com/swift-productions/grid-view-swiftui-d58a5516aaa2?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/d58a5516aaa2</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Wed, 08 May 2024 12:30:40 GMT</pubDate>
            <atom:updated>2024-05-08T12:30:39.997Z</atom:updated>
            <content:encoded><![CDATA[<p>Short tutorial on how to create a structured grid View with SwiftUI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DihSaXzTK7hVxLi-onkqjA.png" /></figure><p>Difficulty: <strong>Beginner</strong> | Easy | Normal | Challenging</p><p>Environment : Xcode 14.3, iOS 16 &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><blockquote><em>Full code at the end of the article</em></blockquote><h3>Create new Xcode Project</h3><p>Open Xcode &gt; App template &gt; Select the SwiftUI Life Cycle and call it <strong>GridApp</strong></p><h3>Create a Model</h3><p>We will define a model struct to represent the data we want to display in our grid. For this tutorial, we will create a simple Item struct.</p><p>Go ahead and create a new swift file, call it <strong><em>GridView</em></strong>. Then, copy the code below right over the struct GridView</p><pre>struct Item: Identifiable {<br> let id = UUID()<br> let name: String<br>}</pre><h4>Create a Grid View Layout</h4><p>In SwiftUI, you can create a grid layout using LazyVGrid or LazyHGrid. For this tutorial, let’s use LazyVGrid to create a vertical grid.</p><pre>struct GridView: View {<br> let items: [Item]<br> <br> var body: some View {<br> ScrollView {<br> LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {<br> ForEach(items) { item in<br> Text(item.name)<br> .frame(maxWidth: .infinity, maxHeight: .infinity)<br> .background(Color.blue)<br> .cornerRadius(8)<br> .padding(10)<br> }<br> }<br> .padding()<br> }<br> .navigationTitle(“Grid View”)<br> }<br>}</pre><h4>Preview the Grid View</h4><p>Now, go back in your ContentView.swift, create some sample data and preview the grid view passing the <em>items</em> parameters:</p><pre>struct ContentView: View {<br> let items = [<br> Item(name: “Item 1”),<br> Item(name: “Item 2”),<br> Item(name: “Item 3”),<br> Item(name: “Item 4”),<br> Item(name: “Item 5”),<br> Item(name: “Item 6”),<br> Item(name: “Item 7”),<br> Item(name: “Item 8”),<br> Item(name: “Item 9”)<br> ]<br> <br> var body: some View {<br> NavigationView {<br> GridView(items: items)<br> }<br> }<br>}<br></pre><h4>Adjust Grid Layout</h4><p>You can adjust the number of columns in the grid by changing the columns parameter of LazyVGrid. Additionally, you can customise the appearance like the color, padding and more of the grid items.</p><h4>Run your app</h4><p>Build and run your SwiftUI app in the simulator or on a device to see your grid view in action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJmQ3JxCR6Rj-CeHqdbj7g.png" /></figure><p><strong><em>Thanks for reading! I’m always happy to have a chat and collaborate at </em></strong><a href="/service/mailto:hello@sullivandecarli.com"><strong><em>hello@sullivandecarli.com</em></strong></a><strong><em>. Consider subscribing to enjoy unlimited access to my articles and all of Medium </em></strong><a href="https://sullivan-decarli.medium.com/membership"><strong><em>through my referral link</em></strong></a><strong><em>.</em></strong></p><h4>Full code below:</h4><pre><br>// GridView <br>import SwiftUI<br><br>struct Item: Identifiable {<br>    let id = UUID()<br>    let name: String<br>}<br><br>struct GridView: View {<br>    let items: [Item]<br>    <br>    var body: some View {<br>        ScrollView {<br>            LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {<br>                ForEach(items) { item in<br>                    Text(item.name)<br>                        .frame(maxWidth: .infinity, maxHeight: .infinity)<br>                        .background(Color.red)<br>                        .cornerRadius(6)<br>                        .padding(10)<br>                }<br>            }<br>            .padding()<br>        }<br>        .navigationTitle(&quot;Grid View&quot;)<br>    }<br>}</pre><pre>// ContentView <br>import SwiftUI<br><br>struct ContentView: View {<br>    let items = [<br>        Item(name: &quot;Item 1&quot;),<br>        Item(name: &quot;Item 2&quot;),<br>        Item(name: &quot;Item 3&quot;),<br>        Item(name: &quot;Item 4&quot;),<br>        Item(name: &quot;Item 5&quot;),<br>        Item(name: &quot;Item 6&quot;),<br>        Item(name: &quot;Item 7&quot;),<br>        Item(name: &quot;Item 8&quot;),<br>        Item(name: &quot;Item 9&quot;),<br>        Item(name: &quot;Item 10&quot;),<br>        Item(name: &quot;Item 11&quot;),<br>        Item(name: &quot;Item 12&quot;)<br>    ]<br>    <br>    var body: some View {<br>        NavigationView {<br>            GridView(items: items)<br>        }<br>    }<br>}<br><br>struct ContentView_Previews: PreviewProvider {<br>    static var previews: some View {<br>        ContentView()<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d58a5516aaa2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/grid-view-swiftui-d58a5516aaa2">Grid View / SwiftUI</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PhotosPicker with Firebase Storage / SwiftUI]]></title>
            <link>https://medium.com/swift-productions/photospicker-swiftui-firebase-storage-photo-library-3d20aadfacd2?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/3d20aadfacd2</guid>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Tue, 17 Jan 2023 18:12:37 GMT</pubDate>
            <atom:updated>2023-09-01T13:27:07.001Z</atom:updated>
            <content:encoded><![CDATA[<p>Learn how to pick up a picture from your phone library with SwiftUI PhotosPicker and upload the data to Firebase Storage.</p><figure><img alt="Screenshot of iPhones with the SwiftUI and Firebase logo" src="https://cdn-images-1.medium.com/max/1024/1*D7r76MRhwyvp35esrWF_Aw.png" /></figure><p>Difficulty: Beginner | Easy | <strong>Normal</strong> | Challenging</p><p>Environment : Xcode 14, iOS 16 &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><p><em>Full code at the end of the article</em></p><h3>Create new Xcode Project</h3><p>Open Xcode &gt; App template &gt; SwiftUI Life Cycle and call it <strong>PictureStorage</strong></p><h3>Firebase Set Up</h3><p>To integrate Firebase to your project, I invite you to check out this article and get back if you don’t know how to configure a project with Firebase:</p><p><a href="https://medium.com/swift-productions/how-to-add-firebase-with-spm-swiftui-d083189d510d">How to add Firebase to a SwiftUI Project with SPM</a></p><p>During the process, make sure to add the following dependency:</p><ul><li>FirebaseStorage</li></ul><figure><img alt="Screenshot of Xcode with Swift package manager" src="https://cdn-images-1.medium.com/max/1024/1*cyMZZ5ioIRGBLmF9nkx3yw.png" /><figcaption>Adding FirebaseStorage with Swift Package Manager</figcaption></figure><h3>Set Up Firebase Storage</h3><p>Unlike Firestore or Realtime Database, the Firebase Storage framework is built to store larges assets. It will be our server to store documents such as videos, images, audio files etc…</p><p>Let’s set it up in our console, go to Firebase console &gt; Storage panel. Then, click on get started and select “<em>start in test mode</em>”, then select whatever storage location.</p><figure><img alt="The setup of the Firebase console" src="https://cdn-images-1.medium.com/max/1024/1*ltmO_4-HOOqRPsYfgPVBIw.png" /><figcaption>Setting up Firebase Storage</figcaption></figure><h4>Let’s code</h4><p>Great! Now, we are ready to upload picture from our front-end to the Firebase backend. First let’s design the user interface to post pictures, import both frameworks at the top of the <em>ContentView.swift </em>file :</p><pre>import PhotosUI<br>import FirebaseStorage </pre><p>The <a href="https://developer.apple.com/documentation/photokit"><em>PhotosUI</em></a> framework will be useful to access the user photo library and <a href="https://firebase.google.com/products/storage"><em>FirebaseStorage</em></a> to use their APIs to upload that selected picture.</p><p>Then, let’s observe these two <a href="https://developer.apple.com/documentation/swiftui/state"><em>State</em></a>, add the following code over the <em>body</em> variable:</p><pre>@State var data: Data?<br>@Statevar selectedItem: [PhotosPickerItem] = []</pre><p>These reference will useful when selecting the picture and upload it as data. The <a href="https://developer.apple.com/documentation/photokit/photospickeritem?changes=_2"><em>PhotosPickerItem</em></a><em> </em>will be used to have a representation type of our data.</p><p>Let’s now create a form composed of a <a href="https://developer.apple.com/documentation/photokit/photospicker"><em>PhotosPicker</em></a> with a button to upload to Firebase.</p><p>Copy/paste the code inside the <em>body</em> variable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27dbaaefbe89a200c6169cdfae6bf24f/href">https://medium.com/media/27dbaaefbe89a200c6169cdfae6bf24f/href</a></iframe><p>Great, we now have a button that let the user select one picture from his phone library. Following the selection, this image is displayed, if the user wish to change it, he can still click and select another one. Also, the button to publish is disabled if there are no images selected.</p><p>SwiftUI make it easy to manage the state of our user input in a few lines of code. You can run your application and test it out:</p><figure><img alt="Screenshot of Xcode running with the Canva" src="https://cdn-images-1.medium.com/max/1024/1*rmhubn2T_hv1Hru3pxqR4w.png" /><figcaption>Canva running in Xcode</figcaption></figure><h4>Upload to Firebase Storage</h4><p>It’s now time to implement a function to uplaod the data we selected to <em>Firebase Storage</em>. To achieve this, we first need to make a reference, copy/paste the following code after the two <em>State</em> we wrote earlier:</p><pre>let storageReference = Storage.storage().reference().child(“\(UUID().uuidString)”)</pre><p>This will be useful to generate a new identifier each time we are uploading an image to Firebase. Now, add the function below inside the button action:</p><pre>  // Function to post data to Firebase Storage<br>      storageReference.putData(data!, metadata: nil) { (metadata, error) in<br>        guard let metadata = metadata else { return }<br>                }</pre><p>You can run the application, select a picture and you will see it on our Firebase console:</p><figure><img alt="Screenshot of the Firebase Storage console with one image uploaded" src="https://cdn-images-1.medium.com/max/1024/1*VVXXSyrpViBMVymDmwNCaw.png" /><figcaption>Uploading a photo to Firebase Storage</figcaption></figure><p>You can upload as many images as you want, every time you upload one, it will get a new identifier.</p><h3>Conclusion</h3><p>In this tutorial, we connected our application to Firebase Storage, setup the framework and upload a photo in just a few lines of code using the new SwiftUI <em>PhotosPicker</em> to access the photo library of our phone.</p><blockquote><strong>You can support my work by either </strong><a href="https://link.springer.com/book/10.1007/978-1-4842-9452-9"><strong>buying my book</strong></a><strong> on SwiftUI &amp; Firebase or subscribing to enjoy unlimited access to my articles and all of Medium </strong><a href="https://sullivan-decarli.medium.com/membership"><strong>through my referral link</strong></a><strong>.</strong></blockquote><p><strong><em>Full code:</em></strong></p><pre>import SwiftUI<br>import PhotosUI<br>import FirebaseStorage<br><br>struct ContentView: View {<br>    @State var data: Data?<br>    @State var selectedItem: [PhotosPickerItem] = []<br>    let storageReference = Storage.storage().reference().child(&quot;\(UUID().uuidString)&quot;)<br><br>    var body: some View {<br>        Form {<br>            Section {<br>                PhotosPicker(selection: $selectedItem, maxSelectionCount: 1, selectionBehavior: .default, matching: .images, preferredItemEncoding: .automatic) {<br>                    if let data = data, let image = UIImage(data: data) {<br>                        Image(uiImage: image)<br>                            .resizable()<br>                            .scaledToFit()<br>                            .frame( maxHeight: 300)<br>                    } else {<br>                        Label(&quot;Select a picture&quot;, systemImage: &quot;photo.on.rectangle.angled&quot;)<br>                    }<br>                }.onChange(of: selectedItem) { newValue in<br>                    guard let item = selectedItem.first else {<br>                        return<br>                    }<br>                    item.loadTransferable(type: Data.self) { result in<br>                        switch result {<br>                        case .success(let data):<br>                            if let data = data {<br>                                self.data = data<br>                            }<br>                        case .failure(let failure):<br>                            print(&quot;Error: \(failure.localizedDescription)&quot;)<br>                        }<br>                    }<br>                }<br>            }<br>            Section {<br>                Button(&quot;Upload to Firebase Storage&quot;) {<br>                    // Function to post data to Firebase Storage<br>                    storageReference.putData(data!, metadata: nil) { (metadata, error) in<br>                        guard let metadata = metadata else {<br>                            return<br>                        }<br>                    }<br>                }.disabled(data == nil)<br>            }<br>        }<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d20aadfacd2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/photospicker-swiftui-firebase-storage-photo-library-3d20aadfacd2">PhotosPicker with Firebase Storage / SwiftUI</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FocusState / SwiftUI]]></title>
            <link>https://medium.com/swift-productions/swiftui-focusstate-navigate-fields-449e470b2971?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/449e470b2971</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[declarative-programming]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Wed, 12 Oct 2022 15:28:09 GMT</pubDate>
            <atom:updated>2022-10-12T15:28:09.044Z</atom:updated>
            <content:encoded><![CDATA[<h4>Short tutorial on how to navigate between TextFields using FocusState</h4><figure><img alt="iPhone opening a form with a keyboard with the SwiftUI logo" src="https://cdn-images-1.medium.com/max/1024/1*Svf8HRVmZ0LjnYnEqIwb7g.png" /></figure><p>Difficulty: <strong>Beginner</strong> | Easy | Normal | Challenging</p><p>Environment : Xcode 14, iOS 16 &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><blockquote>Full code at the end of the article</blockquote><h3>Introduction</h3><p>iOS 15 introduced a new SwiftUI property wrapper called FocusState. It is an helpful API to track which <em>View</em> is receiving user input.</p><p>For the purpose of this tutorial, we will create a<em> </em><a href="https://developer.apple.com/documentation/swiftui/form"><em>SwiftUI Form</em></a> composed of 3 <a href="https://developer.apple.com/documentation/swiftui/textfield"><em>TextField</em></a>. We will then implement the <a href="https://developer.apple.com/documentation/swiftui/focusstate"><em>FocusState</em></a> to navigate between fields using the keyboard, we will also add a focus on the first fields upon screen launch.</p><h3>Create new Xcode Project</h3><p>Open Xcode &gt; App template &gt; SwiftUI Life Cycle and call it <strong>textfieldfocus</strong></p><p><em>Make sure your project is running on iOS 15+</em></p><h4>Creating the Form</h4><p>Heads to the <em>ContentView.swift</em> file and add the followings <em>State</em> so we can observe the user entries in the T<em>extField</em>:</p><pre>@State <strong>private</strong> <strong>var</strong> firstName = &quot;&quot;</pre><pre>@State <strong>private</strong> <strong>var</strong> lastName = &quot;&quot;</pre><pre>@State <strong>private</strong> <strong>var</strong> password = &quot;&quot;</pre><p>Then, add the Form, replace the current boilerplate code inside the <em>body</em> variable by this SwiftUI <em>Form</em>:</p><pre>Form {</pre><pre>TextField(&quot;First name&quot;, text: $firstName)</pre><pre>.disableAutocorrection(<strong>true</strong>)</pre><pre>.submitLabel(.next)</pre><pre>TextField(&quot;Last name&quot;, text: $lastName)</pre><pre>.textContentType(.familyName)</pre><pre>.disableAutocorrection(<strong>true</strong>)</pre><pre>.submitLabel(.next)</pre><pre>SecureField(&quot;Password&quot;, text: $password)</pre><pre>.submitLabel(.done)</pre><pre>}</pre><h4>Implement FocusState</h4><p>Now that we have the user interface, we can implement our focus so that users can navigate between <em>TextField</em>. Add the followings <strong><em>FocusState</em></strong> and <strong><em>enum case</em></strong> over the body variable:</p><pre>@FocusState <strong>private</strong> <strong>var</strong> focusedField: FormField?</pre><pre><strong>enum</strong> FormField {</pre><pre><strong>case</strong> firstName, lastName, password</pre><pre>}</pre><p>To add this ability in the keyboard, we just need to use the <strong><em>.onSubmit</em></strong> modifier. Add it right after the last <strong><em>Form</em></strong> graph:</p><pre>.onSubmit {</pre><pre><strong>  switch</strong> focusedField {</pre><pre><strong>    case</strong> .firstName:</pre><pre>     focusedField = .lastName</pre><pre><strong>   case</strong> .lastName:</pre><pre>    focusedField = .password.  </pre><pre><strong>   case</strong> .password:</pre><pre>    focusedField = <strong>nil</strong></pre><pre>  print(“Name and surname filled: \(firstName),\(lastName)”)</pre><pre><strong>  default</strong>:  </pre><pre>   focusedField = <strong>nil</strong></pre><pre>  }</pre><pre>}</pre><p>And now, we only need to add the focused as a modifier with our TextFields such as follows:</p><pre>TextField(“First name”, text: $firstName)</pre><pre>.disableAutocorrection(<strong>true</strong>)</pre><pre>.submitLabel(.next)</pre><pre><strong>.focused($focusedField, equals: .firstName)</strong></pre><pre>TextField(“Last name”, text: $lastName)</pre><pre>.textContentType(.familyName)</pre><pre>.disableAutocorrection(<strong>true</strong>)</pre><pre>.submitLabel(.next)</pre><pre><strong>.focused($focusedField, equals: .lastName)</strong></pre><pre>SecureField(“Password”, text: $password)</pre><pre>.submitLabel(.done)</pre><pre><strong>.focused($focusedField, equals: .password)</strong></pre><p>Also, with the <strong><em>.onAppear</em></strong> modifier, you can put the focus on the first TextField, this way, upon launch of the screen, the user will be bring to the first <em>TextField</em>.</p><p>Add this after the<strong><em> .onSubmit</em></strong> modifier:</p><pre>.onAppear { focusedField = .firstName }</pre><p>Great, now that is done, we can run our application and navigate between <em>TextField</em> with the keyboard. Also, when the App is launched, we are bring directly to the username <em>TextField</em> and the keyboard is popping up.</p><figure><img alt="Xcode with the simulator running" src="https://cdn-images-1.medium.com/max/1024/1*FxzePkFaGvKiVBteLK706g.png" /><figcaption>Final result</figcaption></figure><p><strong><em>I’m always happy to have a chat and collaborate at </em></strong><a href="/service/mailto:hello@sullivandecarli.com"><strong><em>hello@sullivandecarli.com</em></strong></a><strong><em>. Consider subscribing to enjoy unlimited access to my articles and all of Medium through my referral link:</em></strong></p><p><a href="https://sullivan-decarli.medium.com/membership">Join Medium with my referral link - Sullivan De Carli</a></p><blockquote>Full code below:</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fba036bc28d0a3c0a0130162eb0200b5/href">https://medium.com/media/fba036bc28d0a3c0a0130162eb0200b5/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=449e470b2971" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/swiftui-focusstate-navigate-fields-449e470b2971">FocusState / SwiftUI</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PhotosPicker / SwiftUI]]></title>
            <link>https://medium.com/swift-productions/photospicker-swiftui-tutorial-bcf3a21ffdfc?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/bcf3a21ffdfc</guid>
            <category><![CDATA[ios-16]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Mon, 03 Oct 2022 13:21:13 GMT</pubDate>
            <atom:updated>2022-10-03T13:21:13.718Z</atom:updated>
            <content:encoded><![CDATA[<h4>Short tutorial on how to use the native SwiftUI PhotosPicker with Xcode 14 and iOS 16</h4><figure><img alt="SwiftUI logo with an iPhone opening the photo library" src="https://cdn-images-1.medium.com/max/1024/1*nlgPG-AkCWCvqHOf8sS8Xg.png" /></figure><p>Difficulty: <strong>Beginner</strong> | Easy | Normal | Challenging</p><p>Environment : Xcode 14, iOS 16 &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><blockquote>Full code at the end of the article</blockquote><h3>Create new Xcode Project</h3><p>Open Xcode &gt; App template &gt; Select the SwiftUI Life Cycle and call it <strong>ImagePicker</strong></p><figure><img alt="Screenshot of Xcode, creating a new project" src="https://cdn-images-1.medium.com/max/1024/1*3hzBT1V2f31HLx0QZv3f-Q.png" /><figcaption>Xcode &gt; New Project</figcaption></figure><h4>Let’s code</h4><p>First things to do is to import the framework to access the photo library, head to ContentView and import the framework at the top:</p><pre><strong>import</strong> PhotosUI</pre><p>Now, let’s add two SwiftUI states, add the following code over the body variable:</p><pre>@State <strong>var</strong> selectedItem: [PhotosPickerItem] = []</pre><pre>@State <strong>var</strong> data: Data?</pre><p>The first one will be use to follow the state of the selected image from library and the second for the data it contains, from there, we will be able to display this image in our user interface.</p><p>Now let’s add the <a href="https://developer.apple.com/documentation/photokit/photospicker">PhotosPicker</a>, replace the current code inside inside the <strong><em>VStack</em></strong>:</p><pre>Image(systemName: “globe”)</pre><pre>.imageScale(.large)</pre><pre>.foregroundColor(.accentColor)</pre><pre>Text(“Hello, world!”)</pre><p>With this code:</p><pre>PhotosPicker(selection: $selectedItem, <br>maxSelectionCount: 1, <br>selectionBehavior: .default, <br>matching: .images,<br>preferredItemEncoding: .automatic) { Text(“Select a picture”) }</pre><p>We just implemented the native SwiftUI <em>PhotosPicker </em>with<em> </em>a few parameters:</p><ul><li>selection: the item selection trough a <em>State</em></li><li>maxSelectionCount: the number of picture you can pick up, 1 is enough to our tutorial.</li><li>selectionBehavior: here we left it to default, you can also choose ordered to give an order to the photos.</li><li>matching: the type of content, we choose images but it can be video, live photos etc..</li><li>preferredItemEncoding: the encoding, we left automatic so we are taking the best representation determined by the system.</li></ul><p>You can now run your application and click on “<em>Select a picture</em>”:</p><figure><img alt="Xcode Canva displaying photo library." src="https://cdn-images-1.medium.com/max/1024/1*TMdwlD5LVW6AhHezvI2oxA.png" /><figcaption>Xcode Canva running</figcaption></figure><p>Great, we have access to the photo library with only a few line of codes, let’s extract this data now</p><h4>Extract the image as data</h4><p>Now that we have access to the photo library, we need to extract the image data. To perform, we will use an<em> .</em><a href="https://developer.apple.com/documentation/swiftui/tupleview/onchange(of:perform:)"><em>OnChange</em></a> modifier to the <em>PhotosPicker</em>, add the following code right after the last graph of the <em>PhotosPicker</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e221ed1fd6c5a057932cfaef73070c2/href">https://medium.com/media/5e221ed1fd6c5a057932cfaef73070c2/href</a></iframe><h4>Display the image</h4><p>Now that we have the data extracted, we can display it! Add the following code over the <strong><em>PhotosPicker</em></strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28b768e676ececd0dc436718f44c9796/href">https://medium.com/media/28b768e676ececd0dc436718f44c9796/href</a></iframe><p>This way, when we have something, we display the image otherwise we simply display a gray rectangle.</p><h4>Run your application</h4><p>You can now run your application and test it out.</p><figure><img alt="Xcode simulator running and opening photos selection." src="https://cdn-images-1.medium.com/max/1024/1*W_KGkcz4-5hJaPV08o3dDg.png" /><figcaption>Xcode simulator running</figcaption></figure><blockquote><strong>I’m always happy to have a chat and collaborate at </strong><a href="/service/mailto:hello@sullivandecarli.com"><strong>hello@sullivandecarli.com</strong></a><strong>. Consider subscribing to enjoy unlimited access to my articles and all of Medium through my referral link:</strong></blockquote><p><a href="https://sullivan-decarli.medium.com/membership">Join Medium with my referral link - Sullivan De Carli</a></p><p><strong><em>Here is the full code:</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72146ba2be7b2930b111a3d941b4bfa4/href">https://medium.com/media/72146ba2be7b2930b111a3d941b4bfa4/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcf3a21ffdfc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/photospicker-swiftui-tutorial-bcf3a21ffdfc">PhotosPicker / SwiftUI</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI & Firestore — CRUD exercice]]></title>
            <link>https://medium.com/swift-productions/swiftui-firestore-crud-mvvm-3d816f731b44?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/3d816f731b44</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[ios-16]]></category>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[firebase]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Mon, 26 Sep 2022 14:38:10 GMT</pubDate>
            <atom:updated>2022-09-26T14:38:10.585Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI &amp; Firestore — CRUD exercice</h3><h4>In this tutorial, we will learn how to communicate with Firebase Firestore to create, read, update and delete documents using SwiftUI and MVVM.</h4><figure><img alt="Firebase and SwiftUI logo with 2 iPhones screenshots" src="https://cdn-images-1.medium.com/max/1024/1*xd060FeaeczO5KflKWpb0A.png" /></figure><p>Difficulty: Beginner | Easy | <strong>Normal</strong> | Challenging</p><p>Environment : Xcode 14, iOS 16, MVVM, Firebase &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><blockquote>Repository at the end of this article</blockquote><h4>Introduction</h4><p>The application will be similar to the Apple Note, it is going to feature to ability to post a note, read it, edit it and also deleting it. We are going to use the Model — View — View Model design pattern, so we have a separation of concern between each action. Let’s get started!</p><h4>Firebase Set Up</h4><p>First things is to create a new Xcode project, call it <strong><em>Note</em></strong>.</p><p>Then, integrate Firebase to your project, I invite you to check out this article and get back if you don’t know how to set it up:</p><p><a href="https://medium.com/swift-productions/how-to-add-firebase-with-spm-swiftui-d083189d510d">How to add Firebase to a SwiftUI Project with SPM</a></p><p>During the process, make sure to add the two followings dependencies:</p><ul><li>FirebaseFirestore</li><li>FirebaseFirestoreSwift</li></ul><h4>Set Up Firestore</h4><p>Firestore will be our NoSQL, document based database where we will upload and edit content, head to the Firebase Firestore section in the Firebase console and click on “Create database”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N7ruPhwnnWzPlFp6ZSjDaQ.png" /></figure><p>Then, select “Start in test mode” (will be enough for the purpose of this tutorial) and enable Firestore:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f8awcbAihUfBYpbLprjihw.png" /></figure><p>You are now ready to receive and update documents to and from Firestore!</p><h4>Model</h4><p>let’s create our Model, this is going to be our reference from where we will post and edit data. It is going to be composed of an <strong><em>identifier</em></strong> and a <strong><em>String</em></strong> which will contain our <em>Note</em>. Go ahead and create a new Swift File and call it <strong><em>Note</em></strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95d1e7de705bf66a993ab5dbc6057223/href">https://medium.com/media/95d1e7de705bf66a993ab5dbc6057223/href</a></iframe><blockquote>The <strong><em>@DocumentID</em></strong> is the Firestore document&#39;s reference that we get from the module FirebaseFirestoreSwift.</blockquote><h4>View Model</h4><p>Let’s now use this model and make a connection with the Firebase backend. We are going to implement all the functions in the View Model.</p><p>Create a new Swift file and call it <strong><em>NoteViewModel</em></strong>,</p><p>Then, copy/paste the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21f565092fd2d1e4df5211a171b34fe1/href">https://medium.com/media/21f565092fd2d1e4df5211a171b34fe1/href</a></iframe><blockquote><strong>Here, we have done a couple of things:</strong></blockquote><ul><li>First, we made a reference to our <em>Model</em>, we created earlier.</li><li>We made a reference to the Firestore database collection, we will use this path to post, read, update and delete data.</li><li>We implement the function to post the data to cloud firestore, passing a <em>String</em> (the title).</li><li>We add a listener to fetch all documents under the Firestore collection “Notes”.</li><li>We add the function to update the <em>String</em> of the selected document.</li><li>Finally we add the function to delete data based on the indexSet, this will allow us to remove the data from both our List and erase it from the database.</li></ul><p>We have everything to create our user interface now.</p><h4>View</h4><p>Our user interface will be compose of a <em>List</em> which will allow us to delete a row and navigate to another screen. From there you can also edit it.</p><p>You will also be able to post a note trough an <strong><em>alert</em></strong> with a <strong><em>textField</em></strong>.</p><p>Heads to the <strong><em>contentView.swift</em></strong> file, first make a reference to our View Model:</p><pre>@ObservedObject <strong>private var </strong>viewModel = NoteViewModel()</pre><h4>Read data</h4><p>Then heads the body variable and implement a <em>SwiftUI List </em>embed in a <em>NavigationStack</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a18c50df806145e251deb10a87815c0e/href">https://medium.com/media/a18c50df806145e251deb10a87815c0e/href</a></iframe><h4>Post data</h4><p>Great! We just implemented a <em>List</em> that fetch data from Firestore in real time. Let’s post some data now, in the same file, add the followings two <em>States:</em></p><pre>@State <strong>private</strong> <strong>var</strong> presentAlert = <strong>false</strong></pre><pre>@State <strong>private</strong> <strong>var</strong> titleText: String = “”</pre><p>This will allow us to present and close an alert and read user’s input from the <em>TextField</em>.</p><p>Now, add the following code right under your <strong><em>List:</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/054413298176e3936c24bcafb5737357/href">https://medium.com/media/054413298176e3936c24bcafb5737357/href</a></iframe><p>This <a href="https://developer.apple.com/documentation/swiftui/view/toolbar(content:)-5w0tj"><strong><em>toolBar</em></strong></a> which contains a button to present an alert with a text entry will allow us to post data to Firestore, you can now go ahead and write a few:</p><figure><img alt="2 screenshots of an iPhone presenting an alert and a list" src="https://cdn-images-1.medium.com/max/1024/1*MUgb7gICuh_nYejrfxYNCA.png" /><figcaption>Screenshots of our application running</figcaption></figure><p>It also contains a text to present how many notes are written (with the <em>.count</em> key).</p><p>Now that we are able to read and post notes thanks to the<a href="https://firebase.google.com/docs/database/ios/read-and-write"> great APIs of Firestore</a>, let’s try to update these notes.</p><h4>Passing Data</h4><p>Before updating the data, we will first implement a <em>DetailsView</em>, from there, we will edit our document.</p><p>Go ahead and create a new SwiftUI View, call it <strong><em>DetailsView</em></strong>, then integrate the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0183e540aa3e8e98c680d7dbbeed62f9/href">https://medium.com/media/0183e540aa3e8e98c680d7dbbeed62f9/href</a></iframe><p>Then, go back to the <strong><em>ContentView.swift</em></strong> file and replace your current <em>List</em> with this one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/044e3065a91be4dcfcf5b29742a85d97/href">https://medium.com/media/044e3065a91be4dcfcf5b29742a85d97/href</a></iframe><p>This will allow us to navigate from one view to another while passing data with N<em>avigationLink</em>.</p><h4>Update data</h4><p>For updating the data, we will first implement another <em>toolBarItem</em> with an <em>alert</em>, from there we will allow the user to enter a new text.</p><p>First, let’s observe the followings <em>states</em> at the top of <strong><em>DetailsView.swift</em></strong>:</p><pre>@State <strong>private</strong> <strong>var</strong> presentAlert = <strong>false</strong></pre><pre>@State <strong>private</strong> <strong>var</strong> titleText: String = &quot;&quot;</pre><pre>@ObservedObject <strong>private</strong> <strong>var</strong> viewModel = NoteViewModel()</pre><p>With these states, we are linking to the View Model to pass data to Firestore, handling alert presentation and managing the text entry. We can now integrate our toolbar, copy/paste the code below right we we left a comment for the toolbar:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8246181a249f49c81cbb16995346645c/href">https://medium.com/media/8246181a249f49c81cbb16995346645c/href</a></iframe><p>You can now run your application, navigate between views and <a href="https://firebase.google.com/docs/firestore/manage-data/add-data#update-data">update your notes to Firestore</a> by clicking on “edit” and entering a new text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Vcpq3MBbNDVLWgSicNKjw.png" /></figure><h4>Delete data</h4><p>Last step, delete our notes, this one is simple, SwiftUI is handling everything with the List modifier, add the following <a href="https://developer.apple.com/documentation/SwiftUI/ForEach/onDelete(perform:)"><strong><em>onDelete</em></strong></a> modifier in the <strong><em>ContentView.swift</em></strong> file:</p><pre>List {</pre><pre>      ForEach(viewModel.notes, id:\.id) { Note <strong>in</strong></pre><pre>        NavigationLink(destination: DetailsView(note: Note)) {</pre><pre>          VStack(alignment: .leading) {</pre><pre>            Text(Note.title ?? &quot;&quot;).font(.system(size: 22, weight: .regular))</pre><pre>       }.frame(maxHeight: 200)</pre><pre>    }</pre><pre>  }<strong>.onDelete(perform: self.viewModel.deleteData(at:))</strong></pre><pre> }.onAppear(perform: <strong>self</strong>.viewModel.fetchData)</pre><pre> .navigationTitle(&quot;Notes&quot;)</pre><p>You can now swift right on any list row, click on delete, this will <a href="https://firebase.google.com/docs/firestore/manage-data/delete-data">delete the data from <em>Firestore</em></a> and remove it from the list indexes at the same time.</p><h4>Conclusion</h4><p>With this exercice, we learn how to connect our application to Firebase Firestore, create, read, update and delete data to and from the database. We also use the Model, View, View-Model to better organise our codebase.</p><blockquote><strong>I’m always happy to have a chat and collaborate at </strong><a href="/service/mailto:hello@sullivandecarli.com"><strong>hello@sullivandecarli.com</strong></a><strong>. Consider subscribing to enjoy unlimited access to my articles and all of Medium </strong><a href="https://sullivan-decarli.medium.com/membership"><strong>through my referral link</strong></a><strong>.</strong></blockquote><h4>Github repository:</h4><p><a href="https://github.com/Sullivan677/Note.git">GitHub - Sullivan677/Note: A replica of the Apple Note application built with SwiftUI &amp; Firebase Firestore</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d816f731b44" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/swiftui-firestore-crud-mvvm-3d816f731b44">SwiftUI &amp; Firestore — CRUD exercice</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sign In anonymously with Firebase | SwiftUI]]></title>
            <link>https://medium.com/swift-productions/anonymous-authentication-firebase-auth-swiftui-e3597df43e8f?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/e3597df43e8f</guid>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[firebaseauthentication]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Tue, 30 Aug 2022 14:48:07 GMT</pubDate>
            <atom:updated>2022-09-21T19:12:28.364Z</atom:updated>
            <content:encoded><![CDATA[<h4>In this tutorial, we will use Firebase Auth SDK to generate a unique identifier without asking any information for our user.</h4><figure><img alt="Firebase and SwiftUI logo with an iPhone screenshot" src="https://cdn-images-1.medium.com/max/1024/1*gjiRWeuMqCspoAWbOZtJAQ.png" /><figcaption>The screen we are going to build</figcaption></figure><p>Difficulty: Beginner | <strong>Easy</strong> | Normal | Challenging</p><p>Environment : Xcode 13, iOS 15, <a href="https://firebase.google.com/support/release-notes/ios">Firebase 9.0.0</a> &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><p>Reference: <a href="https://firebase.google.com/docs/auth/ios/anonymous-auth?hl=en&amp;authuser=0">Firebase documentation</a></p><h3>Create a new project</h3><p>Open Xcode &gt; App template &gt; Select SwiftUI Interface and Life Cycle &gt; call your project <strong>FirebaseLogin</strong><em>.</em></p><figure><img alt="Screenshot of the Xcode application" src="https://cdn-images-1.medium.com/max/1024/1*QSGS3SX34IECSAd9Rs6ibA.png" /><figcaption>Xcode, new project</figcaption></figure><h3>Connect to the Firebase SDK with SPM</h3><p>Please, follow this tutorial to create a Firebase project and connect an iOS application to it.</p><p><a href="https://medium.com/swift-productions/how-to-add-firebase-with-spm-swiftui-d083189d510d">How to add Firebase to a SwiftUI Project with SPM</a></p><p>During the process, make sure to include theFirebase Auth module when using <a href="https://github.com/firebase/firebase-ios-sdk">Swift Package manager</a>:</p><figure><img alt="Screenshot of Swift Package Manager selecting FirebaseAuth module" src="https://cdn-images-1.medium.com/max/1024/1*MlQw0KK1fbJyt3gR8xG-uA.png" /><figcaption>SPM &gt; add package</figcaption></figure><p>Once you have successfully connected Firebase to your application with the Firebase Auth framework, we can proceed with the Firebase console set up and coding.</p><h3>Set up Firebase Authentication</h3><p>Go to your Firebase console and select the <strong><em>Authentication</em></strong> section, click on the “Get started” button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yhyvmJeRZ3i2OOFHC_MzZA.png" /></figure><p>Then enable Anonymous Authentication under native provider:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ov_NkANZrzyJEh1GAMriMQ.png" /><figcaption>Firebase authentication &gt; anonymous provider</figcaption></figure><p>Et voilà! You are now ready to send and receive information to and from Firebase, we can now code.</p><h4>Create the functions</h4><p>It’s now time to code, go ahead and create a new Swift file, call it <strong><em>AuthViewModel </em></strong>and copy/paste the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7623ae250a6a591cf0fb842c7569d87/href">https://medium.com/media/e7623ae250a6a591cf0fb842c7569d87/href</a></iframe><p>Here what we have done:</p><ul><li>We made a reference to our user model, which is provide by the Firebase Auth SDK (<a href="https://firebase.google.com/docs/reference/ios/firebaseauth/api/reference/Classes/FIRUser">FIRUser</a>), no need to create one.</li><li><strong><em>listenToAuthState</em></strong> We have a function to determine if the user is logged in or has terminated the session, we will use it to display information later.</li><li><strong><em>SignInAnonymously</em></strong> We implement the sign in anonymously function to sign up to Firebase and create a user identifier.</li><li><strong><em>SignOut</em></strong> We implement the sign-out function to terminate the session.</li></ul><p>Now, I invite you to observe this <em>View Model</em> at the App startup, in the <em>FirebaseLoginApp.swift</em> file, add next to <strong><em>ContentView()</em></strong>, add the following: <strong><em>.environmentObject(AuthViewModel())</em></strong></p><pre><strong>@main</strong></pre><pre><strong>struct</strong> FirebaseLoginApp: App {</pre><pre>  @UIApplicationDelegateAdaptor(AppDelegate.<strong>self</strong>) <strong>var</strong> delegate</pre><pre><strong> var</strong> body: <strong>some</strong> Scene {</pre><pre>  WindowGroup {</pre><pre>   NavigationView {</pre><pre>  ContentView()<strong><em>.environmentObject(AuthViewModel())</em></strong></pre><pre>   } <br>  } <br> }</pre><pre>}</pre><p>This will alllow us to check the user status when the application is starting.</p><p>Now that we have the necessary function, we can go back to our <strong><em>ContentView.swift</em></strong> file and build the UI. First, let’s import the framework:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0063414606ec758cd52dad7496cf47a4/href">https://medium.com/media/0063414606ec758cd52dad7496cf47a4/href</a></iframe><p>Then, replace the code you have in <strong><em>ContentView.swift</em></strong> file to the following one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8ae43da5aa8a9a9c5f0cd1e44f9bfb8/href">https://medium.com/media/d8ae43da5aa8a9a9c5f0cd1e44f9bfb8/href</a></iframe><p>Great! We have done a few things here:</p><ul><li>We made a reference to our <em>View Model</em></li><li>We are displaying the user identifier as a <strong><em>String</em></strong> and performing a session check with <strong><em>listenToAuthState</em></strong></li><li>We implement a button to sign up anonymously</li><li>We implement a button to terminate the session</li></ul><p>You can now run the application and click on Sign in anonymously:</p><figure><img alt="Screenshots of Xcode and the Firebase console, authenticate section" src="https://cdn-images-1.medium.com/max/1024/1*xa8kokGw0LxWkz8QwbFAPw.png" /><figcaption>Final result</figcaption></figure><p>Great! It is working as expected, our user is registered on the Firebase dashboard and both the identifier displayed in our App match with the one in the dashboard.</p><p>Also, when we logout, the identifier disappear because we are listening the user status in real time thanks to <a href="https://firebase.google.com/docs/auth/ios/manage-users"><strong><em>addStateDidChangeListener</em></strong></a><strong><em> . </em></strong>Therefore, when logged out, the session is terminated.</p><p>You can now onboard your user with anonymous sign-in for your next project, it is a great way to get a user identifier without requiring any information.</p><p>I also recommend you to implement a conversion from anonymous account to a permanent one once your users are using the app enough, more information in the<em> </em><a href="https://firebase.google.com/docs/auth/ios/anonymous-auth"><em>Firebase documentation</em></a><em>.</em></p><blockquote><strong>Thanks for reading! I’m always happy to have a chat and collaborate at </strong><a href="/service/mailto:hello@sullivandecarli.com"><strong>hello@sullivandecarli.com</strong></a><strong>. Consider subscribing to enjoy unlimited access to my articles and all of Medium </strong><a href="https://sullivan-decarli.medium.com/membership"><strong>through my referral link</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3597df43e8f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/anonymous-authentication-firebase-auth-swiftui-e3597df43e8f">Sign In anonymously with Firebase | SwiftUI</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI | Firebase Auth — Manage a User Session]]></title>
            <link>https://medium.com/swift-productions/swiftui-firebase-auth-listener-user-signup-manage-fae2294e8192?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/fae2294e8192</guid>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[firebaseauthentication]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Mon, 08 Aug 2022 18:17:55 GMT</pubDate>
            <atom:updated>2023-09-28T15:50:41.431Z</atom:updated>
            <content:encoded><![CDATA[<h3>SwiftUI | Firebase Auth — Manage a User Session</h3><h4>In this tutorial, we are going to integrate Firebase Auth SDK, implement sign ups with email/password and sign outs, and handle the UI based on a user session</h4><figure><img alt="The Firebase and SwiftUI logo with 2 iPhones screens" src="https://cdn-images-1.medium.com/max/1024/1*jVEO0HVV4L8iciaqntlMMQ.png" /></figure><p>Difficulty: Beginner | Easy | <strong>Normal</strong> | Challenging</p><p>Environment : Xcode 13, iOS 15, MVVM, Firebase &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><p><em>Repository at the end of this article</em></p><h4>Introduction</h4><p>We are going to learn how to authenticate our users with an email address, <a href="https://firebase.google.com/docs/auth/ios/manage-users?hl=en&amp;authuser=0#get_the_currently_signed-in_user">get the currently signed-in user with a <em>listener</em></a> and present the sign in screen or the main screen accordingly, as done on popular social media login flows, like Instagram, TikTok, etc.</p><p>This is a great starter if you’re looking to build an application in which access is restricted to authenticated users only.</p><blockquote><strong><em>The logic we are going to implement:</em></strong></blockquote><figure><img alt="An illustration showing a login flow using Firebase backend" src="https://cdn-images-1.medium.com/max/1024/1*EAyF0QvZ9JauVSalims12w.png" /><figcaption>Illustration of the logic we are going to implement</figcaption></figure><h4>Create a new project</h4><p>Go to Xcode &gt; Create a new project &gt; SwiftUI interface &amp; lifecycle &gt; Name it <strong><em>FireLogin</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IJUagijRkTKVJsjHwliZiA.png" /></figure><h4>Add the Firebase SDK to your project</h4><p>Go to the <a href="https://console.firebase.google.com/">Firebase console</a> and create a new project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LMqpEBneMRV8WgFPZyMyEA.png" /></figure><h4>Add an iOS App to your Firebase project</h4><p>Click on iOS+ and follow the 5 steps presented by Firebase. Make sure to add the Firebase Auth module with the Swift Package Manager:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b8E3Dl0wNqwFbakY9IQMMA.png" /></figure><p>Once you have added the <strong><em>GoogleService-info.plist</em></strong> file into your folder and implemented the code below in the file <strong><em>FireLoginApp.swift</em></strong>, you should be successfully connected to Firebase.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/628e3ff5c845a172add92424a965258f/href">https://medium.com/media/628e3ff5c845a172add92424a965258f/href</a></iframe><blockquote>How your FireLoginApp.swift file should look.</blockquote><h4>Configure Firebase Auth</h4><p>Now, head to your Firebase console and add Firebase Auth capabilities — we only need the email provider for now. To do so, click on <strong><em>get started</em></strong>, select <strong><em>email/password</em></strong> provider and switch it on to enable it, then save the changes. You are now ready to communicate user information back and forth with Firebase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*svPOiikXLpElPRnHLeQuqw.png" /></figure><h4>Let’s implement the View Model</h4><p>Thanks to Firebase’s APIs, we don’t need to create a model. It is already provided as part of the <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=&amp;ved=2ahUKEwjTpdLMuq_5AhWTi_0HHVYFBN4QFnoECAcQAQ&amp;url=https%3A%2F%2Ffirebase.google.com%2Fdocs%2Freference%2Fios%2Ffirebaseauth%2Fapi%2Freference%2FClasses%2FFIRUser&amp;usg=AOvVaw0FXw_9MZjJuljL6GrLAaxI"><strong><em>FIRUser</em></strong></a> and it contains all the information that we need. So let’s go ahead and create a Swift file and call it <strong><em>AuthViewModel</em></strong>, then paste the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15b3426d9c56b3b6a70072037b454239/href">https://medium.com/media/15b3426d9c56b3b6a70072037b454239/href</a></iframe><p>In this code we handle multiple crucial steps:</p><ul><li>We implemented a reference to our model, the Firebase user that we introduced as a variable.</li><li>We added <a href="https://firebase.google.com/docs/auth/ios/start?hl=en&amp;authuser=0#listen_for_authentication_state">a listener to check if the user is logged in or not</a>.</li><li>We implemented a <a href="https://firebase.google.com/docs/auth/ios/start?hl=en&amp;authuser=0#sign_up_new_users">function to sign up our user </a>with an email and a password.</li><li>We added the necessary function to log out our user and end the current session.</li></ul><p>Great, now we can head to the <strong><em>FireLoginApp.swift </em></strong>file and add an <a href="https://developer.apple.com/documentation/swiftui/environmentobject"><em>environmentObject</em></a> as part of the body variable next to the <strong><em>ContentView()</em></strong>.</p><pre>.environmentObject(AuthViewModel())</pre><p>This way, we will have a binding with our <strong><em>View Model</em></strong> to observe the user status at the app start up.</p><p>Since the <em>ContentView.swift</em> file will be the first screen we show, we are going to handle the logic in this file:</p><ul><li>If there is no user session active, present the sign up screen.</li><li>If the user is logged in, present the main screen.</li></ul><p>In <em>ContentView.swift</em>, start by observing our View Model:</p><pre>@EnvironmentObject private var authModel: AuthViewModel</pre><p>Then replace the current Text “Hello, world!” with the code below inside the <strong><em>Body</em></strong> variable:</p><pre>Group {<br>if authModel.user != nil {</pre><pre>MainView() <br>} else {</pre><pre>SignUpView() </pre><pre>} <br>}.onAppear {</pre><pre>authModel.listenToAuthState() <br>}</pre><h4>Create the user interface</h4><p>We have just implemented the logic that we described earlier, only one step is missing: creating both screens, SignUpView and MainView.</p><p>Go ahead and create two <strong><em>SwiftUIView </em></strong>files, call one <strong><em>SignUpView</em></strong> and the other <strong><em>MainView.</em></strong></p><p>In <strong><em>SignUpView</em></strong>, implement the following code:</p><p>First, we observe our <em>View Model</em>:</p><pre>@EnvironmentObject private var authModel: AuthViewModel</pre><p>Then, we add two variables for the email and password:</p><pre>@State private var emailAddress: String = &quot;&quot; <br>@State private var password: String = &quot;&quot;</pre><p>Now, we can implement the user interface in the <strong><em>body</em></strong> varaible:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4521b5c4fd69a442fa944630067ca387/href">https://medium.com/media/4521b5c4fd69a442fa944630067ca387/href</a></iframe><p>You can now run your application and sign up your first user! Once you have clicked on “sign up,” you should be able to find your user in the Firebase console &gt; Authentication</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*orUv8DljSQt_s60V1ZR1Xw.png" /></figure><p>Great, our user is registered on Firebase and after it is logged in, we are bringing it to the <strong><em>MainView.swift.</em></strong></p><p>Now that our user can sign up, we are going to add the function to logout and display the current user’s email.</p><p>Observe the <strong>AuthViewModel </strong>inside the <strong><em>MainView.swift </em></strong>file.</p><pre>@EnvironmentObject private var authModel: AuthViewModel</pre><p>And implement the following code inside the<em> body variable</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f8cb7268f1bf93770e84732b0d4389f/href">https://medium.com/media/5f8cb7268f1bf93770e84732b0d4389f/href</a></iframe><p>Great, now you can run your application again. Since we have implemented the listener to check user status, we will have our <strong><em>MainView</em></strong> presented first with the current email.</p><p>When clicking on logout, our session is terminated and we are back to the <strong><em>SignUpView</em></strong>.</p><h4>Where to go from here?</h4><p>We just implemented a logic to restrict user access to only authenticated users, it is fast and easy thanks to the APIs provided by Firebase. You can now implement the sign-in and forget password function. Also, to make the user experience even better, you can check out my other article on how to integrate <em>Sign In with Apple.</em> The code we implemented to check the user session is also going to work great with it, you might just need to adapt the user interface:</p><p><a href="https://betterprogramming.pub/sign-in-with-apple-firebase-auth-swiftui2-0-5e007f1e5a53">Sign in With Apple Using Firebase Auth SDK and SwiftUI 2</a></p><blockquote><strong><em>You can support my work by either </em></strong><a href="https://link.springer.com/book/10.1007/978-1-4842-9452-9"><strong><em>buying my book</em></strong></a><strong><em> on SwiftUI &amp; Firebase or subscribing to enjoy unlimited access to my articles and all of Medium </em></strong><a href="https://sullivan-decarli.medium.com/membership"><strong><em>through my referral link</em></strong></a><strong><em>.</em></strong></blockquote><h4>GitHub Repository:</h4><p><a href="https://github.com/Sullivan677/FireLogin">GitHub - Sullivan677/FireLogin: A login flow using Firebase and SwiftUI to restrict access to authenticated users only.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fae2294e8192" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/swiftui-firebase-auth-listener-user-signup-manage-fae2294e8192">SwiftUI | Firebase Auth — Manage a User Session</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI | fetch data from Firebase Realtime Database]]></title>
            <link>https://medium.com/swift-productions/swiftui-fetch-data-from-firebase-realtime-database-611406d8696c?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/611406d8696c</guid>
            <category><![CDATA[firebaserealtimedatabase]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[iosdev]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Fri, 08 Jul 2022 13:46:29 GMT</pubDate>
            <atom:updated>2022-07-08T13:46:29.471Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to read data from Firebase Realtime database</h4><figure><img alt="Firebase and SwiftUI logo with an iPhone displaying a list of birds." src="https://cdn-images-1.medium.com/max/1024/1*PUTbDb_wvZ74xSa6XjzXJg.png" /><figcaption>The App we are going to build</figcaption></figure><p>Difficulty: Beginner | Easy | <strong>Normal</strong> | Challenging</p><p>Environment : Xcode 13, iOS 15, MVVM, Firebase &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><blockquote>Github repository at the the end of the tutorial</blockquote><h4>Introduction</h4><p>When looking forFirebase tutorials, we often found articles related to Firestore or the authentication SDK. It is forgetting about their realtime database that makes it easy to import our data with a JSON file.</p><p>In this tutorial, we are going to import a JSON containing a list of birds and read it using the Firebase SDK, let’s get started!</p><h3>Create a new project</h3><p>Go to Xcode &gt; Create a new project &gt; SwiftUI interface &amp; lifecycle &gt; Name it <strong><em>Bird</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WZw9nPkrkHJ2cWxb6JKIGg.png" /><figcaption>Xcode &gt; Create a new project</figcaption></figure><h4>Add the Firebase SDK to your project</h4><p>If you don’t know how to add Firebase to your project, please, check out my article below and come back here:</p><p><a href="https://medium.com/swift-productions/how-to-add-firebase-with-spm-swiftui-d083189d510d">How to add Firebase to a SwiftUI Project with SPM</a></p><blockquote><strong>Make sure to add the package <em>FirebaseDatabase</em>, <em>FirebaseDatabaseSwift during the process.</em></strong></blockquote><figure><img alt="Screenshot fo Xcode Swift Package Manager" src="https://cdn-images-1.medium.com/max/1024/1*NbJYg1hONbUc_ndEmVlZ0w.png" /><figcaption>Xcode &gt; Add Packages — Firebase Database</figcaption></figure><h4>Set up Firebase Realtime Database</h4><p>Go to your Firebase Console, in the Realtime Database section, set up the database so it is ready to receive data.</p><figure><img alt="Screenshot of Firebase console — Realtime database" src="https://cdn-images-1.medium.com/max/1024/1*KvcXs-Dixj_LBBOhcR5dWA.png" /><figcaption>Firebase Realtime Database</figcaption></figure><h4>Import data to Firebase</h4><p>I prepared a small JSON file so we have some data to read, download the <em>bird.json</em> file from the following URL:</p><p><a href="https://drive.google.com/file/d/1-UBF7alIqU6dfu6xLBHl6uPU69qgYChz/view?usp=sharing">bird.json</a></p><p>And, import it with the left icon inside Firebase &gt; import JSON</p><figure><img alt="Screenshot of the realtime database section in Firebase console" src="https://cdn-images-1.medium.com/max/1024/1*d-SE-XgXOkcWE4ZOqIl7XA.png" /><figcaption>Realtime Database &gt; import JSON</figcaption></figure><p>You should now have a JSON tree inside your database, we can now start coding!</p><blockquote><strong>Important note: it is possible that your GoogleService-info.plist file doesn’t contains the database URL related to the Realtime Database you just created. You application will not be able to connect to the database.</strong></blockquote><h4>How to fix it?</h4><p>Simply go to project setting, under SDK setup and configuration, download <strong><em>GoogleService-info.plist </em></strong>again, delete the old one and drag the new one to your project folder, it should now contains an entry <strong><em>DATABASE_URL </em></strong>of type String with the correct URL. You are good to go!</p><figure><img alt="Firebase console, project setting" src="https://cdn-images-1.medium.com/max/1024/1*QUpYRFElzIIjVyD87qm2kA.png" /><figcaption>Firebase console &gt; project setting</figcaption></figure><h4>Let’s create the Model</h4><p>First things first, we are going to create our Model, go ahead and create a new file, call it <strong><em>Bird</em></strong>, then, paste the model below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f3f9ee734340e3117f0a7af9e5674ea/href">https://medium.com/media/7f3f9ee734340e3117f0a7af9e5674ea/href</a></iframe><h4>Create the View Model to fetch data</h4><p>Now that we have our Model, we can create the <em>View Model</em> where we are going to connect with Realtime Database and fetch the data.</p><p>Create a new Swift file, call it <em>BirdViewModel</em> and paste in the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40c76ef3d7c463001595a455a374ccb4/href">https://medium.com/media/40c76ef3d7c463001595a455a374ccb4/href</a></iframe><blockquote><strong>Little explanation here:</strong></blockquote><ul><li>First, we started by importing the Firebase framework to access their APIs.</li><li>Then, with the <em>Published</em> variable we made a reference to the Model we created earlier. Then, we created a database reference which is a String such as follow: “birds”. make sure that it matches what you have in your Realtime Database console, otherwise it won’t display anything.</li><li>We then implemented a function to read the JSON file hold by Firebase, we have a snapshot of the data and we are encoding/decoding the JSON that it holds, also we are printing the error in case there are any.</li><li>Finally, we have a function to stop listening to the database so it doesn’t create a memory retainer which can cause a crash later in your application.</li></ul><h4>Let’s build the user interface!</h4><p>Now that we have successfully created a <strong><em>Model</em></strong> and a<strong> <em>View Model</em></strong> connected to Firebase realtime Database, we can create a user interface and display these data, it is going to be a simple list containing a text and an image.</p><p>Head to the<em> </em><strong><em>ContentView.swift</em></strong> file and let’s observe our<em> View Model</em> such as follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2559ad3d8c958e15e7893b5fd9d2f369/href">https://medium.com/media/2559ad3d8c958e15e7893b5fd9d2f369/href</a></iframe><p>Great, now we are able to fetch data from Firebase and inform the UI accordingly. It’s now to build our list containing image and text.</p><p>Add the following list inside the body variable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18b2a461751c0420bef6c017dcf617c5/href">https://medium.com/media/18b2a461751c0420bef6c017dcf617c5/href</a></iframe><p>Great, now that we have our list UI ready, handling also the user interface in case the images are slow to download. we can download our data, simply using the <em>.onAppear</em> modifier on our list and the <em>.onDisappear </em>to stop receiving update from the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bae03b386829ee05449f081275065f5/href">https://medium.com/media/2bae03b386829ee05449f081275065f5/href</a></iframe><h4>Run your application</h4><p>Congratulation! You can now run your application and start receiving data from Firebase. In your simulator, the list will appear with a progress spinner for the images while there are being downloaded.</p><figure><img alt="Xcode running the simulator displaying a SwiftUI List" src="https://cdn-images-1.medium.com/max/1024/1*OvMKUE_JUMiF6loy8kRX7Q.png" /><figcaption>Final result</figcaption></figure><blockquote><strong><em>I’m always happy to have a chat and collaborate at </em></strong><a href="/service/mailto:hello@sullivandecarli.com"><strong><em>hello@sullivandecarli.com</em></strong></a><strong><em>. Consider subscribing to enjoy unlimited access to my articles and all of Medium </em></strong><a href="https://sullivan-decarli.medium.com/membership"><strong><em>through my referral link</em></strong></a><strong><em>.</em></strong></blockquote><h4><em>Find the code on my Github repositories:</em></h4><p><a href="https://github.com/Sullivan677/Bird">GitHub - Sullivan677/Bird: A SwiftUI List fetching JSON data from Firebase Realtime Database</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=611406d8696c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/swiftui-fetch-data-from-firebase-realtime-database-611406d8696c">SwiftUI | fetch data from Firebase Realtime Database</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SwiftUI | Email/Password Signup Using Firebase]]></title>
            <link>https://medium.com/swift-productions/swiftui-email-password-signup-login-firebase-e69fcea92849?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/e69fcea92849</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[email-login]]></category>
            <category><![CDATA[firebaseauthentication]]></category>
            <category><![CDATA[firebase]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Wed, 18 May 2022 09:44:27 GMT</pubDate>
            <atom:updated>2022-05-18T09:44:26.960Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to implement an email/password authentication with the Firebase Auth APIs</h4><figure><img alt="Firebase and SwiftUI logo with an iPhone login page" src="https://cdn-images-1.medium.com/max/1024/1*nbRXHBtNT83JB1GF3XNgDw.png" /><figcaption>The screen we are going to build</figcaption></figure><p>Difficulty: Beginner | Easy | <strong>Normal</strong> | Challenging</p><p>Environment : Xcode 13, iOS 15, <a href="https://firebase.google.com/support/release-notes/ios">Firebase 9.0.0</a> &amp; <a href="https://developer.apple.com/xcode/swiftui/">SwiftUI</a></p><h4>Create a new project</h4><p>Open Xcode &gt; App template &gt; Select SwiftUI Interface and Life Cycle &gt; call your project <strong>FirebaseTest</strong><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gLyf4BhETGUAcQAfUmXuKA.png" /></figure><h4>Connect to the Firebase SDK with SPM</h4><p>It’s now time to add the Firebase SDK to our project to communicate with the Firebase authenticate APIs. Please, check out the following article to learn how to add Firebase to your App:</p><p><a href="https://medium.com/swift-productions/how-to-add-firebase-with-spm-swiftui-d083189d510d">How to add Firebase to a SwiftUI Project with SPM</a></p><p>If you have already connected Firebase to your SwiftUI project, you can also go ahead.</p><h4>Set up Firebase Authentication</h4><p>Go to your Firebase console and navigate to the <em>Authentication</em> section:</p><figure><img alt="Screenshot from Firebase Authentication panel" src="https://cdn-images-1.medium.com/max/1024/1*DRRDvSbQMqVyrMsIGWrX5g.png" /><figcaption>Firebase console &gt; Authentication</figcaption></figure><p>From there click on Get started, and enable email/password login such as follow:</p><figure><img alt="Steps to follow to enable email login with Firebase" src="https://cdn-images-1.medium.com/max/1024/1*CB1fbU3yfegOrgEqSfbehQ.png" /><figcaption>Firebase authenticate &gt; Sign-in method</figcaption></figure><p>You app is now ready to register users to the the Firebase’s backend.</p><h4>Let’s build the user interface</h4><p>Now that Firebase’s backend is successfully communicating with our Application, we can implement our UI. First, create a new file and call it <strong>LoginView.</strong></p><p>We will implement a few things, a <a href="https://developer.apple.com/documentation/swiftui/picker"><em>Picker</em></a> to switch between sign up and login on the same screen, 2 <a href="https://developer.apple.com/documentation/swiftui/textfield"><em>TextField</em></a>, one for the email and one for the password with a <a href="https://developer.apple.com/documentation/swiftui/button"><em>Button</em></a> to sign up or login.</p><p>But, first, let’s import the framework at the top of our File:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71385df718cd0377271fcd571d6949fb/href">https://medium.com/media/71385df718cd0377271fcd571d6949fb/href</a></iframe><p>Then we will add three <em>State</em> variables to follow the evolution of our <em>Picker</em> and the entries for both of our <em>TextField</em>. Add the following code inside the <strong><em>struct</em></strong><em> LoginView</em> but over the <strong><em>Body </em></strong>variable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49a654dbef7597f36568b5a70d17d823/href">https://medium.com/media/49a654dbef7597f36568b5a70d17d823/href</a></iframe><p>Now time to add all the UI elements, add the following code inside the <strong><em>Body </em></strong>variable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a6c257311df15e4bdafdb2d604d213a/href">https://medium.com/media/4a6c257311df15e4bdafdb2d604d213a/href</a></iframe><p>Little explanation here:</p><p>We just implemented a User interface with a switcher to change from login to sign up, this way we will implement the <em>signup</em> function when the user is on the “create account” and call the <em>signin</em> function when the user switch to “Log In”. Run your App in the canvas and you should have this:</p><figure><img alt="Screenshot from Xcode with our App canvas running presenting a login page." src="https://cdn-images-1.medium.com/max/1024/1*HShaKjRhUod7F0yX8x3DzQ.png" /><figcaption>Our App running in the canvas</figcaption></figure><h4>Let’s implement the signup and login functions</h4><p>It’s now time to grab the Firebase Auth APIs to sign up or login our user.</p><p>Implement the two following function after the <strong><em>Body </em></strong>variable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd790e11933fc9ab32c0a522a39693c3/href">https://medium.com/media/dd790e11933fc9ab32c0a522a39693c3/href</a></iframe><p>The 2 functions are taken directly from the Firebase documentation, one is for registering the user, the other to sign in, additionally, we are printing the errors in our console if there are any.</p><p>We can now call these 2 functions inside our <em>Button action</em>:</p><p><strong><em>Button(action: { [add the code below here] },</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec7525f445f31a6ef81fa47fb2c88c92/href">https://medium.com/media/ec7525f445f31a6ef81fa47fb2c88c92/href</a></iframe><h4>Run your App</h4><p>It’s now time to run our App, go to the <strong><em>FirebaseTestApp</em></strong> file and replace <strong><em>ContentView()</em></strong> to <strong><em>LoginView()</em></strong> since we need to run our program in the simulator.</p><p>Now, navigate to the sign up section and enter an email with a password of at least 6 characters, see what’s happening in the <strong><em>Firebase Console</em></strong>:</p><figure><img alt="Screenshot of Xcode running and Firebase Auth console showing a sign up" src="https://cdn-images-1.medium.com/max/1024/1*nMzJ_iLPglUXpf9zGddlUg.png" /><figcaption>Our final result</figcaption></figure><p>Et voilà! We have sign up our user, you can now navigate to the login section and sign-in with the credentials we just used, if you have done a mistake writing the email or the password, Firebase will return an error in the console.</p><h4>Where to go from here?</h4><p>Now that we have added sign up and login, the next step is to check if our user is currently logged in, if yes, show the <strong><em>ContentView</em></strong> displaying their emails and a logout button, otherwise, present the <strong><em>LoginView</em></strong>. This is possible thanks to the <strong><em>Firebase Auth Listener</em></strong> and will be part of an upcoming tutorial. Stay tuned!</p><blockquote><strong><em>Thanks for reading! I’m always happy to have a chat and collaborate at </em></strong><a href="/service/mailto:hello@sullivandecarli.com"><strong><em>hello@sullivandecarli.com</em></strong></a><strong><em>. Consider subscribing to enjoy unlimited access to my articles and all of Medium </em></strong><a href="https://sullivan-decarli.medium.com/membership"><strong><em>through my referral link</em></strong></a></blockquote><h4>I leave the full code here, in case you missed something:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f44df433127526854e788187c4e16e86/href">https://medium.com/media/f44df433127526854e788187c4e16e86/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e69fcea92849" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/swiftui-email-password-signup-login-firebase-e69fcea92849">SwiftUI | Email/Password Signup Using Firebase</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to add Firebase to a SwiftUI Project with SPM]]></title>
            <link>https://medium.com/swift-productions/how-to-add-firebase-with-spm-swiftui-d083189d510d?source=rss----acb6f01f2100---4</link>
            <guid isPermaLink="false">https://medium.com/p/d083189d510d</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[swift-package-manager]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Sullivan De Carli]]></dc:creator>
            <pubDate>Tue, 07 Dec 2021 16:58:42 GMT</pubDate>
            <atom:updated>2023-09-28T16:52:27.847Z</atom:updated>
            <content:encoded><![CDATA[<h4>Short tutorial on how to integrate the Firebase SDK with the new SwiftUI lifecycle using Swift Package Manager</h4><figure><img alt="SwiftUI with Xcode and Firebase logo" src="https://cdn-images-1.medium.com/max/1024/1*KR-J_3nw22wXORw48nIT1Q.png" /><figcaption>Firebase + SwiftUI + Xcode and SPM</figcaption></figure><h4>Create a new project</h4><p>Go to Xcode &gt; Create a new project &gt; SwiftUI interface &amp; lifecycle &gt; Name it <strong><em>PMStart</em></strong></p><figure><img alt="Screenshot of a new project in Xcode" src="https://cdn-images-1.medium.com/max/1024/1*C3g2-KP-KVNkVTBF40jCYQ.png" /><figcaption>Xcode &gt; New project</figcaption></figure><h4>Add a project on Firebase</h4><p>Head to <a href="https://firebase.google.com/">Firebase.google.com </a>&gt; Go to console &gt; Add a new project</p><figure><img alt="Screenshot from a new project in Firebase" src="https://cdn-images-1.medium.com/max/1024/1*EnZ8d0yYaUMZ6jA5eU7Vkw.png" /><figcaption>Create a new Firebase project</figcaption></figure><h4>Add the iOS App to the Firebase project</h4><p>First, go to the main menu &gt; + Add App &gt; Select iOS platform &gt; Add Firebase to your Apple app.</p><p>Copy the bundle ID from Xcode main target and paste it in the field, then click on register app.</p><figure><img alt="Firebase, add a new project" src="https://cdn-images-1.medium.com/max/1024/1*OnO5y2_ovKMO3J4zcuRsoA.png" /></figure><h4>Add the config file</h4><p>Download the <strong><em>GoogleService-info.plist</em></strong> file and drag this file inside your project folder in <strong><em>Xcode</em></strong>, it is important to add this file to the project, otherwise the app will crash with Firebase inizialized.</p><figure><img alt="Firebase, tutorial on how to add the config file." src="https://cdn-images-1.medium.com/max/1024/1*reD95ESiZJshW2DGaYVzNQ.png" /></figure><h4>Add Firebase with Swift Package Manager</h4><p>In Xcode, from the navigation at the top, go to <strong><em>File &gt; Add packages &gt; enter the URL below &gt; Select the packages ☑️&gt; Add Package</em></strong></p><pre><a href="https://github.com/firebase/firebase-ios-sdk.git">https://github.com/firebase/firebase-ios-sdk.git</a></pre><figure><img alt="Xcode, adding Firebase SDK with the Swift Package Manager" src="https://cdn-images-1.medium.com/max/1024/1*1ZibrudBs1xyzenwoNCHnA.png" /></figure><p>I recommend you to add at least<em> Firebase Analytics</em>, <em>FirebaseFirestore</em> and <em>FirebaseAuth</em> and <em>FirebaseStorage</em>. Once it is done, you can click on <strong><em>Next</em></strong> on the screen below:</p><figure><img alt="Firebase, adding the Firebase SDK with Swift Package Manager" src="https://cdn-images-1.medium.com/max/1024/1*pfkhUi_J3dPArD6FqY4aPA.png" /></figure><h4>Add initialization code</h4><p>Since we have created a SwiftUI, the code is a bit different from what is presented in the screen below.</p><figure><img alt="Firebase, adding the initialization code" src="https://cdn-images-1.medium.com/max/1024/1*CKOsbDsqh5NRPJhxyQQoHg.png" /></figure><p>You need to go to PMStartApp file which is the starter file for your app and add the following lines of codes:</p><pre><strong>import</strong> Firebase</pre><p>And this code over the body variable:</p><pre><strong>init</strong>() {</pre><pre>FirebaseApp.configure()</pre><pre>}</pre><h4>Run the App</h4><p>You can now run your app, it is successfully connected to the Firebase backend!</p><figure><img alt="Screenshot from Xcode with simulator running" src="https://cdn-images-1.medium.com/max/1024/1*H-3lPzKHq-bIgRt0oCoKdA.png" /><figcaption>Simulator running with Firebase installed</figcaption></figure><blockquote><strong><em>Would you like to kick start your Firebase project? Consider </em></strong><a href="https://link.springer.com/book/10.1007/978-1-4842-9452-9"><strong><em>buying my book</em></strong></a><strong><em> with tutorials on how to create iOS apps in SwiftUI with the Firebase backend.</em></strong></blockquote><blockquote><strong><em>You can also subscribe to enjoy unlimited access to my articles and all of Medium </em></strong><a href="https://sullivan-decarli.medium.com/membership"><strong><em>through my referral link</em></strong></a><strong><em>.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d083189d510d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-productions/how-to-add-firebase-with-spm-swiftui-d083189d510d">How to add Firebase to a SwiftUI Project with SPM</a> was originally published in <a href="https://medium.com/swift-productions">Swift Productions</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>