Yesterday we just tagged v1.0 of the Globalization Pipeline SDK for iOS. What can an iOS client do? Well, let’s build a simple app and find out.
First, I’ll launch XCode 8 and create a new workspace.
While that is launching, I’ll warn you that your author is only a recent graduate of the Swift playground, who once deployed some toy apps to a then-new iPhone 3GS. So, it’s been a while. Any suggestions for improvement are welcome. The actual SDK, however, was a team effort.
Today’s app will be a color mixer, to help artists mix their colors. You know, red and blue makes purple, and so on.
I will name the workspace
gp-ios-color-mixer, and create a new single view app called
GP Color Mixer. To simplify things, for now, I disable the checkbox “automatically manage signing.”
$ brew install carthage
Now I need a
Cartfile that mentions the SDK. So I create one at the same level as my XCode project, containing:
Following the Carthage instructions, I next run
$ carthage update
which results in
*** Fetching gp-ios-client *** Checking out gp-ios-client at "v1.0" *** xcodebuild output can be found in /var/folders/j9/yn_32djn36x4d4c2mvcr1kgm0000gn/T/carthage-xcodebuild.p2nKN2.log *** Building scheme "GPSDK" in TestFramework.xcworkspace
So far so good. Looking in the Finder, I now have
GPSDK.framework right where I expect.
I’ll add it under “Linked frameworks and Libraries”.
We also need to make sure the framework is available at runtime. To do that, we add a build phase with a one-line script:
/usr/local/bin/carthage copy-frameworks with a single input file -
Will it build? I add this to the top of my generated
I mentioned turning off code signing, but I still ran into some odd warnings:
Following QA1940 I was able to make some progress by running
xattr -cr './Carthage/Build/iOS/GPSDK.framework'. Now, ⌘R Run rewards me with a blank app window and no errors. Let’s write some code!
By code, of course, I mean a trip to the storyboard. Let's add a launch icon, because we can.
Now, I add some static fields, two picker views (for the input colors), and a button for action.
Color.swift to handle the color mixing. It will only support mixing from three of the primary colors - Red, Yellow, Blue. Any other mixing turns into muddy brown. Playground tested, ready to go.
Time to wire it up. We create
IBOutlets for each of the items. And, I’ll clear the result label just to verify that things are wired up. It runs OK, good.
Now, let’s set up the delegate stuff so that we can get the list of colors showing.
Hey, just a little more code and we’re feature complete!
At least, feature complete in English.
I’ll next take stock of the resource strings we need to have translated, so that we can run them through the Globalization Pipeline. I’ll call this
First, I create an instance of the Globalization Pipeline. The name you give the instance doesn’t matter here.
Now I create a bundle named
gp-color-mixer. This name does matter, as our iOS app will use it to access the content.
I’ll Upload the
gp-color-mixer.json file above as the source English content, choosing JSON format for the upload. I pick a few languages for the target.
If I view the bundle, I can see our strings there, as well as translated versions.
The Globalization Pipeline offers this web UI to manage content, as well as powerful REST APIs for managing the translation workflow. I need to grant access to the iOS app so that it can read but not modify the translations. So, switching over to the API Users tab…
The result of creating the API user is that some access information is shown, something like the following:
API User ID: 5726d656c6f6e7761746572 Password: aHVudGVyNDIK Instance ID: 77617465726d656c6f6e77617465726d URL: https://something.something.bluemix.net/something/something
I take these and plug them into a new swift file named
ReaderCredentials.swift like so: (this is a variant of ReaderCredentials-SAMPLE.swift in the SDK’s repo)
(Now, after putting my actual credentials in, and a brief offscreen struggle with
.gitignore, I move on…)
I’m almost done.
First, in the
ViewController.swift, we initialize the GP service and start setting up a few UI items:
Here we set up the service with our credentials. Then, we use our new
get(key: ) function to set the title and mix button’s label.
There is also a
get(color: ) variant that will translate one of our
Color objects. So we use that for the actual mixing function:
Similarly, we can get the
UIPickerView to use localized color names by using this same function:
The iOS app will pick up changes if the translated content changes on the server. We could experiment with adding or removing languages, or updating translated keys.
You can find the source code at https://github.com/srl295/gp-ios-color-mixer.
Let me know if this works for you. This is my first post, and as I mentioned first app, in Swift so that’s a milestone. And, do let me know if
^H^H what can be done to improve the sample app.
Thanks! Now go and make it global.