iOS Extensions with Multiple Targets

How to make an extension work for several app targets?

There are these option to you:

1.
You can duplicate the extensions, provide id from your developer portal, or let Xcode add them itself, and change the target identifiers to your new target.

2.
The other way is, to change the extensions bundle ids on build actions of the scheme to your currently required ones.
This could look somewhat like this, if you want to use PlistBuddy:

/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier fullBundleIdOfExtension" $PROJECT_DIR/Extension\ Directory/Info.plist

fullBundleIdOfExtension is something like com.company.application.extension

This way could cause errors sometimes on the build after a target switch, showing a dialog that the App could not be installed. This requires you to perform a clean build and rebuild.

iOS Notification Service Extension with multiple targets never calls extension methods

Your notification payload is definitely part of the problem.
In your question the payload is:

{
"aps":{
"mutalbe-content":1,
"content-available":1,
"sound":"true",
"alert":{
"title":"Vera Pauw",
"body":"test bericht"
},
"data":{
"type":"test",
"message":"test bericht",
"chatId":"3b002aa6-1117-4206-b54f-8bc2bd689f1c",
"fromId":"",
"fromname":"Vera Pauw"
}
}
}

The aps key should only contain Apple's keys. Your data dictionary should be outside the aps key.

The "content-available" should not be present if the alert, button, or sound keys are there - a notification can not be both silent and user-facing. content-available is only for silent notifications.

The "mutalbe-content" key is also misspelled, which is why your extension is not being activated!

I see many people having the same issues so I wrote a simple push notification validation tool you can use to troubleshoot these problems.

Depending on what you are trying to accomplish a corrected payload might look like this:

{
"aps":{
"mutable-content":1,
"alert":{
"title":"Vera Pauw",
"body":"test bericht"
}
},
"data":{
"type":"test",
"message":"test bericht",
"chatId":"3b002aa6-1117-4206-b54f-8bc2bd689f1c",
"fromId":"",
"fromname":"Vera Pauw"
}
}

Try that and see if the extension launches.

Same extensions in many targets

I think the best would be to have all this extension in one place like a target or an external library and to import it where you need them.

Different deployment targets on app extension

Deployment Targets do not need to match.

Watch app shared across multiple targets

You definitely cannot use one Watch app for multiple targets. Because the only factor Xcode uses trying to find out whether to include a Watch companion app to the bundle or not is a bundle ID of the Watch app. So there is a one-to-one relationship.

It follows you should duplicate each WatchOS app and extension target (changing only the bundle ID) for each iOS app target. But you still can share your code (including storyboards and assets) between targets by using Target Membership and it solves a problem of branded Watch apps either. Finally you get the minimum code duplication overhead.

Update: I see you're trying to duplicate a Watch target using a right click on a target and choosing a Duplicate option. It really doesn't work for some reason.

I just tried to clone a Watch target performing the following steps:

  1. File -> New -> Target -> WatchKit App.
  2. Specified the Embed in Companion Application the needed target.
  3. Added all files to the newly created target using the Target Membership option.

After that a project has been built and ran without any problem with a new Watch target.

This way you don't have to change the bundle ID manually - everything is created automatically and smoothly by Xcode.

Get Bundle ID from a different target

The build settings of the targets are not available for the other targets. However the build settings of the PROJECT are available.

You can thus add a user-defined build setting in your project and call it PARENT_APP_BUNDLE_IDENTIFIER and set the correct bundle id for all your schemes.

Then in the info tab of the app extension target set the bundle id to

$(PARENT_APP_BUNDLE_IDENTIFIER).$(PRODUCT_NAME:rfc1034identifier)

It worked perfectly fine for me.



Related Topics



Leave a reply



Submit