In the AdWhirl SDK—a multi-ad network assemblage—the framework is structured so that, if you don’t additionally import an accompanying library from, say, Google AdSense, or from AdMob, it will simply ignore those code branches when compiling and running the code. That is, there is code in the framework that requires the AdSense SDK to run, but if you don’t include AdSense it just ignores that code, no compiler errors or anything. There is no user-required commenting out of code or anything like that.
I’m trying to build a framework with a similar requirement and I cannot for the life of me figure out how they got that to work. The default, obviously, is that you’ll get linker errors in your code at compile time because of the missing libraries. How do I export a framework (currently as a .a file) that has this functionality?
When you do something like:
What you’re actually doing is sending the
allocmessage to theClassNamemetaclass and theninitto the instance ofClassNamethat the metaclass attempts to return (or to no-one if the metaclass returnednilfor want of resources).Additionally, the Objective-C runtime is reflective. Amongst other things, that means you can query it to find metaclasses from the string name of the classes. E.g.
Which returns either the named metaclass or
nilif that class doesn’t exist. Putting that together:Will give you an instance of the class if such a class is known to the app and suitable resources are available. In the case of optional link libraries, this is therefore a way to try to get an instance of a class but to fail safely if the library containing the class wasn’t linked.