Supporting iPhone 6/6 Plus Native Resolutions: The Basics

Ever since I got the new iPhone 6, I’ve been silently cursing the applications that haven’t yet properly updated their apps for the new screen sizes. The zoomed iPhone 5s display looks really terrible, folks! So, I’m going to give you a five minute tutorial to update your applications. Assuming, of course, that you updated your app last year for iOS 7. I’m also assuming that you have a reasonable understanding of how to navigate Xcode, so this post is a bit light on screenshots.

Step 0: Upgrade to Xcode 6.
I’ll wait. This doesn’t count as part of the five minutes.

Step 1: Add a Launch Screen File in Project Settings

This one is actually pretty easy. Click on your project on the left of Xcode, and select the proper project under “TARGETS”. It’ll probably be auto-selected. Go to the General tab (again, probably already selected) and scroll down to the “App Icons and Launch Images” section. For the Launch Screen File, select your storyboard file. Easy.

Set up Launch Screen File.

Set up Launch Screen File.

Step 2: Update all of your constraints

Alright, so this will take a bit longer. Basically, add constraints to your layouts so that it can stretch and still keep your controls where you want them. Set up constraints for the items on the left based on the left layout bound, and do the same for the items on the right based on the right layout bound. Auto-layout is your friend here. Pay attention to any red or yellow warning indicators that appear in your Document Outline, and fix them/add other constraints where necessary. One of the things that you’ll notice here is that in Xcode 6, the default left/right bounds are to “Margin”. This is something that was introduced in iOS 8, so if you’re still supporting iOS 7, as you should be, this will mess up your layout on those devices. The easiest way to avoid using margins in your constraints is to press the Option key, which changes “Leading Edge to Container Margin” to “Leading Edge to Container”, which was the default behavior in previous versions.

Constraint setup with Container Margins.

Constraint setup with Container Margins.

Constraint setup after pressing Option key.

Constraints setup after pressing Option key.

Step 2.1: A “Gotcha” on Table Views

Sometimes, you just can’t get the table view to start at the top of it’s container. Assuming that you have created your layout correctly, and set the TableView’s top to the Top Layout Guide, it’s because there’s an “Inset” set up. Click on the View Controller in the Document Outline, and uncheck “Adjust Scroll View Insets”.

TableView insets.

TableView insets.

Step 3: Add some new image assets

The iPhone 6 Plus’s display requires @3x image assets. If you want to truly support that device, you need to add @3x versions of your images and icons. @2x images will still work, but they’re obviously scaled up to the @3x size, which can look bad depending on the image.

Application icons.

Application icons.

That’s all. You’re kind of ready for the new iPhones!

iOS 8: Notifications Changes

We’re updating our iOS application at work, and I came across some fun with iOS 8 and notifications. So, I thought I’d write a short post about it.

In iOS 7, we used the following method on the shared UIApplication instance to set up notifications:

In iOS 8, this doesn’t work. So now, it’s a multi-step process:

First, we get an instance of the shared UIApplication (this is obviously optional, but I think it makes the code easier to read). Then, we create a UIUserNotificationSettings instance that specifies which notifications we want. After that, we register for user notifications, and finally, register for remote notifications.

The new method, registerForRemoteNotifications:, sends the same messages as the registerForRemoteNotificationTypes: method. These are application:didRegisterForRemoteNotificationsWithDeviceToken: for a successful registration, and application:didFailToRegisterForRemoteNotificationsWithError: for an error.

Well, what if you want to support both iOS 7 and iOS 8? It’s simple. See if the UIApplication instance responds to the new selector. Here’s the complete code:

Let me know if you have any questions or if there’s a better way to do this in the comments.

EDIT: I originally messed up the if statement in the complete code above. I used registerUserNotificationSettings instead of registerUserNotificationSettings: which caused the statement to be false, even on iOS 8.

SimpleLookups 1.6 Released

Today, I released SimpleLookups v1.6.0. This version includes the following changes:

New Features:
– Specify column suffixes for Name/Description/Code/Id columns.
– Specify column name of Active column.
– Specify whether or not to prepend the table name to the column names.
– Core improvements/optimizations.

Version 1.6 is now available on NuGet and on this site. I recommend NuGet, but to each their own.

As typical, a few links for the lazy:
Getting Started with SimpleLookups

Download SimpleLookups 1.6 | NuGet
View Complete SimpleLookups API Documentation

GIT, Powershell, and Windows 8.1

This weekend I opted to upgrade(depending on your opinion) my laptop to Windows 8.1. Now that I have lost all credibility, I can move on to talk about my experience with using GIT on Windows 8.1, and the issue I had with my PowerShell profile script.

Like many users, I have a profile script set up in PowerShell so that when I open it, my main code directory is the working directory, and I also have a custom prompt set up to tell me the current folder’s GIT status. I wish I could give credit where it is due on this script, but unfortunately I don’t know who wrote the original, original version. It was passed to me by my coworkers and I’ve made some changes/fixes to it to fit my environment.

powershellwithgit

As you can see in the image on the left, my PowerShell prompt shows the current branch, master, the current directory, and the status of my local copy (c = created, u = updated, d = deleted). When I moved to Windows 8.1 (with PowerShell 4.0), the script naturally stopped working properly. So, as a help to anyone out there that also uses this type of script and is making the jump, here’s a working version for Windows 8.1, which also works on Windows 7 (PowerShell 2.0+). You have to place it in the following location and it should be named this way for it to work properly:

DRIVE:\Users\USERNAME\Documents\WindowsPowershell\Microsoft.PowerShell_profile.ps1

You can verify this location on your machine by typing “$profile” into PowerShell.

Here’s the script. Make sure you replace my working path with your own:

In order to run this, you must first run the following command in PowerShell as an administrator, which will allow you to run unsigned scripts:

There’s more information about signed/unsigned PowerShell scripts here.

Any suggestions for improvement? Hit me up in the comments.

Pattern of the Week: Builder

This post is part of a series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Creational

This week’s pattern is called the Builder pattern. The builder pattern is a common way to create an object, using methods and properties to set up exactly the kind of object you want, then calling a Build() method to actually create the object. Let’s look at an interface for a builder object:

This interface has a couple properties, ShirtColor and ShirtSize, and a method called Build. Let’s look at ShirtBuilder, the concrete implementation of this interface:

This class creates a Shirt object, which implements the IShirt interface. This interface and class are incredibly simple, so we don’t need to go over them here. Please look at the source code if you’re curious.

Now that we have the builder ready to create shirts, what do we need to do now? Let’s use it! A builder is also really easy to use.

The shirt variable above now has a fully initialized Shirt object with all of the parameters that you wanted. It’s ready to use.

The question is: when should I use this? You should use this pattern when you need to specify a lot of details about the object you’re creating. If you just need an object, with maybe one or two properties that need initialized, you should consider using the Abstract Factory pattern or a simple copy constructor instead. If there are a ton of things that you need to set on the object, Builder is the way to go.

This is pretty much all there is to this pattern. If you need additional help, please take a look at the full source code below, or feel free to comment, and I’ll respond directly.

Visit next week for an explanation of another pattern.

View Sample Code

Pattern of the Week: Prototype

This is the seventh in a series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Creational

This week’s pattern is called the Prototype pattern. This pattern is used when you need to create exact copies of objects. Let’s look at our example Prototype class:

This StreetSign class has two properties – Text and Shape, and a method called Clone. The prototype pattern is fully represented by the “Clone” method. Basically, the Clone method should be implemented so that it returns an exact copy of the object on which it is called.

There are many ways to implement this pattern. A most ideal way would be if you could have the method reflect over the entire object. This would mean that you could add properties to the class without having to change the Clone method. The simplest way is the way that I implemented it in the code sample. A new instance is created and the properties are set on the new object (in this case, via the constructor). Either way is valid.

It should also be noted that there is a method on System.Object in .NET called MemberwiseClone, which essentially implements the Prototype pattern. The only thing about this method is that it creates a shallow copy, so if you have objects within your objects, those won’t be copied. Instead, the same instances of those sub-objects will be referenced by the clones.

This is pretty much all there is to this pattern. If you need additional help, please take a look at the full source code below, or feel free to comment, and I’ll respond directly.

Visit next week for an explanation of another pattern.

View Sample Code

Pattern of the Week: Proxy

This is the sixth in a series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Structural

This week’s pattern is called the Proxy pattern. This pattern is primarily used to add security or to implement caching for objects. For my example, I’ll be adding security to this class:

This is a pretty simple class, which implements the following interface:

Now, suppose I want to make sure that not everyone has access to the GetStrings method. Well, I could modify the GetStrings method itself, and add some complex code to check a user’s permissions, or I could implement a Proxy to do that for me. The main benefit to using this approach is that I don’t overly complicate GetStrings, and I can separate these two concerns (permissions and data access). Here’s my Proxy class:

The proxy implements the same interface as the original class. This means that you should be able to swap the proxy for the original object with minimal code changes. So how does this all work? Basically, we would use the Proxy to access the object, and if the user doesn’t have access (which we’re not checking here), a SecurityException will be thrown, preventing access to the underlying resource. Obviously, in a real-world use of this pattern, you would actually check the user’s permissions in the ValidateAccess method (and always remember to default to least privilege on errors).

You may notice that this pattern is fairly similar to the Decorator pattern. The difference between the two, as far as I can tell, is that the Proxy actually owns the object that it is adding functionality to (it creates it), while the Decorator has the object passed in as a parameter in the constructor.

This is pretty much all there is to this pattern. If you need additional help, please take a look at the full source code below, or feel free to comment, and I’ll respond directly.

Visit next week for an explanation of the Prototype pattern.

View Sample Code

My Dog, Chuckie

I spoke with my sister and dad on Monday night, and they told me that they had to put my dog, Chuckie, to sleep. I say “my dog” in the sense that for the last eight years, I didn’t live with him, I didn’t feed him, I didn’t take him outside when he needed it, but every single time I went home, I was the only person he was concerned with.

ChuckieWe got Chuckie, a light-brown cocker spaniel, when he was about 2 years old. He was, prior to that point, an outside dog. (This is something that I personally disagree with, to be honest, but I’ll save that rant for later.) There were several large dogs that also lived at that old house, and he had to fight for food and water. My mom joked to the woman who lived at the house that, “if you don’t want that dog, we’ll take him!” A few days later, they gave him to us. We already had another cocker spaniel, Jumanji, and my Dad didn’t want more than one, so we gave him to my sister’s boyfriend. Chuckie lived there for a couple weeks, until my sister took him. You see, her boyfriend’s family grew angry at Chuckie’s in-home restrooming, but instead of trying to train him, they kicked him. My sister saw this, and literally put the dog in her car, and left.

Once she got home with him, we decided to keep him. It was at this point, that Chuckie started to gravitate towards me.

Jumanji definitely considered my dad to be #1. Chuckie considered me to be #1. Every single night for most of my teenage years, Chuckie would climb under the covers and sleep next to me. He would always defend me against “I’m gonna get him”‘s and tended to end up wherever I was in the house. He sat beside me when I spent countless hours learning things about computers, or reading books, or searching the Internet for the latest anime, or playing PlayStation. It’s cliché to say that he was my best friend, but he was. There’s nothing like having a friend like that. He didn’t care about how cool I was or anything, he just wanted to be with me all the time.

Years have passed, and more recently, Chuckie had started not responding when we called his name. He had lost his hearing. His eyes also started getting cloudy. As it turned out, he was completely blind in one of his eyes, and the other one wasn’t doing well either. We had discussed what we would do if he did go completely blind and deaf, but I never really wanted to face that reality. Chuckie was my dog, and I didn’t want him to leave us.

On Monday, the veterinarian told my sister that a growth that was on his mouth was cancerous. It wasn’t something that could be removed, and it was an aggressive form of cancer. He was destined to decline quickly and painfully. They decided that in order to ease this suffering, they would have him put to sleep. While I wish that my family would have given me an opportunity to say goodbye, I am glad that he is no longer suffering from it. It would have been selfish of me to have made him suffer just so I could see him again.

The times that I shared with him will be with me for the rest of my life. Whether it was throwing the ball for him (and the way he couldn’t figure out what direction the ball would be bouncing), or just the way he HAD to sleep in my bed with me (and many other times when I wasn’t there, and he still slept in my bed because he missed me).

You were my dog. Rest in peace, buddy.

Pattern of the Week: Abstract Factory

This is the fifth in a series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Creational

This week’s pattern is the Abstract Factory pattern. To explain this, we must first understand the factory class.

Factories create things, plain and simple. Whether we’re talking about software development or retail, that is what they do. Factory classes typically look something like this:

The method creates an instance of its favorite implementation of the interface that is returned. Obviously, with a simple code change, you could swap out “ConcreteSomething” for “SpecialSomething” and your code would still work, assuming of course that those are both implementations of ISomething. This is a central idea of a SOLID principle, called the Liskov Substitution Principle. Basically, we should be able to swap out an implementation of an interface without breaking our code, or changing the behavior of the application, i.e., post-conditions of using the interface should be the same no matter the implementation that we use. Now that we’ve talked about Factories and Barbara Liskov, let’s go back to the pattern at hand.

The Abstract Factory pattern can be used when you want to define different versions of the same family of interfaces in your application. You know that you need certain objects that implement certain interfaces, but you might need to swap out which ones you use depending on a number of factors (which OS, database server, etc.). The implementation of certain interfaces that you end up using is completely dependent on what implementation of the factory you’re using. Here’s an abstract factory interface, which creates clothes (kind of like a real factory!):

This interface defines two methods: CreateShirt(), and CreatePants(). These methods return IShirt and IPants respectively. Let’s look at those interfaces:

Alright, so we now have defined our interfaces that we will use in the factory. So what do we do next? Well, an interface is fairly useless without implementations, so let’s create some. Here is a DressClothesFactory:

Simple enough, right? The factory defines the CreateShirt() method as returning a DressShirt, which is an implementation of IShirt, and it defines CreatePants() as returning DressPants, which is an implementation of IPants. We could also define a CasualClothesFactory which would return a T-Shirt and Jeans respectively.

The real power of this is when we define a method or class that takes an abstract factory implementation, and uses it to instantiate the classes that it needs. For example, let’s look at the ShowWhatAmIWearingToday() method:

We know ahead of time that we need to get a shirt and get pants, but we don’t really care if it’s a casual day or a dress-up day. We still need those two things no matter what, the only difference is what specific family of IShirt/IPants we’re using.

This is pretty much all there is to this pattern. If you need additional help, please take a look at the full source code below, or feel free to comment, and I’ll respond directly.

Visit next week for an explanation of the Proxy pattern.

View Sample Code

Pattern of the Week: Template Method

This is the fourth in a series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Behavioral

This week’s pattern is the Template Method pattern. This pattern allows you to define a set of algorithms that follow the same procedure, but that might have certain parts that differ. For this pattern, I’m borrowing heavily from my post on the Strategy Pattern, so if you haven’t read it yet, you should read that one first.

Alright, let’s look at a template method:

This method, called Attack(), defines a basic algorithm of attack. First, we print out the warrior’s name (as a battle cry?), then we get that warrior’s weapon of choice, and finally, we use the weapon. Here’s the rest of the Warrior abstract class:

We use this abstract Warrior class to define specific types of Warriors, like this one:

As you can see, the derived class “fills in the blanks” of the template method, by defining what the GetWarriorName() and GetWeaponOfChoice() methods do. We’re filling in the blanks of the template by using inheritance. When we call the Attack() method, the methods defined in the derived class are called, completing the algorithm for the type of Warrior we’re using.

This is pretty much all there is to the Template Method pattern. If you need additional help, please take a look at the full source code below, or feel free to comment, and I’ll respond directly.

Visit next week for an explanation of the Abstract Factory pattern.

View Sample Code