A quick update to my post from last week. Xcode became available as an App in the Mac App Store, and it no longer includes Dashcode by default. That means you need to download Dashcode separately, which requires an Apple Developer account. Apple gave the impression that Dashcode was included with iBooks Author, which doesn’t appear to be the case. If you don’t have Dashcode, don’t worry, you won’t need it for today.
This is part 2 of my series on Interactive Widgets for iBooks Author.
- Using DashCode to create an interactive widget
- Creating an interactive widget without Dashcode (This article)
- Creating a RemObjects SDK client widget
- Creating a Data Abstract client widget
- Main HTML file – You can have other script files, but one HTML file needs to be your main file. The name is flexible.
- Default.png – This is the image that is displayed when your widget is not activated and it is viewed on the book page. It must be named Default.png.
- Info.plist – This describes your interactive widget to iBooks. It must be named Info.plist.
The Main HTML and Default.png are easy to make from scratch if you have some idea what you want, but the Info.plist is a little more complicated, so lets look at it first.
The Info.plist File
A p-list file, or Property List file, is an XML file that stores serialized objects. It contains a dict element containing key/ value pairs. The Info.plist file in your widget needs to contain specific keys. It should look something like this:
You can copy this p-list and save it to disk as Info.plist, as a start for your widget. I’ve cleaned this p-list down to the minimum elements. Let’s discuss the purpose of each element in detail.
The AllowNetworkAccess key is required (according to the documentation) if your widget is going to access the network (local or Internet). If your widget is running on the Dashboard, this is true, but in my tests it isn’t required in iBooks for network access. I would suggest leaving it in if you want network access, just in case. The true element is the value.
CFBundleIdentifier and CFBundleName keys
The CFBundleIdentifier and CFBundleName keys and their values are required, but from what I have seen, the values are not used in iBooks. Removing them, however, results in the widget no longer working. Feel free to change the values if you like for your Widget. They may become relevant in the future.
The MainHTML key specifies the main HTML file the widget uses. This is the only value you may actually need to change to make your widget work. So whatever you name your main HTML file needs to be the value here. Alternatively, you can just rename your main HTML file index.html and leave the p-list above as is.
There are other key elements that are included by Dashcode, and are required for widgets on the Dashboard, but they are not required, nor do they seem to have an effect, in iBooks.
The Default.png File
The Default.png file serves two very important purpose. The first being that it is the “in page” preview of your widget when it is not active, the second that it defines the size and aspect ratio of your widget when displayed in active mode.
The easiest way to create a preview of your widget is to create a screen shot of your widget running in your browser on the desktop. While your widget is running, press Command-Shift-4 and then use the mouse to select the area to capture. While you are capturing, it tells you the size of the image you are capturing. When you’re done, a new “Screen Shot ????.png” file is saved to the desktop. Simply rename it Default.png and move it into the folder with the rest of your widget.
As a Preview
When selecting the screen shot for the preview, there are a few things to keep in mind.
- It will typically be displayed much smaller when embedded in the page. So if you are relying on small details, they will most likely be lost.
- When the widget activates, the Default.png zooms to actual size and is then replaced by the running widget. So if your Default.png does not line up with the running widget, it will be very obvious when that occurs. A transition on startup might be a good idea if you know your Default.png won’t line up.
- The Default.png should be appealing and give some idea of what information or interactivity the widget provides.
The Widget Size
The dimensions of your Default.png determine the running dimensions of your widget. The iPad has a resolution of 1024×768. Your Default.png can actually be larger than that. If that happens, iBooks will automatically reduce the size of the running widget, maintaining the aspect ratio so it fits on the screen. Also, your widget can run in portrait or landscape mode, which may result in size reduction if the width is greater than 768.
The Main HTML File
Test your Main HTML file in Safari, or better yet on the iPad, to get an idea what it will look and behave like in iBooks. There are subtle differences between different browsers.
All of these files need to be placed in a package. This is actually very easy. Simply place them in a single Folder in Finder. Then move up the path to the parent folder. Select the folder and rename it (press return), adding a .wdgt extension. Finder will prompt you to be sure you want to do that.
Once you click Add, the Folder will be replaced with a widget icon.
- If you double-click this icon, it gets installed in the Dashboard and deleted from Finder. Back it up first!
- Even though finder treats this as a single package, it is still physically a Folder with individual files in it. So if you upload it or move it to a non-Mac computer, it will appear as such.
- You can still access the contents as if it were a folder by right-clicking on it and selecting Show Package Contents.
Adding to iBooks Author
You are now ready to add the widget to iBooks Author. The method is the same I outlined in part 1. You can either drag it onto the widget element in iBooks Author, or use the inspector to Choose the widget.
Things to Remember:
- Remember the Default.png, Info.plist and Main HTML file.
- Use a working Info.plist (copy it from above); it must reference your Main HTML file by name.
- The dimensions of your Default.png determine the dimensions of your widget when it runs.
- Double-clicking a .wdgt package installs it in the Dashboard and deletes it from Finder. Back it up!