iosxcodecmakeios-extensions

How to create the new target in Xcode for app extension using CMake?


I want to use Notification Content Extension in my Xcode project. I use CMake to generate my project. Now the project has only one target.

I can add the extension as new target manually in Xcode using menu File - New - Target - Notification Content Extension.

Could you provide an example how to create new Xcode project with additional target for app extension by using CMake?


Solution

  • Since CMake 3.8, you could use XCODE_PRODUCT_TYPE target property to let CMake generate specific type of application.

    Minimal example that should troubleshoot you:

    # add app bundle
    add_executable(MyApp MACOSX_BUNDLE ${APP_SOURCE_FILES})
    
    # add app extension bundle
    add_library(MyAppExtension MODULE ${APPEX_SOURCE_FILES})
    set_target_properties(MyAppExtension PROPERTIES
        BUNDLE YES
        XCODE_PRODUCT_TYPE com.apple.product-type.app-extension)
    
    # link extension bundle with UserNotifications frameworks
    find_library(UN_LIB UserNotifications)
    find_library(UNUI_LIB UserNotificationsUI)
    target_link_libraries(MyAppExtension PRIVATE ${UN_LIB} ${UNUI_LIB})