Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build an iOS framework with weak-linked CocoaPods libraries

I am trying to build an iOS Framework (Test.framework) using the new template offered by Xcode 6 for creating Cocoa Touch Frameworks. The framework has different dependencies (as AFNetworking or FacebookSDK) specified in a Podfile. I don't want dependencies to be included in the framework, I just want to link against them.

The problem is that when I build the framework, the libPods.a is linked and included. Q: How can I link against libPods.a library, but not include it in the framework?


More details:

I have read about weak linking: https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html But I don't have much experience playing with the project settings, I'm not a PRO.

I tried marking libPods.a as an "Optional" but nothing changed, dependencies were still included.

I tried removing the libPods.a from the section "Link Binary With Libraries" in Build Phases, but I got this error (after cleaning project folder and building again):

ld: library not found for -lPods-MyFramework-AFNetworking

I tried removing all the flags from "Other Linker Flags", but it gives me undefined symbols:

Undefined symbols for architecture armv7:
  "_OBJC_CLASS_$_FBSession", referenced from:
      objc-class-ref in TestClass.o
ld: symbol(s) not found for architecture armv7

Here is the source code for a clean project with libraries included: https://www.dropbox.com/sh/0ymuzw6kiagz02j/AABzyHiZVaQQvBEnjBgRBq3ua?dl=0

like image 540
Vladimir Avatar asked Nov 06 '14 23:11

Vladimir


People also ask

How do you make a CocoaPods framework?

Create a Cocoa Touch Framework Xcode project Under the hood, a CocoaPods library is a Cocoa Touch framework library. Launch Xcode and create a new project, choose Cocoa Touch Framework . Enter the name SwiftyLib , select the checkbox Include Unit Tests .

Are CocoaPods static or dynamic?

CocoaPods pod-linkage plugin. In SwiftKey, we have a dynamic framework that we use to share code between the app and the keyboard extension, and all the pods are linked statically except for some of them that are linked dynamically because they are linked to the app and keyboard extension too.


1 Answers

The post_install code below goes at the bottom of your Podfile. It allows you specify which targets you want and the frameworks that will be weak linked. We are able to utilize this to weaklink a Framework inside our Dynamic Framework Target but continue to have it linked correclty when compiling our core application.

targets_to_weaklink=['Target1']
frameworks_to_weaklink=['Framework1']
post_install do |installer|
  targets_to_weaklink.map!{|t| t="Pods-#{t}"}
  installer.pods_project.targets.each do |target|
    next unless targets_to_weaklink.include?(target.name)

    target.build_configurations.each do |config|
      base_config_reference = config.base_configuration_reference
      unless base_config_reference.nil?
        xcconfig_path = base_config_reference.real_path
        xcconfig = File.read(xcconfig_path)
        frameworks_to_weaklink.each do |framework|
          xcconfig = xcconfig.gsub(/-framework "#{framework}"/, "-weak_framework \"#{framework}\"")
        end
        File.open(xcconfig_path, "w") { |file| file << xcconfig }
      end
    end
  end
end
like image 57
jkanter Avatar answered Sep 25 '22 09:09

jkanter