Is there a way to add `if (iOSApplicationExtension)` condition for `UIApplication.shared`

Viewed 1431

I'm trying to find a way of adding a condition for checking is an extension target in Framework (not main app). Is it possible relying on #available(iOSApplicationExtension, *) with some parameters adjustments?

@objc public extension UIView {
    var isAccessibilityCategory: Bool {
        if #available(iOSApplicationExtension, *) {
            return self.traitCollection.preferredContentSizeCategory.isAccessibilityCategory
        } else {
            return UIApplication.shared.preferredContentSizeCategory.isAccessibilityCategory
        }
    }
}

How to detect if code is running in Main App or App Extension Target? - doesn't work, because the framework doesn't know about extension targets

1 Answers

To solve this problem I created an enumerator with all the app that I created, when the user starts the app I check with the bundle identifier in which app I'm and I save it in the appManager, so after I can perform some changes base the app type also in the framework.

This is my Enum:

public enum MyApp: Int, CaseIterable {
    case sysReserved = 0
    case app1 = 1

   public var applicationId : [String] {
       switch self {
           case .sysReserved:
               return [""]
           case .parametersEditor:
               return ["com.team.app1"]
       }
   }
}

This my AppManager

class AppManager: UIApplication {

   var CURRENT_APP: MyApp = .sysReserved

   override init() {
      super.init()
      getCurrentApplicationInfo()
   }

   internal func getCurrentApplicationInfo() {
       CURRENT_VERSION_NUM = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
       CURRENT_BUILD_NUM = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String
       let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleIdentifier") as! String
       CURRENT_APP = .sysReserved

       for app in Application.allCases {
           if app.applicationId.contains(appName) {
               CURRENT_APP = app
           }
       }
   }
}
Related