Category Archives: OS X

Stripping print and debugPrint in Swift for release builds

When building an app for release with Swift you should make sure that any of the print and debugPrint statements you used when building and debugging the app are disabled.  The reason for this is that if you leave them in then they will unnecessarily slow down the app since every write to console output takes time.

In Swift you can easily do this by redeclaring the print and debugPrint functions using these two lines of code…

func debugPrint(items: Any..., separator: String = " ", terminator: String = "\n") {}

func print(items: Any..., separator: String = " ", terminator: String = "\n") {}

We simply declare the functions again but with empty implementations of {}.  When you’re building and testing your app just comment these two lines, but when complete make sure you uncomment them before release.

You should place these two lines at the top-level of your code.  That means, do not put it inside a class, extension or protocol declaration.  A good place to put them is at the top of your AppDelegate file, before the AppDelegate class and not inside it.

Alternatively, simply create a brand new Swift file in your project and call it something like ‘ReleasePrint.swift’ and put the code in there.  Basically, it can be anywhere as long as it is top-level and not inside another class.

Finally, you may find it useful to always see the output from print and debugPrint in the simulator, but never on the device.  If you want to do this you can use the architecture flags to automatically turn the output on and off as follows…

#if !arch(x86_64) && !arch(i386)

func debugPrint(items: Any..., separator: String = " ", terminator: String = "\n") {}
func print(items: Any..., separator: String = " ", terminator: String = "\n") {}

#endif

Network Utility in OS X 10.9 Mavericks

In OS X 10.9 Mavericks the very useful Network Utility app has been moved from its previous location in /Applications/Utilities to /System/Library/CoreServices/Applications. You can activate it by navigating to that folder and simply opening it as normal.

There is also another way to activate Network Utility. Simply open up the ‘System Information’ app, which can still be found in Applications/Utilities, then in the menus at the top of the screen open up the ‘Window’ menu and select ‘Network Utility’.

mavnetutil

How to: Create a bootable installation for OS X Mavericks 10.9 and above

UPDATE: With the release version of OS X Mavericks there is now a much easier way to create a bootable installer. Simply follow these steps.

1) Download Mavericks from the Mac App Store but do not click install. If you install then after it upgrades your machine the installer will be automatically deleted.

2) Insert a USB flash drive and use Disk Utility to format it, name it ‘Untitled’. The installer takes over 4GB so you’ll need at least an 8GB drive.

3) Open terminal and run the following command…

sudo /Applications/Install\ OS\ X\ Mavericks.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ OS\ X\ Mavericks.app --nointeraction

4) You’ll see some output in Terminal letting you know it’s copying files. When that’s done your USB will be ready with the bootable installer.

How to spoof a MAC address on OS X Lion 10.7 (and probably Leopard)

It used to be a little easier to do before Leopard (10.5), but you can still spoof a MAC address rather easily in OS X Lion. If you don’t know why you’d want to spoof a MAC address then you probably don’t want to be doing it, but there are plenty valid reasons why you’d want to do so. Here are some steps on how to fake your address to the example MAC address of fa:ca:dd:fa:ca:dd, and there’s a more detailed description of each step below if you’re interested. Stuff marked like this should by typed into Terminal.

1. Decide which network interface you want to spoof, this is probably en0 or en1, remember which one you pick. I’ll assume you are using en1 for the remaining steps.
2. ifconfig Take a note of your current MAC address for your interface (en0 or en1) in case you want to revert later (but don’t worry if you forget, a reboot will always revert it to the real MAC address anyway).
3 (Wi-Fi only). sudo /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -z
4 (Wi-Fi only). Wait about 10 seconds
5. sudo ifconfig en1 ether fa:ca:dd:fa:ca:dd

* Note if you want to know the MAC addresses of other devices already on the network you can view the arp table by doing…

arp -a

More detail on the steps for those who like to know what’s going on…

(1) Firstly, identify which network interface you want to change, the wired connection or the Wi-Fi. If you have both then they are most likely en0 for wired and en1 for Wi-Fi. If you only have one, such as the MacBook Air that only has Wi-Fi, then it’s probably en0. You can see a list of your network interfaces by typing ifconfig into Terminal. Alternatively, launch the Network Utility app in the Application/Utilities and you’ll see them in the drop-down list there.

(2) Regardless of whether you use ifconfig or Network Utility you’ll see the MAC address listed so take a note of it so that you can revert back later. By the way, if you forget it your machine will always revert to the true MAC address when you reboot anyway so don’t worry.

(3) The next step is to use the airport command to disassociate completely. This is necessary because if you try to set it to fake a MAC address and you are already connected to a network then the change probably won’t take affect. The command you use to do this is /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport. If you are going to be doing this a lot though then you may want to create a symbolic link to the command so you don’t have to keep typing the full path. To do that type in…

sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport /usr/sbin/airport

After that you you can just type ‘airport’ from then on instead of typing the full path. So now to disassociate you just type…

sudo airport -z

(4) Even if your Wi-Fi icon goes grey immediately you should still wait 5-10 seconds to make sure it is fully disconnected before proceeding.

(5) This command is the one that tells OS X to pretend that the interface has a different MAC address.  At this point you probably want to check that the change has actually worked.  The easiest way to do this is to type the following and check that one of the results is the fake MAC you just set…

ifconfig | grep ether