Saturday, June 22, 2013

Play system sound in iOS

Playing the system sound in iOS is quite easy.

  • Create a project and add AudioToolbox.framework reference. 
  • To play the system sound, call AudioServicesPlaySystemSound(1104) where "1104" is the system sound ID.
  • For the complete list of system sound, you may refers to http://iphonedevwiki.net/index.php/AudioServices
You might found the following sample code (somewhere in the Internet) which load the "Tock" sound from the system resources and plays it. But, the following code might not play the sound because you should not call "dispose" function immediately after "play" function as the play function will take some time to execute the "playing" process. This results in the soundID has been disposed/reset before it has been played. The correct way is to call the dispose function when you are unloading the view or app.

     NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
    
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
    AudioServicesPlaySystemSound(soundID);

    AudioServicesDisposeSystemSoundID(soundID);

Sunday, June 16, 2013

Shorthand to initialize NSDictionary

We use to initialize the NSDictionary variable like this:


        NSDictionary* f =[NSDictionary dictionaryWithObjectsAndKeys:@"a", @"CODE", nil];

Instead, you can write your code like this:
   
        NSDictionary* f =@{@"CODE": @"a"};

Note: don't forget the the key & value position in the above codes located differently.

Wednesday, June 12, 2013

Section in UITableViewController

In table view, you may create sections to group the cells. To do this, just follow the settings below:


UITableViewController settings

Declare a class that inherits from UITableViewCell and add a UILabel outlet. In CMyCell class, you may write some customized code. For example, when the view controller passing the data to the cell, the cell will display different controls based on the data.


Then, switch to the Storyboard, choose the"cell view" and set it's class name to "CMyCell".



Drag the label control to the "mainLabel" as shown below:



Sunday, June 2, 2013

Add the shared library into the workspace

As you are working on more and more project, you might wonder how to share the codes among these projects without have to copy the same class files. This can be done by creating a project with all your shared codes. Then, follow the steps below to add the shared project into the new project that you are working on.

Step 1: make sure that you have created a workspace in your project. This can be done by selecting Save As Workspace from File menu.

Step 2: right click on the project that you are working on and choose 'Add Files to "xxx"'. You will have to choose the "xxx.xcodeproj" file from the Finder.


Step 3: open up the project settings and goto Summary tab. Click on the "+" button as shown below.


Step 4: Choose your shared library from the Workspace.


Step 5: goto Buil Settings tab and search for "linker". Key in "-ObjC".


Step 6: You may continue to add new project references. Once you have done it, you might have to regroup the references into Frameworks. This step is optional.


Step 7: Finally, to use the classes that you have done in the shared project, you have to add the source code reference path. Goto Build Settings, search "search" text. Then, key in the shared project source code path in the "User Header Search Paths".


Tuesday, October 30, 2012

Compiling shared library project in workspace

When you a shared library project in a workspace and there are category functions, the compilation will fail. To overcome this problem, you need to add "-ObjC" in "Other Linker Flags" as shown below:



Monday, October 22, 2012

Showing drop down list

In iOS, there is not "combo box" or "drop down list" control as compare to Windows programming. But, there is a way to achieve this.

Below is the screen snapshot that tells you that yes, there is a way to show the drop down list when the user clicks on the "Choose.." button.



First, you have to add a table view controller to the Storyboard and set its' Identifier to "testTableView".


After that (in this new table view controller), you have to populate the data through the numberOfRowsInSection and cellForRowAtIndexPath method. Nothing special here.

In the didSelectRowAtIndexPath, you have to pass the user selection back to the "main view". "setSelection" is a method in the main view controller (the codes will be shown later).

- (void)tableView:(UITableView *)tableView
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* s = [self.data objectAtIndex:indexPath.row];
    [self.main_view setSelection:s];
}

In the viewDidLoad, you have to write some code to control the popover size.

    CGRect rect = [self.tableView rectForSection:0];
    self.contentSizeForViewInPopover = CGSizeMake(200,
                                                  rect.size.height + 20);

In the .h class of the main view, you have to add the UIPopoverControllerDelegate protocol and also a popover controller variable.

   @interface CViewController : UIViewController<UIPopoverControllerDelegate>
   @property(strong)UIPopoverController* popover_controller;

In the .m file of the main view, you have to add two method for the above protocol.

   - (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController{
        return YES;
   }

   - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
      //do cleanup here, if any. 
   }

Through the StoryBoard, select the main view and you have to add the button reference and handle the TouchDown (like click event in the Windows programming). The codes are shown as below:

- (IBAction)choose_btn_onclick:(id)sender {
    if(self.popover_controller == nil)
    {
        CTestViewController* vc =[self.storyboard instantiateViewControllerWithIdentifier:@"testTableView"];
        vc.main_view = self;
       
        UIPopoverController* popover= [[UIPopoverController alloc] initWithContentViewController:vc];
        popover.delegate = self;  
        self.popover_controller = popover;
    }
   
    CGRect popover_rect = [self.view convertRect:[choose_btn frame]
                                        fromView:[choose_btn superview]];

    [self.popover_controller presentPopoverFromRect:popover_rect
                                             inView:self.view
                           permittedArrowDirections:UIPopoverArrowDirectionAny
                                           animated:YES];
}

The popover controller will pass the user selection through this method:

-(void)setSelection:(NSString*)s
{
    self.choose_btn.titleLabel.text = s;
    [self.popover_controller dismissPopoverAnimated:YES];
}