Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional linking for Swift Combine.framework in Xcode 11

Our application supports iOS 11 and higher. In iOS 13 we use SwiftUI + Combine

we wrap import of SwiftUI or Combine framework with correspondent check #if canImport(SwiftUI) or #if canImport(Combine). If we run our app from Xcode 11 under iOS 12 we have error dyld: Library not loaded: /System/Library/Frameworks/Combine.framework/Combine

We fixed same issue for SwiftUI by linking it optionally.

enter image description here

But we can't make same for Combine as it can not be even selected for linking

enter image description here

like image 685
Igor Palaguta Avatar asked Jul 23 '19 16:07

Igor Palaguta


People also ask

Can we use combine in Swift?

The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers.

Does SwiftUI use combine?

If you've been trying out SwiftUI, you've likely been using Combine quite a lot already. Types like ObservableObject and Property Wrappers like @Published all use Combine under the hood. It's a powerful framework to dynamically respond to value changes over time.

Can I use combine with UIKit?

It's probably not a coincidence that Combine happened to be introduced at the exact same time as SwiftUI. While Combine can certainly also be used with both UIKit and AppKit, as well as within scripts and command line tools, its highly declarative design often makes it a perfect logic companion to SwiftUI-based views.

What is combine for?

Combine, announced at WWDC 2019, is Apple's new “reactive” framework for handling events over time. You can use Combine to unify and simplify your code for dealing with things like delegates, notifications, timers, completion blocks and callbacks.


3 Answers

You can add the linker flags explicitly to optionally link Combine when it is available in the build settings. In the Xcode Build Settings add -weak_framework Combine to Other Linker Flags.

build settings Ohter Linker Flags "-weak_framework Combine"

Or add the following line in your XCConfig file:

OTHER_LDFLAGS = -weak_framework Combine

or if you still want to support building with older Xcode versions:

OTHER_LDFLAGS[sdk=iphoneos13.0] = -weak_framework Combine
OTHER_LDFLAGS[sdk=iphonesimulator13.0] = -weak_framework Combine

OTHER_LDFLAGS[sdk=watchos6.0] = -weak_framework Combine
OTHER_LDFLAGS[sdk=watchsimulator6.0] = -weak_framework Combine

OTHER_LDFLAGS[sdk=appletvos13.0] = -weak_framework Combine
OTHER_LDFLAGS[sdk=appletvsimulator13.0] = -weak_framework Combine

OTHER_LDFLAGS[sdk=macosx10.15] = -weak_framework Combine
like image 166
nschmidt Avatar answered Oct 18 '22 03:10

nschmidt


  1. Navigate to Build Phases tab of your target settings, expand Link binaries with libraries section and right click on SwiftUI.framework, then select Show in Finder.
    Reveal SwiftUI.framework in Finder
  2. Drag Combine.framework from the Finder window and drop it into the frameworks list, then choose Optional from the status popup.
    Drag&Drop Combine.framework from Finder
  3. Select the Combine.framework item in the project explorer (right window pane) and choose Relative to SDK from the Location popup in the inspector (left window pane).
    Specify location relative to SDK
  4. If you get a weird relative path (starting with ../iPhoneOS.sdk/), then open the project in a text editor and fix that manually.
    Fix relative path in project file
like image 2
znerol Avatar answered Oct 18 '22 03:10

znerol


Inspired by @nschmidt answer, but with solution that will work both for Xcode 10 and Xcode 11

Add this to xcconfig

 OTHER_LDFLAGS_XCODE_SPECIFIC_1100 = -weak_framework Combine -weak_framework SwiftUI

 OTHER_LDFLAGS = $(inherited) ${OTHER_LDFLAGS_XCODE_SPECIFIC_$(XCODE_VERSION_ACTUAL)}

Or add OTHER_LDFLAGS_XCODE_SPECIFIC_1100 as custom build setting

like image 1
Igor Palaguta Avatar answered Oct 18 '22 05:10

Igor Palaguta