2

Any idea how I could import SwiftUICharts framework only if the iOS version is over iOS 13? I added this framework via File > Swift Package > add package dependency. My app target has to be iOS 10.

I import this framework into a swiftui controller that will only show if the target is over iOS 14, so it's ok to use it there. But I don't know how. A simple @available doesn't work in this case.

enter image description here

EDIT. Here is my code:

AnalyticsNew controller:

 #if !arch(arm)
 import SwiftUI


 @available(iOS 14, *)
 struct AnalyticsNew: View {
     var body: some View {
         Home()
     }
 }

 @available(iOS 14, *)
 struct AnalyticsNew_Previews: PreviewProvider {
     static var previews: some View {
         AnalyticsNew()
     }
 }

 @available(iOS 14, *)
 struct Home : View {
 //... some code
 }

 #endif

AnalyticsView controller that shoes the right controller depending on the iOS version:

#if canImport(SwiftUI)
import SwiftUI
#endif
import UIKit

class AnalyticsView: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let child = createViewController()
        addChild(child)

        child.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        child.view.frame = view.bounds
        view.addSubview(child.view)

        child.didMove(toParent: self)
    }

    func createViewController() -> UIViewController {
    
        if #available(iOS 14, *) {
            #if !arch(arm) //compatible SwiftUI IOS <10
            return UIHostingController(rootView: AnalyticsNew())
            #else
            return AnalyticsOld()
            #endif
        } else {
            return AnalyticsOld()
        }
    }
}
Abv
  • 354
  • 2
  • 13
  • Looking at this, check out [Availability checking in Swift: backwards compatibility the smart way](https://www.hackingwithswift.com/new-syntax-swift-2-availability-checking). – Yrb Apr 29 '21 at 02:03
  • I think you should create a plug-in (bundle) with code dependent on SwiftUIChards, set its deployment target also to 10.13, and load it conditionally (depending on availability, say in App init) in main application to have access to types(views) inside. – Asperi Apr 29 '21 at 04:37
  • @Asperti Thanks for your answer, how would you do that? – Abv Apr 29 '21 at 08:33

0 Answers0