Posts Tagged: objective c


11
Nov 09

iPhone In Action Book – Free Chapter Downloads

Trebitowski-iPhone-2E-1-1

iPhone In Action 2nd Edition

Manning Publishing has started their MEAP (Manning Early Access Program) for the book I am working on.  What this means for you is FREE DOWNLOADS.  There is currently only one chapter available, but there will be more as the book progresses.

The chapter currently available is about audio recording and playback.  It goes into detail about the AVAudio frameworks as well as the MPMediaPlayer.

So be sure to check it out and feedback on the chapter is GREATLY appreciated.

Link to MEAP


28
Jul 09

Creating A Twitter Client In Objective-C Client Part 2

This is part 2 in our series about creating a Twitter client in Objective-C.  In case you missed it, here is a link to part 1 of this series.

In the last tutorial I showed you how to retrieve data from Twitter and display the XML in the Console.  Today, we will be focusing on sending messages to Twitter via POST.  We will be implementing the code to update our Twitter status.  So let’s just dig right in.

1. Updating The TwitterRequest Header File

Open up TwitterRequest.h and add the following code (Click the image to enlarge)

screenshot_16

We have added two properties.  The first isPost will be true when we are calling a method that requires a POST to Twitter.  This will be methods such as update_status, follow, etc… Next, the variable requestBody will hold the POST arguments that will be sent to Twitter.  These will be things such as status text or friend id.

Finally, we will be adding a method called statuses_update.  The reason I named it this is because that is what the method is called in the Twitter API. Like our friends_timeline method, it takes a delegate and selector to call when the request is complete.

Important: I didn’t highlight this in the screenshot but make sure you change theRequest from an NSURLRequest to NSMutableURLRequest.  It will give us some additional methods to set up the POST.

2. Updating The Twitter Request Class

Open up TwitterRequest.m and add the following code (Click the image to enlarge):

screenshot_17

I’ll start by explaining the status_update method.  We first set the global isPost property to true.  This will tell the request method to make a POST.  The next 2 lines set the callback stuff as we did before.  The only new line here is setting the requestBody variable.  This is just a string that looks like “status=new twitter status”.

The addition to the request method is what will allow us to POST to Twitter.  First, we check if the isPost property is set.  This will be true if request is called from our status_update method.  Next, we call the setHTTPMethod of the request to POST. This is pretty obvious.

The following line let’s Twitter know the type of data that we are sending to it.  Next, we call setHTTPBody to set the body of the request.  At some point we will want to URL Encode this string, but that will be for a different tutorial.  Just don’t use any special characters such as & and = in your update to Twitter right now.  All that is happening on this line is we convert the string to NSData using the dataUsingEncoding method of NSString and set it to the HTTPBody.

The last line just sets the Content-Length property to the length of our string.  This is needed to correctly do a POST.

3. Calling The statuses_update Method To Update Your Twitter Status

Open up ApplicationDelegate.m and add the following code (click the image to enlarge):

screenshot_03

One thing to notice here is I commented out the line to get the friends timeline.  This is because having both requests running at the same time with the same request object could cause conflicts.  The best way to approch this to create an entirly new TwitterRequest object.  I just wanted to keep it short.

This is pretty straight forward.  We call the statuses_update method the same way we called the friends_timeline method except pass in the update text.  The information received back from Twitter will look something like this:

screenshot_01

It’s basically all of your personal profile information.

That’s it for today.  If you have any comments or questions, feel free to leave them in the comments of this post or write me on Twitter.  You can also download the source for this version below.

Twitter Mac Client Tutorial 2 – Source

Happy Coding!


25
Feb 09

Why Many iPhone Apps Suck

I have been downloading many different iPhone apps lately and have noticed that many of them suck.

When I say they suck, I’m not necesarily referring to the content of the app. What I am talking about is the programming of the app. But Brandon, how do you know the programming sucks. Well, frequent crashing is an obvious indicator. Also, taking too long to do various computations as well as overall awkwardness.

This is because many non-programmers or hobby programmers decide they want to make an iPhone app without first learning the objective-c language. People just use jank copy and pasted code frankensteined from different examples, close their eyes and pray. This is the architecture of many iPhone apps.

So, who’s fault is this? Well, at first I wanted to say Apple for their lack of tutorials/explanations. After thinking about it, I feel it’s simply lazy programmers driven by trying to make money rather than the desire create a solid and useful applications. It’s quite sad actually.

One challenge here is the iPhone is a terrible platform to learn programming with. The forced program design assumes you have a solid understaning of object oriented programming design patterns as well as many other advanced programming topics. Many CS students don’t even get this until their second year in college! So how can a novice programmer jump right in and make an iPhone app? They code a pile of crap.

So what’s the solution to this? Well I’ll tell you. Teaching people objective-c from the ground up (that, and Apple being more selective when approving apps). I intend to write a whole series of beginner objective-c tutorials (using the mac as a platform rather than the iPhone). I’m not sure yet if these will be posted here or on icodeblog.com. I have yet to decide.

So stay tuned for the first Mac Application Development tutorials.