Archive for the ‘iPhone’ Category

Handling dates in objective-c can be quite difficult if you don’t use the right tools.

And the best tools are for sure the NSDateComponents class. This class is available to both Mac and iOS development, and it makes handling dates much more simple.

Suppose that you need to get the month number of a date. If you don’t know NSDateComponents, a way to do that is create a NSDateFormatter and set the format only for the month and get a string that you could convert to a number. Simple  like that. Actually this is far from simple, and far from optimal and far from good programming practices.



Read Full Post »

Better Logging in iOS

I found this info in stackoverflow.com. The tip comes from Diederik Hoogenboom.

To better handle logging in your application you might want to use these macros:

#ifdef DEBUG

#   define DLog(fmt, …) NSLog((@“%s [Line %d] “ fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);


#   define DLog(…)


// ALog always displays output regardless of the DEBUG setting

#define ALog(fmt, …) NSLog((@“%s [Line %d] “ fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);

I put these macros in a LOG.h file and add it to the .pch file located in the Other Sources folder in xCode, with an

#import “LOG.h”

To enable the DLog function just add the -DDEBUG flag in the “Other C Flags” option in your project’s configuration.

Just to remember you, the ALog will always print what you pass to it.

An example of a line from Dlog(@”Hello world”):

-[YourController yourMethod] [Line 100] Hello world


Read Full Post »

There are no accessors to the buttons and subviews of the UISearchBar public available by default, but we can access them thought the subviews method of the searchBar.

But why this is cool? Imagine you design a view and want a search bar on it. In order to go back from the search, or you need navigation bar with a back button, or you will need to create a back button for yourself in some place. But wait, the search bar can show a “cancel” button, can’t it? So you foward to use the cancel button and you realize that it comes disable until the user write something in the textfield. And we don’t want our users to need to type a letter in order to leave the search page. So with this trick, it’s pretty simple to enable the cancel button, even when the user typed nothing.

Put this code in the viewDidLoad method, or any place you prefer.

for (UIView *possibleButton in searchBar.subviews) {

if ([possibleButton isKindOfClass:[UIButton class]]){

UIButton *cancelButton = (UIButton*)possibleButton;

cancelButton.enabled = YES;




Now you are done. Your cancel button will be available to the user even if he didn’t type any character.

Then yon can implement this method:

– (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {

[self dismissModalViewControllerAnimated:YES];


and dismiss the view controller or anything you want. You can even change the text of the button with the method setTitle:ForState: of the button.

[cancelButton setTitle:@”Back” forState:UIControlStateNormal];


Read Full Post »

It’s pretty simple to use the camera (front, rear and camera albums) in an iphone application.

To get started just create an instance of UIImagePickerController with:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

After you need to decide the source of the image you want to capture. There are these possible values:

  • UIImagePickerControllerSourceTypeCamera – this will get the image from the camera.
  • UIImagePickerControllerSourceTypePhotoLibrary – this will open the album list of the photos app.
  • UIImagePickerControllerSourceTypeSavedPhotosAlbum – this will open the camera roll

you can specify which one you want with:

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

You can also specify if you want the user to be able to edit the image (with cropping) with the property:

picker.allowsImageEditing = YES;

After you need to set a delegate to the controller. This delegate must implement the UIImagePickerControllerDelegate protocol at least this method:

  • – (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

this method will receive a dictionary containing some info about the chosen picture. The original image selected (without editing) will be under the key “UIImagePickerControllerOriginalImage”, the edited image (if you allowed the user to edit) will be under the key “UIImagePickerControllerEditedImage”. These properties are both instances of UIImage. You can even get the selected frame if the user edited the picture, with the property “UIImagePickerControllerCropRect”. This will be an instance of CGRect.

Finally to present the view controller, just invoke in your view controller:

[self presentModalViewController:picker animated:YES];

One last thing that is worth mentioning is that you can verify if your device has a camera before allowing him to use it.

To test if the user has a camera device just use this method (that returns a boolean):

[UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]

You can also query the user about the front camera with the parameter: UIImagePickerControllerCameraDeviceFront.

Knowing that a user does not even has a camera, may help you prevent a broken experience to the user.

Well, that’s all folks, at least for now.

Hope you enjoyed this tutorial.


Read Full Post »

I’m developing a new comics reader, much alike the one from comixology (marvel and DC). Now I already have almost the same effect from the comixology reader, but I’m planning to add some extra features.

The first other feature is a much more adaptable and interactive magazine store. Custom backgrounds, user defined backgrounds, also background music and music control, video and news subscriptions. For now it is already working the custom background.

Also to create the comics there is already a prototype app that make it so much easy, fast and simple to create te comic.

Hope you enjoyed this video. I will upload the magazine creator video soon.


Read Full Post »

Most of the time we see in tutorials, code examples and even apple starter project our view controller being the delegate and the datasource of some tableview. Although I’m not saying this approach is wrong, it is not the best for every situation, and even in the worst cases we most of the time use this approach. As an example, if our viewcontroller has two tableviews, generally we see a lot of if code to control the logic for each tableview. Even, when we got only one tableview, but this table can be ordered, sorted, searched, and displayed in different formats, our code gets full of if codes. This can lead to a huge amount of bugs, work-arounds and finally to an confused and impossible code to maintain.

In those cases we can always use our good OOP (object orientation programming) knowledge.

Objective-C has a very good design pattern called “Delegate”. In this pattern you delegate some functions to another class to execute. In IOS development we use delegates all the time. But the delegate of a tableview does not necessary need to be the viewcontroller container of the tableview. We can create other NSObject classes to be the delegate for a tableview.

“But what kind of benefits do i get doing that?” you must be thinking. Well, using this pattern you can create a different delegate for each state of your table.

In in the state1 your tableview does not have sections and your cells should only display text, you create a delegate for that.

And if in the state2 your tableview should have sections, and each row should display also a detail for each row, you don’t need to create a lot of ifs. Just create another delegate, assign it as the new delegate for you tableview, and call the reloadData message of your tableview. Ad you are done.

Here goes an example of two datasources delegators:


Read Full Post »

Often i need to use some image as a background for many visual components, like views, scrollviews, buttons, other images… and most of time i ended using a large enough image to cover the front image.

So, if my view was 1000×700 i needed to load an 1000×700 image for the background. But this image will eat a lot of memory just to be there as a background. There should be a better way to do this. And there is a better way to do this.

All you have to do is create your UIImage like this

UIImage* backgroundImage = [[UIImage imageNamed:@”stretchableImage.png”] stretchableImageWithLeftCapWidth:44 topCapHeight:45];

this stretchableImageWithLeftCapWidth specify how many pixel in the left must be kept. The same goes to the topCapHeight, and in this case it specifies the number of pixels in the top.

So in the example, the sdk will keep the first 44 pixels, copy the 45th along all the way of the image, and in the end keep again the last 44 pixels. It will do the same for the height.

So you wont need to keep a large image in memory. And you can also use this technique to create dynamic buttons, views and anything that you may even not know how length it is.

Here you can find an image as example (but keep in mind that as a designer, i’m a good programmer ^_^” )

Visit my tumblr for more iphone tips and tricks

Read Full Post »

Older Posts »