Categories
Development

Revisiting Objective-C, REST, and JSON

I get a lot of hits on this site for two posts.

  1. Objective-C REST and JSON
  2. Objective-C Objects from JSON

Both posts touch on using JSON to create Objective-C objects. The first one mostly talks about great Cocoa Libraries you can use to make REST calls and parse JSON results.

Since that time JSON parsing has become a part of iOS. In iOS 5 Apple introduced NSJSONSerialization.

For a presentation on REST and JSON to @NSSLO back in May I created a super simple project that used NSURL, NSMutableURLRequest, NSURLConnection, and NSJSONSerialization to call the iFixIt API.

If you’re interested in grabbing the project, it’s available on GitHub.

Will write C/C++ for food

The Code

Most of the RESTTest project is a boiler plate iOS application. There are a couple places in MasterViewController.m you should pay attention to and we’ll take a look at the iFixItBadges and the iFixItBadge classes.

The Basics

To get the ball rolling we make a call to a private method: getBadges.

- (void)getBadges; {
	NSURL *url = [NSURL URLWithString:@"http://www.ifixit.com/api/0.1/badges"];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                                       timeoutInterval:10];
	if (request) {
		[request setURL:url];
		connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	}
}

This method is pretty straight forward and uses three of the four classes we mentioned above; NSURL, NSMutableURLRequest, and NSURLConnection. That’s all it takes to do a simple(REST) HTTP GET call. Yes, it’s sample code, so it’s not complex and is not something you’d find in a shipping application as is. It would need some beefing up and it definitely changes if you’re going to do a POST, or DELETE call, not to mention the lack of authentication. I picked the badges call because it didn’t require authentication.

When we allocate and initialize NSURLConnection it takes off and starts doing the work. Notice we’ve specified self as the delegate, which means NSURLConnection will expect us to have implemented the NSURLConnectionDelegate Protocol.

The next method we’ll want to take a look at is connectionDidFinishLoading.

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
	NSString* s = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
	NSLog(@"Received data %@", s);

    badges = [[IFixItBadges alloc] initWithData:receivedData];
    
    [self.tableView reloadData];
}

This is called by NSURLConnection when it’s finished receiving the response from our badges call. At this point we should have a nice JSON response to parse. Notice we’re working on receivedData, which is private and is built in the didReceiveData method. I’d recommend building the code and running it in the debugger to see how it works.

Back to connectionDidFinishLoading. We have our receivedData (an NSMutableData*), now we’re going to create an IFixItBadges object.

Creating objects from JSON

To review. We’ve called the iFixIt badges method and we’ve received our response data. Now we need to do something with it. Say hello to NSJSONSerialization.

- (id)initWithData:(NSData*)data;
{
    if ((self = [super init])) {
        badges = [[NSMutableArray alloc] init];
        
        NSError *error = nil;
        NSArray *resultData = [NSJSONSerialization JSONObjectWithData:data
                                                              options:kNilOptions
                                                                error:&error];
        if (resultData && nil == error && badges) {
            NSDictionary *badgesData = nil;
            NSEnumerator *resultsEnum = [resultData objectEnumerator];
            while (badgesData = [resultsEnum nextObject]) {
                IFixitBadge *badge = [[IFixitBadge alloc] initWithDictionary:badgesData];
                [badges addObject:badge];
            }
        }
    }
    
    return self;
}}

When we get our result back we create an iFixItBadges object, which is a collection of iFixItBadge objects. We could’ve just as easily used NSMutableDictionary or NSMutableArray to hold these objects.

Notice the use of NSJSONSerialization. One call. That’s all it takes to create an array of data we can then iterate over to create an iFixItBadge to add to our collection. Once the collection is created we tell the table view to reload itself using [self.tableView reloadData];

That’s it in a nutshell. If you have any questions about this, or find a problem, please feel free to leave a comment or send email to rob.fahrni@gmail.com.

Happy coding.

Categories
Indie iOS

My @Twitterrific for iOS Wish List

It’s no secret I’m a big fan of Twitterrific from The Iconfactory. I’ve tried a number of Twitter clients, but I keep coming back to Twitterrific. I think it has a lot to do with the minimalistic design. It just works and doesn’t have a lot of extraneous features. But, and there’s always a ‘but’, there are some things I’d like to see. Mostly I’d like a small change to the user interface. It’s something I’ve designed in my top secret design notebook, but since I’ll never create my dream Twitter client, I might as well share these minor UI tweaks in hopes someone will do it. Hopefully, The Iconfactory does it. You never know, it could happen.

Oh, please forgive the mess. I can’t spell Photoshop, much less use it. At least this hack should get the idea across.

Yeah, yeah, I know, it’s all hacked up, but it’ll do.

On the left Twitterrific as it sits today, on the right is what I’d like to see. What’s different?

  1. The account back button is gone.
  2. The title “All Tweets” is gone, replaced by the account name.
  3. The Profile Icon, gone. Replaced by the “Compose Tweet” button.
  4. The bottom bar/toolbar, gone. No replacement.

Since the back button is now gone for choosing an account it’s replaced by the account name in the top bar. Tapping on the name allows you to choose which account to view or what you’d like to view for the current account; All Tweets, Mentions, Messages, Favorites, or Search.

In the upper right corner we’ve replaced the Profile icon with the Compose Tweet icon. To show your profile you can tap on your avatar in a tweet, which works today.

In the lower left corner the refresh button has been removed. I kind of like having an explicit refresh button, but “Pull to refresh” is ubiquitous. If you’d like to have a refresh button, put it in the upper left corner.

The purple bar across the bottom works as it does today. It pops up for a few seconds after a refresh, then goes away.

I think that does it for now.

Categories
How To Social

Updating Twitter from a Facebook RSS feed

Last November Eureka Burger opened a new store in San Luis Obispo and I wanted to keep up to date on events, so I searched for them on Twitter. No luck. They didn’t have a Twitter account, but they did have a Facebook page.

@EurekaBurgerSLO

Since I knew I could publish to Twitter from the excellent ifttt using an RSS feed I setup an Unofficial Eureka Burger SLO Twitter account, @EurekaBurgerSLO. A few clicks to create a Recipe on ifttt and the account was up and running.

Enter Facebook Timelines

When Facebook switched on the new Timeline feature the RSS feed for Eureka Burger SLO stopped working and the @EurekaBurgerSLO account stopped updating. Since I didn’t have a Facebook account I asked my wife to go through the process of finding an RSS feed for a Facebook Timeline. We couldn’t find one, so I gave up on the account and approached a Manager at Eureka Burger SLO about taking over the account. He agreed and I turned over the account to Eureka Burger. I was absolutely trilled. I no longer had to maintain the account and all the problems that might come along for the ride.

Why isn’t it updating?

After a while I noticed the @EurekaBurgerSLO account stopped updating. Why? Well it looks like they had one person updating the feed, instead of hooking it to their Facebook account to automagically publish to Twitter. As of this writing the last update was June 8, 2012. That really bummed me out.

Introducing @SLOEurekaBurger

That’s right, I’ve created a new account so I can automagically update it and see what beer is featured on “Steal the glass night.”

This time around I did a bit of digging, ok, I did a single Google query that lead me to this entry of Stack Overflow. It allowed me to dig out the Facebook RSS feed for Eureka Burger SLO with a couple URL’s.

How To Find a Facebook Wall RSS Feed

We’re going to use the Eureka Burger SLO Facebook account as an example, since it’s what I used.

Step 1: Right click the GIGANTOR image, known as a Cover, and copy the URL.

Step 1: Copy the URL

In this case the URL is:

http://www.facebook.com/photo.php?fbid=335049253213178&set=a.335049249879845.89916.234741763243928&type=1

All we need is the fbid parameter, in bold above.

Step 2: Check the Facebook Graph for that fbid

http://graph.facebook.com/335049253213178

The graph call will produce a JSON feed that looks like this:

Step 1: Copy the URL

Notice the highlighted number id. We need to copy that number for the next step in the process.

Step 3: Get the RSS Feed

https://www.facebook.com/feeds/page.php?id=234741763243928&format=rss20

Once again, notice the bold text above. That’s the id we copied from step two. Now you have an RSS feed for a Facebook Wall. Very nice.

Step 4: Create an ifttt Recipe

Since I needed a way to publish that RSS feed to the new @SLOEurekaBurger Twitter account I chose to use ifttt. I’ll let you go explore that. Ifttt is an awesome service if you need to transform data from one format to another, or from a format to a social media stream, like going from a Facebook RSS feed to Twitter.

Hopefully someone will find this useful. I know I did. Let’s hope it works.

Categories
Hardware

Buying a PC Laptop?

I’m a Mac

I’ve been a Mac for a few years now. When I was working at Pelco a few years back I had a 17″ MacBook Pro and I loved it. We bought our daughters Macs when they graduated from high school and I bought my lovely wife a Mac. We have more than a few iPod’s laying around the house and four iPhones between us. We’re definitely an Apple loving family.

Three years ago, with the help of my brother, we released our first iOS application, RxCalc. It’s been all Apple, all the time since then. I finally got my first Mac for Christmas, December 2011. Up to that point I developed iOS software on my wife’s Mac.

I was finally able to venture out on my own in May, 2012. My primary goal was to find enough freelance work to allow me to work on my own software from time to time. I’d love to ship some iOS and Mac software this year, or early next year. So far things have been great. I’m just rolling off a great project and trying to wrap up another, both iOS. But, being a freelancer is difficult. Work is work, right?

To Windows, or not to Windows?

Most of my 20 plus hears has been spent writing software for Windows, so it’s something I know, and I’m comfortable writing software on that platform. I’ve been in discussions with a company to work on some Windows based software, so I may need a PC once again.

I have a decent MacBook Pro so I wonder if VMWare would do the trick, but the software I’d be working on requires hardware accelerated graphics. Have the VM folks solved that problem? Will Direct3D work? I don’t know the answer to that. These are questions I need to solve so I can decide if spending the cash, and carrying around two laptops, is worth it.

PC Laptops

On Saturday I walked into our local Best Buy to see if I could find a decent 15″ laptop for under $1000. All the laptops were under $1000! That was a big surprise. The thing I quickly discovered is I don’t pay attention to hardware any longer. Since I became a Mac user our choices are limited to a few very high end pieces of hardware, so all you have to decide is how much RAM you’d like and how big the hard drive should be. In the PC laptop world is wild and varied. The one thing I did notice is every laptop I looked at had about 4GB of RAM. That would need to be bumped right away to 8GB and I figure I’d need at least a 500GB drive, but what processor should I get? It’s not like I’m going to use this laptop for anything but coding on Windows.

So, the big question. What sub $1000 laptop is decent enough to install Windows 7/8 and run Visual Studio? It doesn’t have to be the best ever, just good enough.

Comments welcome.

Categories
Social

Facebook Is Not Your Brand

I’m not a fan of Facebook. That’s ok, plenty of you are fans. The business world is obsessed with Facebook. I’d imagine most major corporations have a Facebook page by now. Why? Facebook is not your brand. Why should you promote another companies brand by using them as your main page? Why not use your corporate website for that? Sure, you can have a Facebook page, and advertise there, just don’t make that your brands main site. Doesn’t it seem counter to what you’re trying to achieve? Don’t you want the focus to be your brand, not the fact it’s hosted on Facebook?

Here’s a quote from an article in Fortune that left me thinking, you’re doing it wrong.

The Timeline redesign changed all of that. Tabs have been minimized and — more importantly — companies can no longer set a default landing page. For brands, a critical chance to make a first impression is gone. The first thing users now see on Pages is a fluid, ever-shifting Timeline of recent posts and comments

Emphasis is mine. Why in the world would a company rely on something completely out of their control to make a first impression?

Host your content on your website. It’s your brand, it belongs to you. Link to your important content from your Facebook and Twitter account. That’s what the Internet is all about. Links. Facebook is a walled garden. Why trap your content behind those drab blue walls?

BaahHere’s a great example, at least for me it’s a great example. Peter Jackson has been posting updates about the progress on The Hobbit via his Facebook page. I don’t have a Facebook account, so I can’t get behind the wall to see his updates. Why would you make those posts private? Wouldn’t you like 100% of the people who have access to the Internet to have access to those updates? I guess not. Maybe Peter Jackson’s company is being paid handsomely for his Facebook updates?

The big question is, where does that content go when Facebook disappears as a company?

As my brother is fond of pointing out, in the end I guess we’re all just a bunch of sheep, some choose to follow Facebook.

Categories
Business Social

Twitter Crazy Talk

The hubbub over a recent Twitter Engineering post, and all the follow on posts, has me thinking. No, thinking isn’t one of my strong suits, but here’s what I think is going to happen.

TWITTER IS GOING TO KILL OFF ALL THIRD PARTY CLIENTS.

Watch out! It's a blog fly!That’s it in a nutshell. Why do I believe that? Today Twitter announced it’s overhauling mobile.twitter.com. They just shipped a new release of Twitter for iOS, without a Twitter for Mac update, and now they’ve taken the time to overhaul mobile.twitter.com? Something stinks, besides me.

Why would you spend the time and money to overhaul the mobile website if you didn’t intend to make it your primary view of the Twitter Universe on mobile? Ok, ok, maybe they’re perfectionists, who love their craft, I can accept that explanation. Then again, maybe they plan on killing off all native clients? It could happen, right?

There’s my big BOLD prediction.

I hope I’m wrong. In fact I’m pretty sure I am, but there’s that little part of me that thinks this may happen.

Man, I really don’t want to HAVE to use the website to tweet and read tweets. That would be teh suck.

Categories
Social

Ged talks to The Magic 8-Ball

Gedeon Maheux: “Damn straight they would. There are many developers and companies who have build their livelihood around the Twitter API since 2007. We even helped Twitter evolve and grow to where it is today. Are you saying none of that matters now?”

So, yeah, Twitter client developers are worried. Can you blame them?

Categories
MLOTW

Movie line of the week answer

I forgot to post this last Friday.

The correct answer was…

Back to the Future Part II

Categories
#twitter

Test: Embed Twitter Status

Categories
Social

No, No, No, No, NO!

AHHHHHH!All Things D: “As Sippey’s post notes, it seems that copycat clients — or third party apps that really don’t add much value outside of what Twitter.com offers — may be on the chopping block”

Another take on yesterday’s Twitter announcement.

Can you imagine being stuck with Twitter’s mobile client, or worse, TweetDeck?

If this does happen, and I don’t believe it will, I feel super sorry for the likes of Iconfactory and Tapbots.