How to incorporate Framework SwiftUI

前端 未结 3 1740
無奈伤痛
無奈伤痛 2021-01-02 21:16

We can use Framework SwiftUI only from iOS 13.0+. So how to incorporate this framework from the deployment Target 10.0 or at least 12.0.

相关标签:
3条回答
  • 2021-01-02 21:48

    There is no reasonable way to do that. #available or @available allow you to differentiate some pieces of code or whole classes but not app itself.

    You can use @available to silence warnings of SwiftUI structure or classes with 12.0 Deployment target but in this case you need to completely duplicate UI from storyboards(.xib, whatever) using Swift UI. Moreover, completely different "binding" approach will also require you to reimplement existing logic across that app(doesn't matter what kind of architecture you have used before). Any VIPER, VIP, MVC is aimed to send some data change notifications to a ViewController(View)? in Swift UI you need to use Bindable objects. It also causes you to create duplicates of ViewModels(if you have such) because they will be different.

    Result? You have second implementation of UI, second implementation of view models, additional 80% of implementation of presenter(or what do you use). Only database and rest managers will be reused. And this is valid if you have very good architecture. Don't forget that you need to support two branches of UI and logic.

    There is two ways: redevelop almost full application to support SwiftUI and UIKit or, if you don't want to have only one iOS 13 supported, wait until iOS 14 will be released and then support both of them with SwiftUI

    0 讨论(0)
  • 2021-01-02 21:53

    Build Phases → Link Binary With Libraries → Add SwiftUI.framework (Status: Optional)

    import SwiftUI
    
    @available(iOS 13.0, *)
    struct SwiftUIView : View {
        var body: some View {
            Text("Hello World!")
        }
    }
    
    @available(iOS 13.0, *)
    struct SwiftUIView_Previews : PreviewProvider {
        static var previews: some View {
            SwiftUIView()
        }
    }
    

    etc.

    SwiftUI preview for this target doesnʼt work, but you can add a special target “SwiftUI Preview”.

    0 讨论(0)
  • 2021-01-02 21:56

    Although @DenFav is absolutely right - supporting deployment target below iOS 13 with SwiftUI is a pain, but it is possible.

    Steps:

    1. Link the framework weakly (I used this answer):

      Adding -weak_framework SwiftUI to Other Linker Flags fixed my issue

    2. Wrap all SwiftUI calls with canImport (see answer):

      #if canImport(SwiftUI) && canImport(Combine)

    This will allow you to build and archive with deployment target < iOS 13.

    Optional:

    Now the question is: how to deal with viewModels. I solved this with my own implementation. You can check the solution in the public repo of Ruuvi Station. Note: the code is complex (VIPER), that's why I'll shortly describe the main ideas.

    The viewModel implementation is in Classes/Presentation/Binding.

    I'm using these viewModels, wrapping them with ObservableObject for SwiftUI.

    You can still observe changes made in SwiftUI code.

    The result is: iOS 13 uses SwiftUI code for Presentation Layer, while iOS 12 and lower is using traditional UIKit code.

    The viewController is responsible for determining if SwiftUI code can be used.

    0 讨论(0)
提交回复
热议问题