iOS extensions with multiple targets

2019-02-02 03:53发布

In iOS 8, when we create a new extension, we have to decide which target it is attached to. The extension will have the same bundle ID's prefix as the target.

  1. Is there any way to change the target afterward?
  2. If my project contains 2 (or more) targets (for example one for debug/simulator, one for production/device), what's the best way to work with extensions? Do I need to create another extension and duplicate the code (very bothersome to keep the same code for both targets)?

5条回答
对你真心纯属浪费
2楼-- · 2019-02-02 04:02

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.

enter image description here

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
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-02 04:04

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

查看更多
一纸荒年 Trace。
4楼-- · 2019-02-02 04:11

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

enter image description here

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

enter image description here

查看更多
一纸荒年 Trace。
5楼-- · 2019-02-02 04:27

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.

查看更多
混吃等死
6楼-- · 2019-02-02 04:29

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.

查看更多
登录 后发表回答