How to create a Library/Framework that will be linked and used only if present at compile time

Ian

I'm interested in replicating the sort of functionality that you see with Apple frameworks: where Apps can take advantage of new frameworks on more recent versions of iOS but don't fail when they're missing.

Of course if there's another approach to solving the problem I'd love to see it.


The position I'm in is as follows:

Company-A provides an iOS framework (hereafter named FrameA) to third parties that they can link into their Apps.

I am tasked with creating an optional module that can be "bolted on" to FrameA, but isn't actually a part of it (this is due to FrameA often being updated and customized for different customers and other business limitations).

The third parties can choose to use the bolt-on library (or framework, or whatever will work) or not. If it is present FrameA must link it and the code execute it's functionality. If it is not present then FrameA continues as it does now. It must work for third parties that receive new versions of FrameA but not the bolt-on library. I should make as few changes to FrameA as possible since those changes will have to be maintained going forwards by another team.

I also need to do this within Apples submission rules, the solution must support at least iOS6, 7 and 8. So Dynamic linking is not allowed. Company-A provides FrameA directly to the third-parties, so the only part that Apple has to approve is the final App.

Jeffery Thomas

I've done this kind of thing on a small scale, and it's not too bad.

From within FrameA, use a factory to instantiate all bolt-on objects. The factory uses NSClassFromString() to dynamically fetch the class. Avoid class methods: this makes things harder. Whenever possible, use protocols instead of classes: this makes things easier.

- (id<FABoltOnProtocol>)createBoltOnClass
{
    return [[NSClassFromString(@"BOBoltOnClass") alloc] init];
}

This code will run even if BOBoltOnClass is not linked into the final app. NSClassFromString(@"BOBoltOnClass") will return Nil, [Nil alloc] returns nil, and [nil init] returns nil.

You can test for bolt-on's existence the same way.

BOOL MyHasBoltOn
{
    return NSClassFromString(@"BOBoltOnClass") != Nil;
}

The drawback is FrameA must be compiled with the headers from bolt-on. This is why I recommended protocols. These protocols can be owned by FrameA and bolt-on will conform to it (NOTE: I hinted at this by naming the protocol with FrameA's prefix FA and the class with bolt-on's prefix BO).

Finally, being a string, you can read @"BOBoltOnClass" from a config file so you can swap different bolt-ons as needed.

Be aware that the build flag -ObjC must be set, as otherwise the compiler will not load symbols for the bolt-on classes as they're not called conventionally.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a library which uses mutexes only if pthread is linked?

From Dev

Check if reference is present at compile time

From Dev

Add a compile time only dependency in sbt

From Dev

How to read attribute only if it is present?

From Dev

Compile time only dependency and ProGuard using Gradle

From Dev

How to time condition at compile time

From Dev

Initialize linked list or work queue at compile time VS runtime

From Dev

How does Rust achieve compile-time-only pointer safety?

From Dev

Conditionally compile only one module at a time

From Dev

How to create a static string at compile time

From Dev

detect if long long is present at compile time

From Dev

Does Delphi require dfm at compile time when dcu is present?

From Dev

How to create a memory pool with arbitrary number of (compile time determined) containers?

From Dev

Gradle dependency for compile time only and test

From Dev

Serialize only compile-time information

From Dev

How to create a library which uses mutexes only if pthread is linked?

From Dev

Android Studio compile linked libraries only when changed

From Dev

How to read attribute only if it is present?

From Dev

Trying to create a linked list for the first time

From Dev

Linked list compile time error

From Dev

Initialize linked list or work queue at compile time VS runtime

From Dev

Is the "present()" intrinsic evaluated at compile time

From Dev

Compile Time: How can I check if a datatype (and or value) is being used within a method

From Dev

Determine at compile time if this is read only

From Dev

Create linked time input - HTML/PHP/JAVASCRIPT

From Dev

Create a generic sortableTable object to be used to alphabetically sort a table elements each time a column is clicked using only JavaScript

From Dev

how to compile text present in a cell ? (excel)

From Dev

Disable function only for compile-time argument

From Dev

How can I check if the data is present in firebase database only one time without event listeners

Related Related

  1. 1

    How to create a library which uses mutexes only if pthread is linked?

  2. 2

    Check if reference is present at compile time

  3. 3

    Add a compile time only dependency in sbt

  4. 4

    How to read attribute only if it is present?

  5. 5

    Compile time only dependency and ProGuard using Gradle

  6. 6

    How to time condition at compile time

  7. 7

    Initialize linked list or work queue at compile time VS runtime

  8. 8

    How does Rust achieve compile-time-only pointer safety?

  9. 9

    Conditionally compile only one module at a time

  10. 10

    How to create a static string at compile time

  11. 11

    detect if long long is present at compile time

  12. 12

    Does Delphi require dfm at compile time when dcu is present?

  13. 13

    How to create a memory pool with arbitrary number of (compile time determined) containers?

  14. 14

    Gradle dependency for compile time only and test

  15. 15

    Serialize only compile-time information

  16. 16

    How to create a library which uses mutexes only if pthread is linked?

  17. 17

    Android Studio compile linked libraries only when changed

  18. 18

    How to read attribute only if it is present?

  19. 19

    Trying to create a linked list for the first time

  20. 20

    Linked list compile time error

  21. 21

    Initialize linked list or work queue at compile time VS runtime

  22. 22

    Is the "present()" intrinsic evaluated at compile time

  23. 23

    Compile Time: How can I check if a datatype (and or value) is being used within a method

  24. 24

    Determine at compile time if this is read only

  25. 25

    Create linked time input - HTML/PHP/JAVASCRIPT

  26. 26

    Create a generic sortableTable object to be used to alphabetically sort a table elements each time a column is clicked using only JavaScript

  27. 27

    how to compile text present in a cell ? (excel)

  28. 28

    Disable function only for compile-time argument

  29. 29

    How can I check if the data is present in firebase database only one time without event listeners

HotTag

Archive