Search code examples
iosxcode

Don't we need to link framework to XCode project anymore?


Base on this question

Why don't iOS framework dependencies need to be explicitly linked to a static library

I read the selected answer and still don't understand so I made an example project

Test Project on Github

In the test project, I remove all framework from Link Binary With Libraries and File navigation for both main project and the static library (including Foundation.framework and UIKit.framework too), basically, both project link to 0 frameworks.

Questions are

  • In static library, it's including MapKit/MapKit.h without referencing the Mapkit.framework to the project, why is its still working?
  • In main project, I remove UIKit.framework and Foundation.framework from the project, why is it still working?
  • Since it's working for now, will there be any issue later?

Thank you for your comment.

P.S. By working, I mean I can run on the simulator and I can archive the main project without any error.

Edit 25/07/2014

I tried with the real app that I'm working on, it's the same.

  • I highlight Foundation, UIKit, CoreData and 10 another frameworks in File Navigation, well, all of them.
  • Uncheck the target in Utilities Panel --> Target Membership
  • Build : Pass, Run : Pass

Every functionality of my app is still working as expected. I don't get this.


Solution

  • Check your project build settings. Underneath LLVM 5.1 — Language — Modules you should see the option 'Link Frameworks Automatically'. In your case it sounds like it's set to 'YES', the default.

    In that case, instead of producing an error when you reference a class that the compiler doesn't know, it'll figure out which Framework contains that class and link it. In your code it'll be MKMapView or one of the other MapKit classes that triggers the linkage.

    EDIT: from the relevant 'What's New?' document:

    Auto Linking is enabled for frameworks imported by code modules. When a source file includes a header from a framework that supports modules, the compiler generates extra information in the object file to automatically link in that framework. The result is that, in most cases, you will not need to specify a separate list of the frameworks to link with your target when you use a framework API that supports modules.

    Another way of looking at it is that the compiler is smart enough to mutate #import to @import when the framework has been built appropriately. All system frameworks have been.