Category Archives: Apple

Mac Environment for OS X Yosemite

I updated to OS X Yosemite and as usual a lot of development things stopped working.  I figured it was time to get serious about Python as well, so here’s how I’m setting things up.

brew

I use brew to put packages on Mac OSX.  I like it.  Its easy.  Unfortunately, with the update brew stopped working.  This is because of the new Ruby that is included with OS X.  I found the answer here:

http://jcvangent.com/fixing-homebrew-os-x-10-10-yosemite/

Lifesaver!

brew packages

brew upgrade

That command does it all for me.

rdesktop

This part took the longest.

This is how I access Windows Servers I have to work with.  When I updated rdesktop and I would launch the desktop, I got:

ERROR: Failed to open display: /private/tmp/com.apple.launchd.807sqNUiEV/org.macosforge.xquartz:0

Had to follow instructions here: http://stackoverflow.com/questions/26489928/cant-load-x11-in-r-after-os-x-yosemite-upgrade

Then reboot.  That didn’t work.  Next, I updated XQuartz.  This was done by opening XQuartz and then in the preference menu, check for updates and then update to the latest.  (2.7.7 in my case).  And guess what?  It hung forever.  So I had to go to Apple’s site and download it:

http://xquartz.macosforge.org/landing/

Then I extracted it and it worked fine.  Whew!

Environment

All this environment stuff needs some order.  I setup my bash_profile environment similar to this post:

http://hackercodex.com/guide/mac-osx-mavericks-10.9-configuration/

and this post:

http://hackercodex.com/guide/python-development-environment-on-mac-osx/

The resulting is that I now have Python installed and its coming from /usr/local/bin instead of the one supplied by Apple.

pip install virtualenv

From here I set up a directory called ~/VirtualEnvs as specified by the above referenced setup document.  Now all my projects for python can be done inside of virtual python environments with the command:

virtualenv <myproject>

So we are ready to role with that.

Here is my ~/.bash_profile

[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile

# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
# make my user installed binaries take precedence. 
#export ARCHFLAGS="-arch x86_64"
test -f ~/.bashrc && source ~/.bashrc
export PATH=/usr/local/bin:$PATH
export PATH=/usr/local/sbin:$PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

and here is the .bashrc file

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
# require a virtual environment to install pip
export PIP_REQUIRE_VIRTUALENV=true
# so we don't have to redownload packages.
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache
# more info: http://hackercodex.com/guide/python-development-environment-on-mac-osx/

Hope that helps. And I’m open to nice suggestions.

Ruby on Rails Environment

I noticed my rails projects no longer worked after I opened Xcode and updated some components.  To solve this I found I needed to run:

xcode-select --install

I also ran

gem update

This command took about 45 minutes to run!  That didn’t quite do the trick.  What really did it was my rvm environment got messed up.   We don’t want brew to control ruby versions it turns out.  So I added back the rvm call in the .bash_profile (fixed above).  Then I ran rvm install 2.1 to install the 2.1 version of ruby.  I then did rvm default 2.1 and from now on, that ruby version pops up and I’m good.

 

DIY SSD for MacBook Pro migration

I just finished installing an SSD for my MacBook Pro.  The results have been pretty great so far.  Subtle, but when I really start working again I’m hoping to see a huge difference. I’ve really noticed a performance hit lately especially with Spotlight and opening lots of apps so I’m hoping this solves it.

The first thing I did was bought a SSD from Amazon.  I got the Samsung 840 Series Solid State drive.  The drive came 2 days later thanks to Amazon Prime.  Once it came it was like Christmas.  I opened up the box and threw everything away except the hard drive.

Next I attached it to my computer with a SATA wire.  I borrowed it from a great guy I know at Mountain States Networking named Justin Kurynny.  (He’s the last person in a long line of people that told me I *had* to get an SSD)

From there I just went and downloaded SuperDuper from the web.  It works with Mountain Lion too!  I’m still on Lion and it worked flawlessly.

I did this on a Friday night and went to bed while it copied.  It ended up taking 6 hours to copy 500GB.  I woke up the next morning ready to finish up.

First, I watched a video on how to take apart my MacBook Pro.  Seemed simple enough, and it was.

I layed everything out, took my old hard drive out and replaced it with the new SSD.

The only thing that got me was that the hard drive needed a special tool (or at least a tool I didn’t have) for the drive mount screws.  After a trip to Home Depot with  3 kids who made it take much longer than it should have I had my tool.  I came home and finished replacing it.

Once I got it on all was good.  Super easy.  I turned my MacBook on and it worked just like it did before but with a little more spring in its step.  (You know that same feeling you get when you walk through a shopping center and they’re playing Christmas music? …Ok, maybe not)

A great guy I know named Tony Bourke calls SSDs a “Charlton Heston” technology.  Because once you try it, the only way you’ll ever not use it again is if someone pries it from your cold dead hands.  Awesome.

 

iOS NSStreamDelegates, Grand Central Dispatch, & NSNotificationCenter

One of the more difficult design patterns I’ve managed to get working in Objective-C while developing an iOS app is one that requires NSStreamDelegates, Grand Central Dispatch (GCD) and NSNotificationCenter.  This design pattern comes into play in the following situation:

  • Application launches
  • Application does some communication with a server in the background
  • When the server communication in the background is complete, the View Controller is updated to display information from the server.

Its a pretty common scenario and I’m sure one that has many answers.  In my case, I needed to use CFReadStreams and CFWriteStreams due to the way the client server communication works.  So if there was a simpler way to do it, I’d love to know.

The design pattern that I used at a high level to accomplish this is as follows:

 

  • Application launches,  displays its first View Controller
  • This first View Controller in viewDidLoad registers an observer with NSNotificationCenter and then kicks off a background request via GCD to do all the client/server communication
  • The background request that launches is an NSStreamDelegate object.  It sends info to the server.
  • Once the background request receives information from the server, it sends a notification to NSNotificationCenter that the client/server communication is complete
  • Finally, the first View Controller gets this notification, and then updates its View with that info.

That’s the high level.  So now lets go through and flesh out the details.

Application Launch and First View Controller

This is a simple example, but its just to get you the basic idea.  This part is standard in most iOS apps.  The thing to note about this is that my FirstViewController is not an NSStreamDelegate.  It does however contain an object that is an NSStreamDelegate.  This is called my Communicator class.

...
#import "Communicator.h"
@interface FirstViewController : UIViewController  {
    UILabel *info
    Communicator *myComm;
    UIActivityIndicatorView *spinner;
}
...
// @property stuff here...

First View Controller viewDidLoad

Here’s where we start to get cool.  viewDidLoad looks something like this:

- (void)viewDidLoad
{
    // Let user know that something is happening.. spin the spinner..
    [spinner startAnimating];
    // register for notification of updates.
    [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(updateView) name:@"updateView" object:nil];
    // create a thread and get the server updates.
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        myConnectionInfo = [[Connection alloc] initWithStuff:@"Stuff"
        myComm = [[Communicator alloc] initWithConnection:myConnection];
        [myComm communicate:@"Send this message"]
    });
    [super viewDidLoad];
}

A few things to note here:

1.  We add an observerer to NSNotificationCenter.  This basically says:  If someone sends the updateView message, then I’ll respond to it with my updateView method.  (We’ll show this later)

2.  We dispatch an asynchronous task to GCD.  That’s what this whole dispatch_async business is all about.  This way, the view controller will just display while this task runs in the background.

3.  We instantiate myComm and initiate it with some connection info.  This info might be something you got from the app delegate, some other view controller, or a plist.  That part is left to you to decide.

NSStreamDelegate actions

Our class Communicator is a NSStreamDelegate.  Here’s some of the relevant header info:

...
@interface Communicator : NSObject <NSStreamDelegate> {
    NSInputStream *inputStream;
    NSOutputStream *outputStream;
    NSString *theResult;
    NSString *cmd;
    Connection *myConn;
}
//... @property stuff here... and functions

Nothing big here, just the NSInputStream (the stream we get info from the server) and the NSOutputStream (The stream we write to the server)

As part of the initialization of the Communicator, we have a function called startConnection. This can be called in the init methods. (provided you have all the server communication info: (e.g: user, password, server, port, etc).

Our’s has this:

    self.inputStream = (NSInputStream *)readStream;
    self.outputStream = (NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    // schedule on the main run loop
    dispatch_async(dispatch_get_main_queue(), ^ {

        [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

        [inputStream open];
        [outputStream open];
     });

The important part here is what you do before you schedule your input and output streams on the RunLoop.  The RunLoop is the preferred way to work with NSStreamDelegates.  You have to make sure that you put it running back on the main thread’s runloop.  That’s what this whole dispatch_async(dispatch_get_main_queue() bit is about.  If you leave this out, then you’ll never get any connections going on.

Once you open the connections and you do your reading and writing in the handleEvent function:

-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
// clever code here.
}

Then once you finally get the data from the input stream you send a notification.  This is done for us in the

case NSStreamEventEndEncountered:

we run:

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateView" object:nil];

Before we do that, however, we set our theResult NSString to the value we received.

Updating View in First View Controller

Back in the FirstViewController, we have a function updateView:

- (void)updateView {
    //NSLog(@"Got the update");
    dispatch_async(dispatch_get_main_queue(), ^{
        info.text = myComm.theResult;
        [spinner stopAnimating];
        spinner.hidden = TRUE;
    });
}

Notice here that once again we need to make sure it goes back on the main thread before we update the view. Then, since our Communicator belongs to our ViewController, we can get the result of the client/server communication through the getter and set the UILabel to that result.

Summary

As mentioned before, there are probably lots of ways to do this and some are probably better than what I’ve mentioned here.  However, this was pretty tricky to me to figure out and once I did, I thought it was worth sharing as I couldn’t find any holistic info on it via any search engines.

I’d be curious to know if this helps anybody.  Thanks for reading.

UCS Tech Specs

UCS Tech Specs

My first iOS app has been published on iTunes! It’s called UCS Tech Specs. The purpose of the app was really just to help me get more familiar with the Cisco UCS product line. I have a hard time searching for tech sheets on my computer that have all the details people ask me about a specific server. Having it in an iPhone app is pretty handy! I hope it helps other people as well.

What does it do?
It has technical specifications for the most common UCS products. The fabric interconnects, IO modules, different blades, etc. If someone were to ask you: “How many DIMM slots are available on a C250 M2?” then you could quickly look it up. It’s basically just a series of tables that allow you to drill down to different components of a UCS product.

Who will support it going forward?
Me. I plan on spending an hour or two each month updating with the latest parts. The more people that use it, the more time I’ll spend supporting it. The way I’ve set up the app makes it really easy to add or remove components.

What are future enhancements we’ll see in this app?
There are two main features I’d like to add:
1. I’d like to make the iPad app a little different. I’d like to add bigger pictures, and more interactive.
2. I’d like to make the app check for updates if there is a internet connection. That way, I wouldn’t have to update it in Xcode for changes. It would pull updates from an Internet site. This would make updates faster and make it easier for other people to add changes to the app without having to muck around with the code. I’m working on that right now.

I have a suggestion/comment/correction/complaint for you, how should I get it to you?
I’d love any feedback you have. Just email/twitter/comments here. If you just want to talk about iOS and why my app sucks, that’s ok too.

iOS app submitted to iTunes Connect

I just submitted my first app to iTunes using Xcode4 and iTunes Connect. There was a bit of a learning curve, but after hacking away at it last night I’m happy to say its sitting in Apple’s queue waiting for review.

First off, let me say that even though its a total pain there are some good things that come of it: Because of the validation process they make you go through you don’t have to wonder if there is technically something wrong with your app. The validation phase takes care of making sure you have the basic technical aspects of your app working correctly. This includes verifying certificates are correct, making sure nothing offensive is sent to the store, and making sure the code actually compiles and runs.

Perhaps the most difficult thing for me was to sit down and actually read the manual. The part I missed during the entire development process was the iOS portal:

iOS developer portal

This is where you look to upload to the portal

This is found when you log into the iOS developer center.

The first thing I missed was that I needed to create a ‘Distribution Certificate’.

This distribution certificate then needed to be downloaded into my Keychain. Basically, once this is done, you click on the file to download it, then click on the downloaded file and it will be inside your keychain. Not too hard right?

The next step is to actually create an App ID. I found this part frustrating as you are not allowed to delete an app id once it is created. In addition to this the App ID you create inside of iOS portal must be the same as the bundle ID of the app as well as the bundle ID that you give to iTunes connect. (More on iTunes connect in a bit). This is the part that took me forever because I renamed my app several times. I started with UCS Tech Ref, then UCS Tech Specs. So my bundle ID was ucstechref, then I changed to ucstechspecs. The problem is that in iTunes Connect, I had already named it ucstechref and I wasn’t allowed to change it.

Anyway, once the app ID was created, I configured them for push downloads by doing the process it describes in the configuration section of App ID. When finished my App ID had these things configured:

The last step in the portal was to create a “Distribution Provisioning Profile” for the App itself.

And that was it for the iOS portal. Now keep in mind that before I ran through the iOS portal to create my App IDs and Distribution provisioning profile, I had to actually create the app in iTunes Connect. This was pretty self explanatory.

The last step was putting it all through in Xcode. First, you need to open the Distribution Provisioning Profile and drag it into the Xcode Organizer. (Open Xcode and click shift+command+2). Once done here, you need to code sign your app:

The final part is to make sure your -info.plist has the Bundle Identifier set to the App ID that you specified in iTunes Connect and the App ID in the iOS Portal.

Whew!

Now you have to make sure that you have lots of pictures and the right sizes for all of them! That part was actually kind of enjoyable for me. I use Pixelmator to do all my art work and its pretty easy to get the hang of.

Once you have the right icons specified, go into your product Build Settings and check the ‘Validate Built Product’ and you’ll be warned of any issues you have.

Once that passes, you Archive the application, and can now validate, share, or distribute.

Words can not express the relief I felt after I finally validated the app and it passed all the inspections. The next step was to submit the app. I was stoked. App submitted. But then, I got an email from Apple immediately stating that my app was missing an image! I had forgotten an iPad screenshot. I quickly took one, submitted it in iTunes Connect and I am now back to hurry up and wait mode.

All in all a pretty rigorous process. However, I think its good because it filters a lot of junk from getting on to iTunes. Even though some apps are pretty weak in the iTunes store, they still pass enough hoops to make sure that they’re not complete garbage. I’m hoping my users will at least think that about my app… but if they don’t, hey, what do you expect for free?

Welcome to iPhone development

Last year I learned Ruby and Ruby on Rails.  So in keeping with that good advice to learn a new program language every year, I thought this year I would tackle objective C and Apple’s IDE (xcode).  Why not?  iPhones are fun and the only reason I was able to talk my wife into letting me fork out the money for a Mac was because I told her I wanted to write iPhone apps.  Well, 3 years later and I’m finally doing it…

So here’s how I’m starting:

Resources

As I’ve been researching the best book to buy or examples to find, I’m always struck by how readily information is available to any seeker of wisdom.

Books

There are lots of books about iPhone development and I read the first chapter of several of them.  I ended up accidently clicking the Beginning iPhone 4 Development: Exploring the iOS SDK from Amazon which I suppose wasn’t bad since it seems to be the top recommended book.  The book will probably arrive sometime this week.  But as I checked out the first chapters of the one by the Big Nerd Ranch, I really liked how they presented and explained things.  So that’s it for books.  O’Reilly has a deal today as well for $13 for a book called Cocoa & Objective-C: Up&Running .  Its $14 with the 50% discount code of DDCRN. There seems no shortage of people who want to tell you how to code for the iPhone as long as you are willing to pay for it.

Apple Resources

I was able to download from Apple’s iBook store the free iOS Application Programming Guide, which was actually pretty well written and I’m sure I’ll go back to.  It is about 333 pages and tells how apps run on an i* device.  Many of the concepts I’m sure I’ll forget but hopefully when I run into a serious bug I’ll come back to it and say doh!  its cause of _______.  I actually really like this doc (for now) as it provides a theory which is something a person who already knows how to program needs to get familiar with to get up and running on this platform.  This is a huge recommendation.  There are also several other Apple docs referenced in this pdf file that shouldn’t be overlooked.  Included in this is “The Objective-C  Programming Language”

Videos

Here was where the real treasure chest was found.  The course given at Stanford university is offered on YouTube and is also available from iTunes complete with power point documentation.  Watching the last half of the first class and I already created my first app with a slider that changes a label.  The Stanford class doesn’t use any books and instead references Apple documentation.  It seems to be a great way to start learning. I figure if I do one lecture a day (or 1 every two days where I follow along ) then I’ll be in very good shape.

Other Online Resources

In addition to iTunes U and YouTube there are countless other sites where people freely share information.  Just google the class you want to know about and you’re there.  But how can you beat getting taught from Stanford professors who are also employees of Apple?

The first program

So after using xcode and generating the slider demo from the Stanford class I thought I’d first just try to compile using good old gcc and a basic program.  There was a good wiki article that explained the history of the language.  One of the important things I read was: “In Objective-C one does not call a method; one sends a message” so as a future reference, I have to remember that objects are sent messages to run what would be a method in most normal OOP languages.  The  other few important things:

  • Classes are declared in header files and methods are implemented in the *.m files (which used to stand for messages)
  • In the header file a (-) in front of a method means its an instance method.  A (+) in front of the method means its a class method (similar to how in Ruby we do a self.foo)

So here’s a program named  nothing.m that does nothing:

#import <Foundation/Foundation.h>

int main(){
 return 0;
};

Then, I just compile it on the command line:

gcc -framework Foundation nothing.m -o nothing

Running nothing:

./nothing

Not very interesting, but hey, its a program and it did what you told it to do.  You can then get fancy and add a printf or something else.  But from here on out, I’ll probably be in xcode as that seems to be what all the cool kids are doing.

What are some important beginner tips you have?

NTFS on OSX

Apple doesn’t ship support for mounting NTFS partitions read-write.  You can mount drives, but you can only read from them.  Well that’s not good enough.  Go here:

https://sourceforge.net/projects/catacombae/files/NTFS-3G%20for%20Mac%20OS%20X/2009.4.4/NTFS-3G_2009.4.4-catacombae.dmg/download

No need to reboot, just close the installer when finished, then remount your drive and you’ll be writing like the wind.

I use the same drivers for CentOS/Red Hat  to mount NTFS all the time.

iPhone App development

Last night I tried my hand at iPhone development.  I watched the Stanford classes on iTunes U.  The first class by Evan Doll was ok.  Since I’ve been programming for a while, really the only thing that helped was the last 15 minutes where he gave a demo of it.  The demo didn’t work and there were also some differences in Xcode with my setup (either that or its the new 3.0 stuff.)  I was able to figure it out and get it working.

2 Gotchas:

1.  Make sure you save the .xib file in Interface builder before compiling and running in Xcode with apple-R

2.  The part I was stuck on was adding the Actions and Outlets.   I couldn’t do this in the Inspector window like he did.  I created the class in the Inspector, but then had to add the Action and Outlets in the Tools->Library.  Select the class in the list of all the classes and then you can edit it below.

Once that was done, I could follow along with the rest.  It was a good way to jump into iPhone development.  For me, since I come from the world of no IDE’s and just VI, this was a good intro.  Hopefully I’ll be able to get more into it in the future.  It certainly looks like a cool platform to develop on.

The State of My iPhone

The State of my iPhone is good.  I’m super happy with it, and its by far the best phone I’ve ever had.  I can’t say I’ve ever had a blackberry, but from what I’ve seen from the competition I have not been impressed.  So I remain an Apple fan, and I don’t think the Android phone is ever going to beat it.  The iPhone is to smart phones as Disneyland is to amusement parks.  There are some that have compelling offerings, like open source, an open market, or a faster/bigger roller coaster.  But having the business model where the entire experience is completely controlled is what makes Apple work, and probably the reason they are cringing at AT&T every day.

Since I’ve gone to the iPhone 3.0.1 I’ve stopped jail breaking my phone.  The only reason I had jailbroken the phone in the past was because I wanted tethering, video, and Mike Tyson’s Punch-Out.

The video now comes with the iPhone 3GS, but on my iPhone 3G, I don’t have such luxury.  But it turns out I didn’t use it all that much.  So no big loss.  Also the footage I came out with wasn’t all that great.  I guess I don’t have that exciting of a life.  So no big deal losing that.

Tethering

Tethering is the act of using your iPhone as a modem for your PC to get you on line.  I used to use PDA net from the jail break stuff.  But my friend recently showed me the link below:

http://help.BenM.at

From here it was easy to get tethering.  I opened the above URL from my iPhone, followed the prompts, and now my iPhone can get my computer on line and I can do work.

As far as Mike Tyson’s Punch out, I think I’ll be content after having made it to Bald Bull 2.  So I no longer have an NES emulator on my iPhone.

Application Updates

My top apps:

  1. Pandora
  2. Facebook
  3. TweedtDeck
  4. Classics
  5. The Weather Channel
  6. iSSH
  7. Shakespeare
  8. Constitution
  9. AP Mobile
  10. Bump
  11. vlingo
  12. GPSLite

The last 3 are ones that my friend just introduced me to.