14

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

4
  • The #available statement cannot be used for detecting target type like application extension. That can only detect what operating system and what version of that OS the app is run on. #available was created so that you can use system capabilities that only became available in newer OS versions than your minimum target dependency - such as using Combine or SwiftUI in a project targeting iOS13 <. It cannot be used for running custom logic for app extensions rather than in the app target itself. Commented May 13, 2020 at 11:09
  • Also, you should not be asking the exact same question after your original has been closed as a duplicate. If you don't agree with the duplicate closure, edit your question to explain why the answers for the duplicate don't apply to your problem and tag the closer in comments so that they can reopen your question if they agree. How come this particular answer doesn't apply to your question? It requires no knowledge from a framework about the app/extension that runs it. Commented May 13, 2020 at 11:13
  • 2
    @DávidPásztor I need a condition that handles UIApplication.shared correctly at compile-time, not runtime Commented May 13, 2020 at 11:19
  • 1
    Did you figure out a way?
    – hyouuu
    Commented Jan 13, 2022 at 21:08

1 Answer 1

-2

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
           }
       }
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.