Category Archives: Objective-C

Simulating Low Disk Space in iPhone Simulator

While testing my iPhone application, I realized there is no way to test out my worst-case scenario, when there is no disk space. How do we do that? This StackOverflow post lays it out conceptually:

I bet you could also create your own .dmg file with file system of size … say 2Mb and write to it. If this works, then it is super-easy for testing – you just mount it and switch the path for testing. If the dmg is small enough, you could probably even upload it to the source control.

Let’s do this “in practice”.

Continue reading

ASIHTTPRequest vs. NSURLRequest

Support for one of my favorite libraries, ASIHTTPRequest, has been discontinued. I have been slowly but surely making a transition from ASIHTTPRequest (back) to NSURLRequest in some of my projects. I picked ASIHTTPRequest over NSURLRequest because NSURLRequest is verbose. Extremely verbose. ASI also has queueing control, throttling control and much, much more. These are are just some of the things that made ASIHTTPRequest such a pleasure to work with. Alas, I am stuck with NSURLRequest again…

Anyway, one of the more interesting aspects of connection handling is session control. I rely on the backend server to set a session cookie when I log in, and to keep that cookie alive while I do other stuff in the program. But in particular, when I transitioned the login screen to NSURLRequest, I noticed that, despite logging in successfully, my program would think that I’m NOT logged in, in the parts where ASIHTTPRequest is still used. It looks like ASIHTTPRequest is blind to the session cookies that are set using NSURLRequest.

I should also mention that I’m using CodeIgniter on the back end for session control, but in theory it shouldn’t matter (spoiler alert: but it does).

Let’s line up the usual suspects and try to figure out why this happens.

The Usual Suspects
Continue reading

UINavigationController and Rotation

Recently working on a project that needs a video displayed from a table view inside of a UINavigationController stack. The video needs to be in Landscape mode, while the rest of the project can be in Portrait or Landscape. The view controllers are pushed onto the stack, and each of the view controllers overrode the shouldAutorotateToInterfaceOrientation: method. The landscape controller had return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);; the rest of the controllers simply returned YES. So far, so good.

Here’s the problem. The view controller that contained the video (aka VideoViewController) arranged its elements in landscape mode, but started in potrait mode. Rotating it to Landscape mode corrected the issue, then rotating it back to Portrait mode, it still looked ok:

I could not make this work using the pushViewController method on the root view controller’s navigationController. But, for some strange reason, presentModalViewController: presented this view in the correct orientation. So the table view gets pushed onto the NavigationController with pushViewController and the VideoViewController gets presented as a modal view. The interesting thing about it is if you specify YES for the animated parameter, it doesn’t look like a modal view controller — meaning that it doesn’t pop up from the bottom like normal modal VCs, but rather from the side, and doesn’t look unnatural.

This is only a workaround until I can figure out why a regular pushViewController call doesn’t rotate the view, and I was lucky enough that in this instance a modal controller worked out well.

Constants in Objective-C

If you’re a Java programmer developing on the iPhone platform, you’ve probably wondered about how to set constants in your programs. Undoubtedly, you have have come across the #define preprocessor macro, and maybe a few other methods, but I’m going to show you my approach to this problem using the singleton design pattern. Continue reading