Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use SwiftUI Interface with UIKit Life Cycle?

Tags:

swift

swiftui

I like to know why we should create and use a SwiftUI Interface with UIKit Life Cycle? I get confused about it! Because:

1-if you want UIKit programming then go and choose Storyboard for Interface and then choose UIKit for Life Cycle.

2-if you want SwiftUI programming then go and choose SwiftUI for Interface and then choose SwiftUI for Life Cycle.

3-if you want change your Swift Language then go change it to Objective-C and you will get Storyboard as Interface and UIKit as Life Cycle.

Here comes Option 4:

4- You can have SwiftUI for Interface and UIKit as life Cycle! which is the Topic of this Question! If I am not wrong SwiftUI handle things different than UIKit, we do not need define delegate or protocols in SwiftUI or things like that, but why we get AppDelegate and SceneDelegate in this case! what is pros and cons in case we create such a Xcode project?

like image 583
ios coder Avatar asked Nov 05 '20 20:11

ios coder


People also ask

Should I use SwiftUI or UIKit?

SwiftUI is limited and works better with iOS 14 and iOS 15. On the contrary, UIKit enables you to support even iOS version 9.0. This makes UIKit more flexible. It surely wins against SwiftUI when it comes to backward compatibility.

Can you use SwiftUI and UIKit in the same project?

The hosting controller is compatible with UIKit since it is a subclass of UIViewController . The purpose of the UIHostingController is to enclose a SwiftUI view so that it can be integrated into an existing UIKit based project.

What is SwiftUI app life cycle?

It's an iOS app's “main” function, the first one called during the lifetime of the app. You typically use it for: Initial configuration of 3rd-party components and frameworks, such as setting up Firebase, Bugsnag, Realm, OneSignal, Parse Server, Reachability services, and so on.

Is SwiftUI backed by UIKit?

Does SwiftUI replace UIKit? No. Many parts of SwiftUI directly build on top of existing UIKit components, such as UITableView .


1 Answers

With SwiftUI you should in most cases use SwiftUI lifecycle.

If you need some AppDelegate methods you can use @UIApplicationDelegateAdaptor:

  • SwiftUI app life cycle iOS14 where to put AppDelegate code?

There are some cases, however, when you need UIKit lifecycle:

  • access to SceneDelegate (see Is there any way to call SceneDelegate methods in iOS 14 app life cycle?)
  • deployment target iOS 13 (SwiftUI lifecycle is available on iOS 14+).
like image 56
pawello2222 Avatar answered Oct 26 '22 15:10

pawello2222