简体   繁体   中英

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?

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})

I tested on cmake3.23 on mac, app extension is a executable not library. It should be like this:

add_executable(MyAppExtension MACOSX_BUNDLE ${APPEX_SOURCE_FILES})
set_target_properties(MyAppExtension PROPERTIES
    XCODE_PRODUCT_TYPE com.apple.product-type.app-extension)

Then you can embed app extension to app:

set_target_properties(MyApp PROPERTIES
        XCODE_EMBED_APP_EXTENSIONS MyAppExtension)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM