Posts Tagged ‘hack’

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 »