Routing in SwiftUI

Purposes

  • Separate the navigation logic from the view layer. It’ll allow you to change navigation without modifying views.
  • Simplify the implementation of tasks, like deep linking, by making navigation more flexible

SwiftUI-Specific Information

The declarative nature of SwiftUI makes it challenging to separate the navigation from the view layer. Push navigation requires inserting a NavigationLink into a view. And modal presentation needs the .sheet modifier to be added somewhere in the view code. You must provide a destination view for both the NavigationLink and .sheet. Also, in general, you’ll use the @state property to trigger navigation.

This is different from UIKit. For example, to present a view modally in UIKit, it’s enough to call presenton the current view controller and pass a destination view controller to display. That can be easily done from outside of a presenting view controller.

Solutions

We’ll consider two somewhat different solutions to separate the navigation from the view layer. Both use the native SwiftUI navigation methods NavigationLink and .sheet modifier. But providing a destination view and navigation state will be moved to a router.

  1. Router with trigger views: A router will return trigger subviews for all possible navigation routes to insert them into a presenting view. Such a subview code snippet will contain a NavigationLink or .sheet modifier inside, as well as a destination view specified, and will use a @state property, stored in the router, via binding. This way, the presenting view won’t depend on the navigation code and destination, only on a router protocol.
  2. Router with type-erased modifiers: A presenting view will be configured with general modifiers for presenting any other views. Being initialized with a router, those modifiers will track the navigation state stored in the router via bindings and will perform navigation when the router changes that state. The router also will have functions for all possible navigation. Those functions will change the state and trigger navigation as a result.

Example of usage

To make it easier to understand, let’s consider both solutions in the context of a simple example.

The project will consist of two simple views: a presenting view with a single button and a presented view that should be navigated to when the user taps the button. The presented view will display a text passed from the presenting view. Tapping on that text will dismiss the presented view.

1. Router With Trigger Views

Presenting view

The presenting view is initialized with a router that knows how to navigate to the presented view. The router provides a trigger view that, being inserted, can perform the desired navigation in response to user interactions.

Using PresentingRouterProtocol instead of the actual router class allows us to change the navigation logic easily and to initialize the PresentingView with a mock router for tests.

We should wrap the router property with @StateObject (available from iOS 14). This is because the navigation state resides in a router, and the view should update on the state changes. Also, in contrast to @ObservedObject,this wrapperavoids recreating a router every time the owner view is updated. It’s crucial for our implementation of push navigation based on NavigationLink.

#xcode #programming #swift #mobile #swiftui

What is GEEK

Buddha Community

Routing in SwiftUI
Robert Wilson

Robert Wilson

1618289269

How SpotnRides Act as the Best Route Planning App Solution in 2021?

Are you looking for the best route planning app solution provider to enhance the fleet management service? Take a look at the SpotnRides route planner app which has sparkling key-features that sharp your logistics service smartly.

#route planning app #route planner app development #build a route planning software #route optimization software 2021

SwiftUI Scratch Card Effect - Custom Masking - Animation's -View Builder-SwiftUI Tutorials

Hello Guys 🖐🖐🖐🖐
In this Video I’m going to show how to create a Stylish Scratch Card Animation Effect With Custom Masking in SwiftUI | Scratch to reveal content SwiftUI | SwiftUI Custom View Masking | SwiftUI Custom Animation’s | SwiftUI View Builder’s | SwiftUI Gesture’s | Xcode 12 SwiftUI.

► Source Code: https://www.patreon.com/posts/early-access-52075157

► Support Us
Patreon : https://www.patreon.com/kavsoft
Contributions : https://donorbox.org/kavsoft
Or By Visiting the Link Given Below:

► Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. It’s gives a great experience and I think you should give it a try too https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=kavsoft&utm_content=description-only

► My MacBook Specs
M1 MacBook Pro(16GB)
Xcode Version: 12.5
macOS Version: 11.4 Big Sur

► Official Website: https://kavsoft.dev
For Any Queries: https://kavsoft.dev/#contact

► Social Platforms
Instagram: https://www.instagram.com/_kavsoft/
Twitter: https://twitter.com/_Kavsoft

► Timestamps
0:00 Intro
0:26 Building Home View
1:56 Building Scratch Card View(View Builder)

Thanks for watching
Make sure to like and Subscribe For More Content !!!

#swiftui #animation's #swiftui

SwiftUI Cloud App UI - Adaptable For Both iOS & macOS - Complex UI - SwiftUI Tutorials

Hello Guys 🖐🖐🖐🖐
In this Video I’m going to show how to create a adaptable Cloud App UI for Both iOS & macOS Using SwiftUI | SwiftUI Cloud App UI | SwiftUI macOS App Development | SwiftUI Mac Catalyst Apps | SwiftUI Complex UI | SwiftUI Mac App | SwiftUI Custom Side Bar Menu | SwiftUI Hamburger Menu | SwiftUI Slide Out Menu | Xcode SwiftUI.

► Source Code: https://www.patreon.com/posts/early-access-app-52186632

► Support Us
Patreon : https://www.patreon.com/kavsoft
Contributions : https://donorbox.org/kavsoft
Or By Visiting the Link Given Below:

► Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. It’s gives a great experience and I think you should give it a try too https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=kavsoft&utm_content=description-only

► My MacBook Specs
M1 MacBook Pro(16GB)
Xcode Version: 12.5
macOS Version: 11.4 Big Sur

► Official Website: https://kavsoft.dev
For Any Queries: https://kavsoft.dev/#contact

► Social Platforms
Instagram: https://www.instagram.com/_kavsoft/
Twitter: https://twitter.com/_Kavsoft

► Timestamps
0:00 Intro
0:42 Building Side Bar Menu
7:54 Building Main Content View
20:32 Building Side View
23:37 Adapting App For iOS

Thanks for watching
Make sure to like and Subscribe For More Content !!!

#swiftui #complex ui #swiftui #ios & macos

What's New in SwiftUI for iOS 15 - Part 2 - WWDC 2021 - Xcode 13 - SwiftUI 3.0

Hello Guys 🖐🖐🖐🖐
In this Video I’m going to show what are the new features introduced with Xcode 13 at WWDC 2021 | WWDC 2021 | SwiftUI 3.0 | What’s New in SwiftUI | What’s New in Xcode 13 | iOS 15 | SwiftUI Keyboard Accessory View | SwiftUI Keyboard Begin/Resign First Responder | | Xcode 13 beta SwiftUI.
#wwdc #swiftui #ios #xcode #xcode13

► Subscribe For More: https://www.youtube.com/Kavsoft?sub_confirmation=1

► SwiftUI 3.0 Tutorials: https://youtube.com/playlist?list=PLimqJDzPI-H-NE08JyPV0l76W70UuBVfd

► SwiftUI 2.0 Tutorials: https://youtube.com/playlist?list=PLimqJDzPI-H81yR_E21IvWmvZCy91PZTn

► Support Us
Patreon : https://www.patreon.com/kavsoft
Contributions : https://donorbox.org/kavsoft
Or By Visiting the Link Given Below:

► Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. It’s gives a great experience and I think you should give it a try too https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=kavsoft&utm_content=description-only

► My MacBook Specs
M1 MacBook Pro(16GB)
Xcode Version: 13 Beta
macOS Version: 11.4 Big Sur

► Official Website: https://kavsoft.dev
For Any Queries: https://kavsoft.dev/#contact

► Social Platforms
Instagram: https://www.instagram.com/_kavsoft/
Twitter: https://twitter.com/_Kavsoft

► Timestamps
0:00 Keyboard Accessory View
2:17 Focused (Keyboard Resign/Begin First Responder)
3:28 Access to Bindings within the Collection/Set
4:58 New Foreground Styles
6:10 Custom Rendering And Colouring For System Images
7:24 New Text Customisation’s
8:47 Timeline View
9:58 New Button Customisation’s

Thanks for watching
Make sure to like and Subscribe For More Content !!!

#swiftui #wwdc #xcode #swiftui

SwiftUI 2.0 URL Session Download Task With Document Interaction Controller - Tutorials

In this Video i’m going to show how to download files from website using URL Session Download Task And Previewing the files using UIDocument Interaction Controller Using SwiftUI 2.0 | SwiftUI 2.0 URL Session Download Task | SwiftUI 2.0 Download Task | SwiftUI 2.0 Downloading Files | SwiftUI 2.0 Saving Files Using FileManager | SwiftUI Custom Progress View | SwiftUI Opening Documents Using UIDocument Interaction Controller | Xcode 12 SwiftUI.

Source Code 👇
https://kavsoft.dev/SwiftUI_2.0/Download_Task

Support Us By Patreon : https://www.patreon.com/kavsoft
Support Us By Contributions : https://donorbox.org/kavsoft

Support Us By Visiting the Link Given Below.

⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. It’s gives a great experience and I think you should give it a try too https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=kavsoft&utm_content=description-only

My Xcode Version is 12.4
My macOS Version is 11.2.3 Big Sur

For Any Queries And Any Request For Videos Use The Given Link

https://kavsoft.dev/#contact

For More

https://kavsoft.dev

Instagram

https://www.instagram.com/_kavsoft/

Twitter

https://twitter.com/_Kavsoft

#swiftui #swiftui 2.0 url #swiftui tutorials