Tag Archives: Objective-C

Objective-C – Convert NSString File Path to NSURL and Vice Versa

Convert NSString to NSURL

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
/* OR */
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

 

Convert NSURL to NSString

// The output string will have the file:// prefix
NSString *filePath= [fileURL absoluteString];

// The output string will have the file path only
NSString *filePath= [fileURL path];

 

Updated @ 2011-01-10: Thanks for the comments of Brandon and Antal.

iPhone – Configuring the AVAudioSession and Output Audio to iPhone Speaker

I am working on the AVAudioRecorder and AVAudioPlayer. By default, the output audio is routed to the receiver instead of the iPhone speaker. In order to fulfill my requirement, i have to configure the AVAudioSession in the AppDelegate.

There are 2 ways to route the audio output to speaker.

  • Overriding the output audio route
  • Changing the default output audio route (iPhone OS 3.1 or above)

Continue reading iPhone – Configuring the AVAudioSession and Output Audio to iPhone Speaker

iPhone – Aligning UIToolbar Items on Code Level

In Xcode, If you want to add items to the UIToolbar, you need to create an NSArray which stores all the items and put them to the UIToolbar using the setItems method.

If you want to align one items on the left and the another one on the right. you need to create a space item in between the two button in the array. That’s just like adding a Flexible Space Bar Button Item in Interface Builder.
Continue reading iPhone – Aligning UIToolbar Items on Code Level

iPhone – UIScrollView with Paging Example

I have downloaded the PageControl example from developer.apple.com but i don’t know the reason why i cannot open the .xib in Interface Builder. Anyway, i cannot make use of this example for my purpose as i don’t how to remove the UIPageControl without using the Interface Builder.

So i follow the example to create a simple UIScrollView with paging feature by scrolling the screen in horizontal direction.

GitHub:ScrollViewWithPaging – Xcode project Continue reading iPhone – UIScrollView with Paging Example

iPhone – Add the Return Key for UIKeyboardTypeNumberPad

Update @ 2012-11-10: This solution in this post is outdated. please refer to the comment made by Jonny for the solution in iOS 6.

When you try to use the UIKeyboardTypeNumberPad for a UITextField, you will release that there is no return key and so the UIKeyboardTypeNumberPad cannot be dismissed.

Luckily, i find out a very good article which talk about the solution of the missing return key. It makes use of the NSNotification and NSNotificationCenter such that a DONE button image will pasted on the bottom left of the UIKeyboardTypeNumberPad. Here is the code for returning from the number pad after editing a UITextField.

*Update on 2010-09-11: The approach does not work in iOS SDK >= 4.0. if your are using 4.0, you can take a look at iPhone – Add the Return Key for UIKeyboardTypeNumberPad in iPhone SDK 4.

Continue reading iPhone – Add the Return Key for UIKeyboardTypeNumberPad

Objective-C – Create a Static Class

Update @ 2012-08-07: As mentioned by Siby, the approach in this article may cause memory leak. Please try to use singleton as shown in the following link.
The Objective-C Singleton

 

Like Java, i would like to add a Static Class such that i could call static variables and static methods in any where.

In Objective-C, you can create a static variable using a static modifier.

For static method which is also known as class method, you can use the + sign instead of the – sign when declaring the method.

I have created a Utility class as follow.
Utility.h

@interface Utility : NSObject
+ (int)getNumber;
+ (void)setNumber:(int)number;
@end

 

Utility.m

#import "Utility.h"

@implementation Utility

static int number = 1;

+ (int)getNumber {
  return number;
}

+ (void)setNumber:(int)newNumber {
  number = newNumber;
}

+ (id)alloc {
  [NSException raise:@"Cannot be instantiated!" format:@"Static class 'ClassName' cannot be instantiated!"];
  return nil;
}

@end

 

Now i would like to get and set this static variable when loading HelloView. so I need to modify the HelloViewController.m.
HelloController.m

#import "Utility.h"

@implementation HelloController
...
- (void)viewDidLoad {
	[super viewDidLoad];
	NSLog(@"number = %d", [Utility getNumber]);
	[Utility setNumber:3];
	NSLog(@"number = %d", [Utility getNumber]);
}
...
@end

 

Run the application and check the Debugger Console.

2010-04-09 17:27:18.864 HelloWorld[10860:20b] number = 1
2010-04-09 17:27:18.874 HelloWorld[10860:20b] number = 3

 

Done =)

Update @ 2011-08-25: Add (id)alloc in Utility.m to avoid the class being instantiated. Thanks Liam. =)

iPhone – Disable the Cut/Copy/Paste Menu on UITextField

Update @ 2012-07-11: for ios >= 5.1 canPerformAction:(SEL)action withSender:(id)sender is not working anymore. Please refer to the following post for more information
StackOverflow – How do you REALLY remove Copy from UIMenuController

 

If you want to disable the Cut/Copy/Paste Menu, add the following piece of code in the implementation file of the view controller which containing the UITextField
Continue reading iPhone – Disable the Cut/Copy/Paste Menu on UITextField