6

Apple introduced the concept of "Widgets" in the WWDC 2014. Widgets are used to give quick access to the application with much needed information. I tried to add Widgets to my application. It was easy when we have only one Target in the application.

But, if we have multiple Targets, it seems like we need to add separate Widget for each Target. Is it possible to add a single Widget and make it to share with all the Targets?

Thanks in advance.

Andrew
  • 15,357
  • 6
  • 66
  • 101
ashok
  • 255
  • 2
  • 4
  • 10

3 Answers3

12

This certainly is possible. I have an app called DD-WRT that has 3 separate targets. Basically 1 was for iPhone, 1 was for iPad and 1 was a free 'lite' version. (I've since made iPhone & iPad the same Universal app but still need to maintain all 3 on the App Store).

I wanted to add a today widget to all 3 apps, but with only 1 code base like with the app itself.

So how did I do it...

Start by adding a today widget target for one of your app targets. You can do this by selecting Editor > Add Target and selecting Today Extension from the Application Extension section.

Give the widget target a name and select one of your Application targets this widget will be assigned to.

Add target

You will now see a new group of files in the file inspector which is where you'll code your widget File inspector

Now do exactly the same process for each other app build target you have but giving each one a slightly different name and selecting a different app target for each. You should have something like this All targets

Now you'll also have multiple widget files which is what we don't want. We only want to use 1 set of files. The secret here is with the Info.plist file of the widget. The bundle identifier has to match the bundle identifier of the containing app. So rename each of the widget's Info.plist files so you can tell which is for which target, then move them all to the folder of the first widget you created. You can now delete the files and folders for the extra widgets.

You should now have something like this: Info.plist files

Now you need to tell each extension target that you changed the .plist filename.

Under the Build Settings tab, enter plist into the search box then edit the entry for Info.plist file to the name of the plist for this target. edit plist name

Now you must set all your widget files to be members of each target. For both the default TodayViewController.m and the MainInterface,storyboard files, select them then in the File inspector window tick each target. Like this: File targets

Remember to select all your extension targets when you add new files.

One final thing I found I had to do (I spent ages looking at a crash until I figured it out). In the General tab, for each Extension target, click the Main Interface drop down box and re-select the MainInterface storyboard. Even though it looks like it's already selected, it's actually selected the ones you deleted. Re-selecting it makes sure you are pointing to the only one that's remaining.

Hopefully that's it. Give each target a Clean then build and run each.

Darren
  • 10,182
  • 20
  • 95
  • 162
  • What a great answer, Thanks! (+1) – Yossi Nov 19 '15 at 01:31
  • Thanks for the help and I used the same plist for all target (widget target) – anthonyqz Dec 08 '15 at 23:15
  • Thanks! I want to add: if you send data to the extension via userdefaults, don't forget to check the AppGroups switch – brainray Jan 18 '18 at 14:33
  • this solution is not working in the latest versions of xcode. today you also have another files exept plist (.entitlements, .intentdefinition) so it will not work. the only thing that worked for me is to combine the 2 top answers from: https://stackoverflow.com/questions/25398636/ios-extensions-with-multiple-targets the fitst solution + the script they said we need to add in the pre-build. – Asi Givati Feb 02 '22 at 10:34
0

i had the exact same Problem. Implementing a today widget and want to use it with targets setup for development/deployment and testing (Testflight).

I ended up with creating a second widget adding all needed files and use exactly the same StoryBoard as in the other Widget. So the only thing to maintain is a second info.plist. I could not figure out any easier ways to achieve this.

Before everything worked fine i had to clean the target and reset the simulator for some Reasons.

Hope this helps.

scrat84
  • 185
  • 1
  • 8
0

I solved this by having an extension per target and then only one extension has all the classes and the target membership for each class includes each extension target.

This has the added bonus that I can use some resources such as the colour asset catalogue from the main target in its associated extension.

Leon
  • 3,614
  • 1
  • 33
  • 46