I’ve been listening to The History of English Podcast. It’s not easy to binge but it’s a mainstay in my weekly podcast rotation. I’m currently around the fall of the Roman Empire. It’s fascinating that our alphabet has bounced between cultures but with only minor changes.
Automating iOS app builds
I believe an important part of the development process is getting a working version into the hands of others. As part of this, being able to install the latest and greatest on the fly is paramount. It took me a lot of time to find a reliable way to build, sign, and distribute apps as part of this internal continuous delivery.
Localizing attributed strings on iOS
In an iOS app, localization can be especially difficult when dealing with attributed strings. Fairly often, designers request something like:
Read our Terms of Service, Privacy Policy, or contact us with any questions.
or like:
Searching for burgers in SOMA, San Francisco, CA:
The golden rule of localized strings is to treat them as atomic units:
- Never concatenate strings to form sentences. Many languages have different sentence structure or gender rules than English and you cannot just substitute a single word or phrase in for any other.
- Never use substrings for searching in an original string. If you depend on the standalone translation of “Privacy Policy” matching the sentence version, you will likely find translators do not understand this intent. You may also find the same search term multiple times (imagine if the user searched for “Search”).
Often these complexities are cited as reasons to avoid localization. But, unless you have geographic constraints, you will find a substantially larger audience with a localized application.
ZSWTappableLabel and ZSWTaggedString are two open-source libraries I have released to help solve these problems.
ZSWTappableLabel makes links inside your attributed strings tappable, as the name suggests. It’s a UILabel subclass which does not do any drawing itself, making it fast and easy.
ZSWTaggedString is the powerhouse. It transforms an HTML-like syntax into an attributed string. You can read more about the syntax and advanced usage on its GitHub page, but here’s how you might use it for the examples above:
Read our <i><tos>Terms of Service</tos></i>, <i><privacy>Privacy Policy</privacy></i>, or <i><contact>contact us</contact></i> with any questions.
Searching for <term>%@</term> in <location>%@</location>:
In my experience, localizers1 are familiar enough with HTML to have no issues with localizing these strings. By marking the regions you intend to be visually distinct, they can more easily understand your intent, producing better localizations.
While on the subject, here are a few best practices for localization in iOS:
- To handle the current locale changing, or the dynamic type setting changing, reload your UI when observing:
NSCurrentLocaleDidChangeNotification
UIContentSizeCategoryDidChangeNotification
- To represent dates, durations, distances, lengths, etc., use an appropriate formatter.
- To create your own date formats, use
+dateFormatFromTemplate:options:locale:
on NSDateFormatter. Remember that these need recreating if the locale changes. - To combine a first and last name, use
ABPersonGetCompositeNameFormatForRecord
with a temporaryABPersonRef
, or use the newNSPersonNameComponentsFormatter
. - For sending non-user-facing data to a server, use
en_US_POSIX
as your locale.Read more tips and tricks at NSHipster about NSLocalizedString and NSLocale.
T-Mobile: good ideas, bad experience
When T-Mobile entered the wireless scene as the “Uncarrier” I was impressed. Their greatest contribution to the carrier ecosystem is consistently adding features, forcing other carriers to keep up. Instead of rationed text and voice, we’re in a world where data is king.
However, if you are considering T-Mobile service, I suggest reconsidering. The plans and features look appetizing, but their execution leaves a lot to be desired. I would not count on their unique features; just the now-basics with worse coverage.
I wrote off my initial experiences as anecdotal, but it became cumulatively enough for me to leave their service.
Enabling bridge mode on AT&T U-verse
These are instructions to configure a U-verse gateway to send all of its incoming traffic to your own router without impacting its normal networking services.
IRC channel prefixes for Gmail labels
I filter a lot of email at work, and got into a really hellish game battling hierarchical labels. To restore myself to sanity, and to corral them into some sort of system, required inspiration: IRC. Specifically, the prefixes used on its channels (chat rooms). These prefixes vary from most important to least, and they sort in the following order as well.
Saving optimal JPEGs on iOS
Conventional wisdom for creating a JPEG version of a UIImage
is first to turn it into an NSData
and immediately write it to disk like so:
NSData *jpegRepresentation = UIImageJPEGRepresentation(image, 0.94);
[jpegRepresentation writeToFile:outputURL.path
atomically:NO];
Most of the time this is exactly right. However, if file size is important, Image IO is a great alternative. It is a powerful system framework to read and write images, and produces smaller files at the same compression level.
Why Image IO?
A project I am working on requires uploading photos en masse. Low upload bandwidth makes file size a limiting factor, so I sought out ways to reduce it.
I put together a test project to find the differences between the two methods. The results are pretty interesting:
- Image IO files are on average 20% (but up to 30%) smaller1.
- Image IO takes about 2x longer.
The only discernable visual difference is the grain in the images, but even that is minor. Here’s a diff between two versions of the original photo. The changes are nearly all in the grain.
Using Image IO
First, you’ll need to add two new framework dependencies:
@import ImageIO; // to do the actual work
@import MobileCoreServices; // for the type defines
When creating your JPEG file, the first step is to create a CGImageDestinationRef
specifying where to write the result:
CGImageDestinationRef destinationRef =
CGImageDestinationCreateWithURL((__bridge CFURLRef)outputURL,
/* file type */ kUTTypeJPEG,
/* number of images */ 1,
/* reserved */ NULL);
Image IO is able to produce files of a few different types2 but my focus here is JPEGs. Next, we set up the properties of the output file, specifying a constant compression factor:
NSDictionary *properties = @{
(__bridge NSString *)kCGImageDestinationLossyCompressionQuality: @(0.94)
};
CGImageDestinationSetProperties(destinationRef,
(__bridge CFDictionaryRef)properties);
And, importantly, we specify what is to be written out:
CGImageDestinationAddImage(destinationRef,
/* image */ image.CGImage,
/* properties */ NULL);
And finally, we write it to disk and clean up the reference:
CGImageDestinationFinalize(destinationRef);
CFRelease(destinationRef);
- The UIImage version has a color profile, while the Image IO version does not. However, running both files through Image Optim produces a 7% reduction on both, so I am choosing to ignore this difference. Afterall, you can’t remove the color profile anyway! ↩
-
The following are possible types you can use, from the documentation:
Constant UTI type kUTTypeImage
public.image kUTTypePNG
public.png kUTTypeJPEG
public.jpeg kUTTypeJPEG2000
public.jpeg-2000 (OS X only) kUTTypeTIFF
public.tiff kUTTypePICT
com.apple.pict (OS X only) kUTTypeGIF
com.compuserve.gif
Using the Xcode Structure menu
Xcode’s Editor > Structure menu has a few great actions:
These actions all act on either your cursor position or selection.
Balance Delimiter
Normally you can double-click quotes, brackets, or parenthesis to select the matching character and all text in-between. The Balance Delimiter behaves similarly: it looks at your cursor position (or selection), finds the nearest pair and selects in-between.
This doesn’t have a default keyboard shortcut, but you can set one up in Xcode’s Key Bindings preferences. I set it to ⇧⌃I
since I use it in similar ways to Re-Indent.
Re-Indent ⌃I
Objective-C is a fairly indentation-heavy language, and Xcode generally does a good job at indenting while you’re typing. However, if you’re pasting text or refactoring things can get pretty hairy, so let Xcode fix up your code for you with Re-Indent.
Shift Left ⌘[
, Shift Right ⌘]
You can also indent manually using the Shift Left and Shift Right actions, which unindent or indent by one tab, respectively. They do exactly what they say on the tin.
Move Line Up ⌥⌘[
, Move Line Down ⌥⌘]
These actions move your current line (or selection) up or down by one line. Simple, right? What’s really useful is that it is context-aware: they understand going in and out of control flow or blocks. Much faster than cutting, pasting and re-indenting each time.
Comment Selection ⌘/
Rather than wrapping your code in /* … */
and dealing with the conflicting multi-line comments you probably already have, simply select what you want to temporarily eliminate and hit the shortcut. Each line selected is then prefixed by //
.
How I started programming
My first experience programming was making webpages seventeen years ago at the age of ten. It was amazing: by just typing some words and strange markup into a text editor I could create exactly what anyone else could! A bunch of documents magically turned into websites.
Error arguments in Objective-C
From the Programming with Objective-C (backup) overview from Apple:
When dealing with errors passed by reference, it’s important to test the return value of the method to see whether an error occurred, as shown above. Don’t just test to see whether the error pointer was set to point to an error.
and from the Error Handling Programming Guide (backup):
Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning
nil
orNO
, you should always check that the return value isnil
orNO
before attempting to do anything with theNSError
object.
For example, let’s say we’re executing a fetch request:
NSError *error = nil;
NSFetchRequest *request = /* … */;
NSArray *objects = [context executeFetchRequest:request
error:&error];
To test if the fetch was successful, we must do:
if (objects) {
// hooray!
} else {
NSLog(@"Got an error: %@", error);
}
A method taking an NSError **
does not guarantee how it is used when successful. In this case, even a successful method call may end up with error
set to something other than nil
.