1

I'm building a static library to distribute to other folks to use in iOS apps, wherein I'd like to take advantage of of frameworks only if they have been included in the app by the users of my library. I've figured out how to compile the library so that it does not itself include any frameworks, but as soon as I try to use it in an app, it fails, because the library references frameworks that don't exist.

I'd rather not force my clients to load frameworks they don't need. Weak-linking frameworks is cool, but that just means that the framework doesn't have to be present on the system (e.g., for older versions of iOS); its support is still compiled into the binary. Better would be not to require the framework to be linked at all, and only use it if it is linked (optionally or not).

So, is there any way to detect that a framework is included in an iOS app at runtime, not just whether it's present on the system?

Community
  • 1
  • 1
theory
  • 9,178
  • 10
  • 59
  • 129
  • 1
    `NSClassFromString(@"FrameworkClass") == nil` – Bryan Chen Mar 28 '14 at 04:28
  • No, that just tells me if the framework is present on the system, not whether the app included it. – theory Mar 28 '14 at 04:36
  • what are the differences? – Bryan Chen Mar 28 '14 at 04:38
  • In your version, you test if the class in question is present on the device. What I want to do is detect whether the user of my app has required the framework that includes that class. – theory Mar 28 '14 at 04:43
  • 1
    so you want to know does the app developer include a framework explicitly? not sure how useful is that... you can ask them to `#define USE_SOME_FRAMEWORK` – Bryan Chen Mar 28 '14 at 04:47
  • Yeah, I can add methods to the interface to turn things on and off, but would rather save my clients the additional complexity. I guess my real question is, what does it mean to say that a framework is linked? If a framework is not linked, are the classes not available? – theory Mar 28 '14 at 17:35
  • @BryanChen did some testing with `NSClassFromString()`, turns out you're exactly right, it's just what I needed! Happy to accept your answer if you add one. – theory Mar 28 '14 at 20:49

1 Answers1

0

you can use

if (NSClassFromString(@"FrameworkClass") == nil) {
   // the framework is not available
} else {
   // the framework is avaiable
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • Works great, as long as you're careful to instantiate objects with the class returned by `NSClassFromString()`. – theory Mar 29 '14 at 04:53
  • @theory & Bryan-Chen: could you provide insight how you achieved this? I'm creating a lib too that would provide additional funcs if Firebase is linked with the host project. But this is the error I get http://imgur.com/a/bRgQ3 – batman Nov 22 '16 at 08:14
  • @akdsouza You either have to include the relevant headers and then test to see if the class is loaded at runtime via `NSClassFromString()` or make `baseRef` have the type `id`. – theory Nov 22 '16 at 21:30
  • 1
    @theory I ended up doing just that and using reflection; else the compiler would throw errors. – batman Nov 23 '16 at 04:25