简体   繁体   English

如何使用 CMake 在 Xcode 中为应用程序扩展创建新目标?

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

I want to use Notification Content Extension in my Xcode project.我想在我的Xcode项目中使用通知内容扩展。 I use CMake to generate my project.我使用 CMake 生成我的项目。 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.我可以使用菜单文件 - 新建 - 目标 - Notification内容扩展在Xcode中手动将扩展添加为新目标。

Could you provide an example how to create new Xcode project with additional target for app extension by using CMake?您能否提供一个示例,说明如何使用 CMake 创建新的Xcode项目以及应用程序扩展的附加目标?

Since CMake 3.8, you could use XCODE_PRODUCT_TYPE target property to let CMake generate specific type of application. 从CMake 3.8开始,您可以使用XCODE_PRODUCT_TYPE目标属性让CMake生成特定类型的应用程序。

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.我在 mac 上的 cmake3.23 上测试过,app extension 是一个可执行文件而不是库。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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