NSURLConnection and iPad SDK 3.2

I was coding an app that made a series of asynchronous HTTP requests, and stumbled upon a strange bug that I couldn’t quite pin down.

Running under iOS4 in the simulator and device (iPhone4 and 3.1.x), there were no problems writing this:

The connection would initialize and then run without a hitch. As I was hoping to port the app over to the iPad, I decided to test it using the iPad simulator under 3.2. However, once the app hit [c start];, I immediately got a EXC_BAD_ACCESS exception and the app crashed.

On further inspection, it seems as though the app will obey the startImmediately boolean on an iPhone device, but is strangely ignored on an iPad. I gave the documentation a read-through, and I couldn’t find anything of the sort.

A somewhat simple but annoying work around involved starting the NSURLConnection request immediately and removing [c start];:

Hopefully someone will be able to shed light on this one, it really had me stumped.

Modal View Controller Example – Part 1

In your iPhone app, you’ll probably be spending most of the time pushing new view controllers to the stack in order to show screen flow. Sometimes, though, you just want to popup a screen for quick display or input.

Here’s a quick demo/tutorial on the different standard modal views offered by iOS, including a simple way of passing generic string data back to the parent view. I’m assuming basic knowledge of iPhone programming, so feel free to skim if you’re comfortable.

First up, create a new Xcode View-Based application. I named mine “ModalViewExample”. Open up the NIB file “ModalViewExampleViewController.xib” and drag four buttons onto the screen as shown.

Now, we’ll begin attaching the buttons created in Interface Builder to our View Controller code. Open “ModalViewExampleViewController.h” and declare your buttons as IBOutlets (Interface Builder Outlets):

Make sure you synthesize the outlets in ModalViewExampleViewController.m, and release them in dealloc

Ok, so now we have some buttons declared in our class, and some buttons dropped into the interface. At this point, your app has no idea what the relationship is. We’ll set these relationships in Interface Builder.

Switch back to your NIB file, and select “File’s Owner” in the NIB Object Window. Select the Connections Inspector tab, and drag a connection from the hollow point to the Button object in your View. You can see what I mean in the screenshot below. Repeat for the other buttons on the page.

Basically, what we’ve told Xcode to do is relate the code-based objects with the interface objects we created. This allows them to pass messages back and forth.

The next step is to actually assign methods to the click events. Go back to your ModalViewExampleViewController.h file and add these methods:

You’ll also need to implement them in ModalViewExampleViewController.m:

We now have definitions for buttons, and the actions to go with them. To connect them, go to Interface Builder. Select one of your buttons and drag a relationship between the “Touch Up Inside” event in the Connections Inspector and the First Responder in the Object Window. Select the name of the method you created.

After connecting all the buttons to their relative objects and actions, you’ll actually need to make the app do something. This is where we will define a modal view to present to the user.

Add a new UIViewController subclass with XIB to your project. I named mine “SampleViewController”. Open the XIB in Interface Builder and drag a new button onto the screen.

Once you’ve done that, create an IBOutlet for the button and define an action as above.

Be sure to go to Interface Builder and assign the connections as you did above. Once that’s done, you’ll need to synthesize and release the dismissViewButton object in SampleViewController.m as you did in ModalExampleViewController.m.

Assuming all your connections are in place, it’s time to actually do something with them. Switch to your ModalViewExampleViewController class and replace your previous method definitions with the following code. Also be sure to add #import “SampleViewController.h” to the top of your definition in order to expose SampleView to your class.

Let’s take a very quick look at what we’re doing. First, we define an instance of the new SampleViewController class. We then give it a modal transition style, based on the values found in the UIViewController documentation. Sending it to the front is as easy as using presentModalViewController:animated: Take note that you should probably use the navigation controller to push modal views if you have one. (i.e. [self.navigationController presentModalViewController:animated:]).

To finish up, we’ll need to code a way to dismiss the modal view once it’s presented to the user. Jump to the SampleViewController class and add this code:

Once that’s in place, build and run the project. You should be able to click through the buttons and check out the range of different modal views iOS has to offer. Note that the page curl only works in iOS 3.2 or greater. If you’re compiling for lower versions, you’ll just get the default slide up animation.

In Part 2, we’ll add a delegate in order to pass values between the modal and non-modal views.

Download the sample project from this tutorial here.

Quick Zend Cache Setup

Here’s a quick solution to get a cache running on your Zend Framework install. It’s very simple and should provide a good solution for most small-scale sites.

You can put this code inside of your bootstrap file. I like to use the method style for invoking the bootstrapper.

What this snippet does is initialise two related caches – a core cache, and a page cache. The core cache (in this instance) is attached to our database table abstract class, which allows results to be stored as serialized strings. The page cache is invoked whenever template output is due to occur, and saves pre-rendered HTML for immediate output.

You can find all the documentation for tweaking your cache settings in the Zend_Cache documention.

Simple Age-Check in iPhone SDK

For a recent project, I needed to get the user’s age within my iPhone application. It was a simple “how old are you?” question. Here’s the quick snippet for how I went about getting that info.

What you’ll already need is either:

  1. a UIPickerView object created, initialized, and set in your view controller; or
  2. a UIPickerView created and attached using Interface Builder.

In this example, I’m using the variable name pickerView for my UIPickerView object.

That’s it! How accurate it is depends entirely on Apple’s implementation. You can also do the same for any of the calendar values listed in the NSLocale documentation.

Zend, SSL, and mod_rewrite

I had a Zend project in which we had to use the Apache module mod_rewrite to “toggle” our secure connection depending on what pages the user landed on. The requirements were something like:

  • User visits any administration or account pages, and SSL is off, switch it on
  • User visits a page that is not an administration or account page, and SSL is on switch it off

As it was a Zend MVC project, we also had a rewrite rule that routed everything to /index.php in the document root.

This is Zend’s recommended .htaccess rule for routing:

For the uninitiated, the block says: If the request is a valid file that exists, or it is a directory, don’t rewrite the URL, otherwise, rewrite the whole string to read “index.php.” The framework will generally just read the query string server variable and go from there.

So on to my additions for toggling SSL.

The above code checks the secure protocol, and then checks the requested path (in this case, /admin or /account). In finding that, it toggles the SSL accordingly.

Here’s the problem: because the two toggling rules only work if triggered as a redirect, the server’s request URL is rewritten twice, meaning that any “toggling” will result in a flat redirect to /index.php, breaking the application.

After hours of testing, rewriting, and Google-fu, I came up with this fix:

becomes:

This means that the request is reattached to the end of the query all the time. This is hidden from the user (i.e. no ugly index.php/admin/login/ or what-have-you), and is successfully passed onto Zend to trigger the appropriate dispatcher. This works for any number of redirects.

Hope this saves someone else hours of trial-and-error.

Good Greif – Greifswald

On my way to Rostock proper, I took a trip out with friends to visit the quiet town of Greifswald to the East. It’s about 800 years old or thereabouts (if you count the Monastery) and originally flourished due to the fact they had a brewery (this is Germany, after all.) The name comes from a legend of a Greif (Gryphon) living in the now non-existent forest that would eat disobedient kids who strayed too far. Such charming old stories.

We took a trip out to a small harbour, where I stood overlooking a partially frozen sea, before venturing back into the main square to have a quick look around. This isn’t a tourist city, so sights per se were limited. Nonetheless, the town was very charming.

Highlight: Wallensteinkeller Nordic restaurant (using the term loosely here) where you do as they did in the middle ages — eat with your hands, listen to “Ye Olde” style archaic German, and become fat on the awesome food and beer.

Bremen – A Grimm City

Although a port-city like it’s cousin Hamburg, Bremen is actually a much nicer town. It is smaller, perhaps by half, but there were just as many things to see as Hamburg. It lacked a version of the Reeperbahn, but it did have the Stadtmusikanten (Town Musicians), a very cool statue based on a Grimm Brothers fairy-tale.

I was a little concerned about the location of my hostel on arrival – it featured a sign at the top of street that read (roughly translated): “No weapons between 8pm and 8am.” If they need to tell people not to carry weapons, I’m going to stay inside. The stay was absolutely uneventful in that regard, so I lumped the warning together with the “No Tanks” sign I saw in Hamburg.

There were a half-dozen or so streets that were old (and nice, unlike Hamburg), and also, they were worth seeing (unlike Hamburg). Think The Rocks, but throughout the whole town. It wasn’t a novelty, but it was good to be able to walk around the peaceful streets without needing to check for my wallet every five minutes. The river running through Bremen was also cleaner, but I could probably attribute that to less marine traffic.

Oh, and I was also told by hostel reception about an “Australian Pub” along the pub-strip next to the water. Out of curiosity as to what makes an “Australian Pub”, I visited it. As opposed to the Aussie pub in Dresden named “Ayers Rock”, which sold kangaroo steak, this particular pub (“Kangaroo Island”) only sold cocktails. And Foster’s. Yes, they actually think we drink that swill in Australia. Sigh.

Highlight: Relaxing in the main square in front of the Rathaus, Frauenkirche, and other Baroque buildings with a coffee while a brass-band played some cool music.

Hamburg – Full of Hamburgers

Hamburg is the second-largest city in Germany, and the second-largest active port in Europe. I could probably just end the post there. It is not really a tourist city, save for its red-light district in St. Pauli – the Reeperbahn. Consequently, during the day I spent most of my time wandering around looking for things to see.

The Hamburg Rathaus is one of the most impressive buildings in the city (certainly better than Berlin’s Rotes Rathaus), and it was good to finally see water that wasn’t falling from the sky. Unfortunately, I missed the fish markets on the Sunday morning, which are apparently chaotic. Then again, if one of your city’s tourist attractions is your fish market, you really need to reconsider your definition of tourism.

Although being stuck for things to see in the city, I met an Australian in the hostel, and we took a sightseeing tour by bus. I had hopes that they would show me something I a) had not already seen; or b) could visit again properly. Well, the bus was good, in a way – it confirmed my suspicions that there really is not much in Hamburg as a tourist. That being said, I didn’t mind wandering around, as there were enough shops to poke around in, and there weren’t churches on every street corner. The long walk along the main harbour was also good, and something different from the other cities I’d already visited.

On one of the evenings we did manage to visit the Reeperbahn, though. I had an image in my mind of “seedy” and attached St. Pauli to this image, but it couldn’t compare to what it was actually like. Every ten shops or so weren’t sex shops or strip clubs, and homeless people littered the streets like cigarette butts. Groups of punks were throwing up in the gutter out the front of super markets, and creepy fat guys were slurring out invitations to the “life shows” (sic) going on inside their club of employment. I fully expected the median age of the visitors to be something around 20-25, but in reality it was more like 50-55. It’s as if all that was dodgy about Hamburg was distilled and poured between the suburban borders of St. Pauli. It was great. The place had a disgusting character about it that was charming and hilarious, and it’s no wonder it attracts so many people in what is an otherwise boring city.

Highlight: The Reeperbahn. It’s practically the only thing with a pulse in the city, and although the beer was expensive and the food crap, I didn’t get VD from stepping onto the street.

Potsdam – Homebrand Berlin

On a spur of the moment I decided to visit Postdam, a city just outside of the border of Berlin and in the state of Brandenburg. It’s a very small city (especially coming from Berlin), and it could easily be explored in a day. My main attraction to the city was Schloss Sanssouci and the park it’s situated in.

After leaving the hotel, the weather was turning bad. Map in hand, I walked out anyway, determined to explore the tiny town and the Park. I saw Potsdam’s Brandenburger Tor (the Bi-Lo version of the Berlin Brandenburger Tor) and walked through the main square. It was nice enough, but the rain was coming down harder, and I needed to find the Park and palace.

Well, flash forward 40 minutes, and I’m standing at the bank of the Neusee, about 5km in the wrong direction from the Park, soaked by the pouring rain. I found an information board to confirm my exact location, and after shaking myself off like a German Shepherd, walked back the 5km the way I came. Once I recognised the street I needed to be on, the rain stopped and allowed the sun to start drying me off. I eventually found Park Sanssouci without a hitch after my detour.

As opposed to the Tiergarten in Berlin, there was actually life in the trees at Park Sanssouci. A major surprise was finding a running fountain with ducks. A note here: every other fountain I’d come across had been switched off due to Winter, presumably to avoid freezing. So to see ducks chilling out under a running fountain was a welcome change, and probably a good indicator that Spring had decided to show up after all.

The front of the palace is a large, stepped garden leading up to the main doorway. The view from the bottom up towards the Palace was good, but the view from the top was even better. After finding that tours were not on that day (for an unknown reason), I walked around the park for a few hours and made my way home (sans detour).

Berlin – Bear Patrol

Berlin is huge. I liked it because it actually felt like I was visiting a real city for the first time this trip. I’ll split the trip up by destinations instead of blow-by-blow. You should get through a beer and/or coffee while reading this.

Brandenburger Tor

The Brandenburger Tor sits in the middle of Berlin (more or less), and overlooks Pariser Platz. It is a monolithic structure, featuring a statue atop a series of columns. It attracts tourists and beggars like flies to sh-… honey. The Berlin Wall ran right past when it still stood. I visited the Brandenburger Tor a number of times as it was quite close and worth just strolling past on the way to somewhere else.

The Reichstag

Woo politics. The Reichstag is the German equivalent of Parliament House. Besides being huge and architecturally old, there wasn’t much interesting about it. It burned down in 1933, and was bombed to pieces in 1945. Retaining mostly the same design, it’s back. Maybe that was a good reason to visit.

Schloss Charlottenburg

The Palace at Charlottenburg was originally the home of a German big-wig, but now houses a Porcelain museum (yeah, I know), so the only thing worth taking a look at were the sculptures and architecture. Normally, there is a huge garden behind the Palace, but – yep, you guessed it – there was nothing to see in Winter. Plus, they charged you the princely sum of 12€ for the privilege. No thanks, I’ll just stick to the outside. It was worth the ten or fifteen minutes spent there.

Rotes Rathaus

Despite the name, the Rathaus doesn’t actually contain Rats, it contains Councillors. Yes, yes, I know. Apparently Berlin’s Rathaus is special because it’s made from that god-awful red-brick that the 80s loved so much. Not my cup of tea, but the Berliners are proud of it.

Tiergarten

Ok, now I know that I’m visiting during Winter. That’s fine. But when I’m on a tour bus, and the “picturesque” Tiergarten is on my right, I’d at least like to see something resembling life. The Tiergarten was originally the King’s personal hunting ground, smack-dab in the middle of Berlin city. Now, it’s a huge public park (I mean really big) with all manner of ponds and plant-life. At least, that’s what I presume it’s like in Summer. Nevertheless, I think the photos are nice, and strolling through a forest of sticks was relaxing and much better than getting buffeted by icy winds on my way to the other places in the city.

Fernsehturm

Berlin’s Fernsehturm (TV Tower) can be seen from a lot of places in the city, and is one of the tallest structures in the world. For an arm and a leg, you can take the lift to the viewing platform, and for the other arm and leg, you can eat in the revolving restaurant (great food, though), 300 metres or so above the ground. You could feel your ears pop as the lift shot you into the sky. No pictures from the tower, as it was night when I visited the lookout.

Berliner Dom

What’s this? Another Church? Yes, please! No, just kidding. Everything pales in comparison to the Kölner Dom, which I still have nightmares about. Sure, it’s big, and it wasn’t hideous on the inside like the Frauenkirche, but it’s really just another building where Kings could show everyone how great they were. That being said, I swore out loud when I saw one of the sarcophagi had a sculpted figure of Death writing the occupant’s name into his book. Creepy as hell.

Holocaust Mahnmal

The Holocaust Mahnmal (memorial) is a series of grey stone blocks of various heights. It’s hard to describe more than that. No names, just blocks. It was weird walking through, as the ground is unexpectedly uneven, and unbeknownst to me at the time, about 40 kids were running up and down along the narrow paths. Who knew a memorial could be fun?

Neue Nationalgalerie

Supposedly a “new” art gallery (mix of modern and contemporary, if I recall correctly). The most of the building was either undergoing renovation, or was closed due to the setting up of new exhibitions. This took the grand total of available works in the gallery to: 1. The main (only) room is a huge open space with glass walls. In the centre hung a chandelier. The artwork? The carpet. What’s worse, is that it looked like it was blown up from a mobile phone picture and printed at Harvey Norman onto some cheap carpet. I laughed twice – once, because the work was just really horrible; and twice because the security guard looked embarrassed whenever someone walked in, looked around, and walked straight back out. They were gracious enough to waive the entry fee to the gallery.

Kaiser-Wilhelm-Gedächtniskirche

This fragment of a church is supposedly a reminder that war sucks. It was originally a large church built in Charlottenburg by – you guessed it – Kaiser Wilhelm. It was partially destroyed by bombs like everything else in Berlin in 1945. Part of it remained standing though, so instead of rebuilding it, they secured the ruins and called it a memorial. What they should’ve called it is the Kaiser-Wilhelm-Würstekirche, because there were eight wurst stalls at the foot of the church, surrounded by an equal number of souvenir shops. War – what is it good for? Profits, apparently.

Bauhaus Archiv

After already trekking for 4km once to look for this place and finding it closed, I was keen to go back when it was actually open for business. The Bauhaus school was a design school in the early 20th century that focused on simplicity of colour and form (mainly in object design and architecture). You know you’ve reached the pinnacle of success when your designs are so simplistic that your design museum sucks. I can’t hate the Bauhaus, because I love what they’ve produced, – “tubular chairs” and current modern “desk lamps” are just two prolific designs to come out of the school. I think it reminded me too much of my classes in University. Nevertheless, I’d be kicking myself if I’d never visited. My favourite part of the museum was the temporary exhibit featuring current Japanese simplicity in design.

Gendarmenmarkt

The Gendarmenmarkt is a square that features the Konzerthaus, flanked by the Deutscher Dom on the left and Französicher Dom on the right. Individually, the buildings are moderately impressive, but the fact they are all set together makes the whole square look really impressive.

Jüdisches Museum

I called this the “Jew Museum” to a friend and immediately got an icy reply. Ok, so the Jewish Museum in Berlin is a museum dedicated to all Jewish history (as opposed to just the treatment in the war). Upon walking in, I had to empty my bag, my pockets, go through a body scanner, and was wanded over before a guard decided to pat me down. I packed myself back together, only to be forced to check my bag and jacket at the coat room (it was freezing in the museum by-the-by). I put the criminal treatment behind me (as well as the 200,000 multi-national students milling about in all corners) and head into the museum proper. The architecture was… interesting to say the least. Angles, corners, jags, voids, darkness – everywhere. There was a big justification spiel for the design, but honestly I don’t remember it (something about a fragmented Star of David). The museum featured a timeline of Jewish history from about 990 CE to the present day. The focus was on their persecution throughout history, and how awesome it is/was to be Jewish. I wish there was another way to describe it, but this is the impression that one gets after visiting. Despite being new, this museum was no better or worse than any of the others I’ve visited (and believe me, that’s been a few).

Siegessäule

The Siegessäule – translated by the tourist board as “Victory Column” – is a tall column with a golden statue on top, erected to commemorate victories in wars against the Dutch. Yeah, the Dutch had an army; although apparently not a very good one. Anyway, you can normally go through an underground passage and take a long trip up the stairwell to a lookout on the tower. Except right now – it’s under restoration for Summer. It was impressive in any case, and I’d definitely bid on it if I saw it on eBay with 10 minutes left. AAAAA++++++++++

Museum für Naturkunde

Oh, come on, I’m a geek, ok. It’s the natural science museum, and apparently one of the largest collections in the world, with over one million specimens. After seeing some of the “collection”, though, that’s less than it seems. (Referring to the tray of aphids presented. Maybe 100 in a 20cm square area). The exhibits were much of a muchness, but they do have the largest (mounted) Brachiosaurus skeleton on the planet, and also the Archaeopteryx fossil. I think maybe 2 people reading this will know what I mean and share my excitement. I was embarrassed for them when they featured a couple Australian animals, though – the “Koala Bear”, and the “Tasmanian Wolf”. Shudder.

Dalí Museum

They should reserve the word “museum” for places that would bore most people, but I don’t think “The Dalí House of FUCK YES” would fit on their sign board. The “museum” was great – there were sculptures, lithographs, videos, paintings, step-by-step litho prints, biographies, commissioned works – everything. The best part was that the work was not only really strange (in the typical Dalí way), but the artistic skill could also easily be seen (as opposed to the questionable ability of some contemporary artists). There was so much detail in even the mundane sketches that you could easily spend a long time studying the works. Worth visiting for anyone even half-way interested in art history, or anyone who likes to giggle at scribbles of penises on desks.

Typographie des Terrors

Ok, let this be a lesson to anyone sightseeing in an unknown city – research your destination. My travel book and city-map from the hotel both listed “Typographie des Terrors” as being an exhibit about Nazi history. In reality, the “exhibit” doesn’t exist yet (not until later in 2010), so they’ve erected a walkway with a series of information boards along the construction site. It was really interesting, but there’s something about reading Nazi history in freezing temperatures that doesn’t do it for me. I think the twenty or so others near me thought the same thing.

Checkpoint Charlie

Seeing Checkpoint Charlie left me a little speechless, to be honest. I’ve seen historical photos of the checkpoint (and others like it), including buildings nearby and various “highlights” involving tanks and citizens. To see the location in person was very strange. We ventured into the Checkpoint Charlie museum and spent a while reading about the things that actually happened between the East and West, and the ramifications that the wall had on political tensions. The strangest story (to pick just one) is the story of a five year old who drowned in the river. His family watched him die because to jump in would mean being executed by the border patrol on the opposite side. The entire history of the wall is filled with these types of stories. There are a number of cool pieces of history at Checkpoint Charlie, including the “last” Kremlin flag in Berlin, the Checkpoint box itself, and (in the museum) the fragmented piece of concrete with the white painted stripe signifying the border between the sectors (as they said in the museum “tanks used to face off on either side of this line.”)

Two things here made me groan out loud, though. As a bit of comic relief, the Tzechisches Zentrum (Czech Center) is based at Checkpoint Charlie. They’ve nicknamed their office “Czech Point.” Oh, it gets worse. Next door is a small shop selling food, with a signboard above it labelled “Snackpoint Charlie.” What was war good for again?

Madame Tussaud’s

I should preface this with the fact that I had no intention of going. That is, until I saw their Oliver Khan in the advert (German goalkeeper). It was *really* good. So I took my ticket and strolled on in past Marilyn and JFK, although I didn’t recognise him at first with the top of his head still intact. First of all, I was greeted with a sign, something along the lines of: “We have a Hitler model. Please don’t pose or take pictures.” Of course, everyone was sneaking in pictures – surveillance be damned. It’s a truly surreal experience being in the museum. They look /way/ too real, so when you bump into them, it’s habit to say “oh sorry” and then feel instantly stupid as the waxy face smiles back at you. There were dozens of people inside, all kissing and hugging the myriad figures. I should also note at this point that most of the celebrities were German, with a few US stars thrown in for good measure. The Pope and Johnny Depp were both really good, although no pics of the Deppster due to hundreds of fawning girls. I’ll probably take a trip to the Sydney museum when I return.

Berliner Mauer Gallery

There are a few locations where the Berlin Wall still stands, and it’s a lot thinner and smaller than you would imagine a wall separating a city to be. However, one of the locations has been opened up to artists as a mural gallery. Just… wow. Words are useless here, look at the pics.

As for Berlin in general – I love it. It’s a busy, modern city that I could easily work in given the opportunity (more so than Leipzig). The shopping centres were really good (both the Prole and the Bourgeois ones), and there was always a place to eat or get a coffee close by. I visited a couple of bars and had dinner at a couple of good places, but I’d like to spend more time getting to know the other nightlife. I’ll put it on the list for when I come back.

Highlight: Wow, it’s hard to pick one. I’m going to cheat and say the first day when I was sightseeing on the tour bus. We saw the most, the weather was good, and then to finish, we ate at the top of the Fernsehturm overlooking Berlin. Win.