Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode and optional frameworks

Tags:

xcode

macos

ios

Under Linked frameworks and libraries there is a Required or Optional option.

Could somebody explain a situation in which and how to work with an optional framework? I could see hypothetically a situation where I have some test data and IF the framework is included I would want to enable some sort of functionality and if it is not included than perhaps I wouldn't do something....

But otherwise I'm at a loss as to when you would want to use an optional framework

(a code example would be awesome if one exists)

like image 987
Jeef Avatar asked Oct 09 '15 12:10

Jeef


1 Answers

Optional linking is useful if you're targeting older OS versions where a certain framework might not be available yet. In this case you can set the linkage of the given framework to optional, and this causes the program not to crash on launch if dlopen cannot find the given framework.

Then in your code you can put guard statements around the usage of this framework in order to avoid crashing b/c of using unresolved symbols:

 if (MyWeakLinkedFunction != NULL)
 {
     result = MyWeakLinkedFunction(); // this function comes from a weakly/optionally linked framework
 }

See: Frameworks and Weak Linking

like image 125
Tamás Zahola Avatar answered Oct 09 '22 20:10

Tamás Zahola