Search code examples
iosswiftios10-today-widget

Sharing UI elements between main App and Today widget/extension in Swift


I have a few UI elements on my app's ViewController (mainly UILabels). I am having to recreate these on my Today extension's View Controller. This does not seem right. Is there a way to share/reuse these UI elements; for example in a container view?


Solution

  • There are a few ways to do this. (Swift modules, frameworks, static libraries)

    The simplest way to get what you want is to just add the view's file to the Build Sources of the Today widgets target.

    If you're UI component is AwesomeView.swift it'll be listed under:
    Xcode Project Settings > Targets > MainApp > Build Phases > Compiled Sources

    Go to:
    Targets > TodayWidget > Build Phases > Compiled Sources

    Then click the + and add the AwesomeView.swift file to the today widget target as well. Now it will be compiled into, and can be used by, both binaries.