iOS extensions with multiple targets

馋奶兔 提交于 2019-11-29 23:40:08

To share one widget among a lot of targets one should only add widget.appex target to Embedded Binaries for every parent target in General configuration tab

Then you'll get Embed App Extensions area at Build Phases automatically

apparition47

This is my setup: I have 3 targets (production, staging, local) and an extension target that I don't want to duplicate 3 times.

Just to clarify Neo Chen's answer, edit each of your parent targets' schemes:

Build > Pre-actions > New Run Script Action > Provide build settings from (parent scheme).

Paste this for each extension:

#!/bin/bash

buildID=${PRODUCT_BUNDLE_IDENTIFIER}
extId="notification-service"

/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $buildID.$extId" "${SRCROOT}/${extId}/Info.plist"

Seems to work on first build.

It seems like you should be able to just duplicate the Extension target with its own Info.plist, but not anything else.

However, when you create an Extension, Xcode adds "Embed App Extensions" to the Build Phases of the app's target, as seen below, and there's no UI to do that yet.

Still, you can create the extension for the second target, then delete all the files except the .plist, and fix what needs to be fixed. Here's a step-by-step:

  • Create "Extension 1" for "Target 1"
  • Create "Extension 2" for "Target 2"
  • Delete all files created for "Extension 2", except its Info.plist
  • Make the "Build Phases" for "Extension 2" target the same as the build phases for "Extension 1". Usually that's adding the necessary .m files to the "Compile Sources" phase, and resources to the "Copy Bundle Resources" phase

I have create a Run Script to support this requirement

#!/bin/sh
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${SRCROOT}/ImagePush/Info.plist"

buildVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE")
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $buildVersion" "${SRCROOT}/ImagePush/Info.plist"

buildID=${PRODUCT_BUNDLE_IDENTIFIER}
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $buildID.ImagePush" "${SRCROOT}/ImagePush/Info.plist"

ImagePush is my Extension

add to the target which one you need and add make sure this script run before your extension setting in Build Phases, then you just need to do the build action twice (PS: first time it will fail, will try to improve) and it will support multiple target

You need to create multiple extensions for each ID, but you can create dynamic framework and just link it with each extension. Then you will not need to duplicate your code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!