Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 - Single Widget for multiple targets

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.

like image 628
ashok Avatar asked Dec 15 '22 21:12

ashok


1 Answers

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.

like image 177
Darren Avatar answered Jan 03 '23 12:01

Darren