Don’t use setAlternateColor: on UISwitch

March 3, 2011 § 1 Comment

According to Apple documentation, it is recommended not to change UISwitch.

By default UISwitch shows a blue button with “ON” label and white button with “OFF” label. API won’t allow you to change the font name, font size, font style, font color, button color on UISwitch.

Even though there are many examples on how to use setAlternateColor: method to change the default UISwitch button from blue color to orange color, this is not recommended. Your app will be rejected if you use it.

The UISwitch class is not customizable.

You would have to build your own custom switch. It is in general difficult to subclass UIControl and reimplement UISwitch. There are other work around approaches to recreate Switch using png images.

Sending email without and with HTML formatting in Objective-C

November 16, 2009 § Leave a comment

This is the standard of sending email in Objective-C iPhone App.

Use a mailto url + stringByAddingPercentEscapesUsingEncoding + HTML.

Simple Email without any sentences or any HTML formatting:

NSURL *url = [[NSURL alloc] initWithString;@”mailto:myname@gmail.com?subject=Hello&body=Hi”];
[[UIApplication sharedApplication] openURL:url];

Email with HTML formatting:

NSString *emailBody = @”<table>
<tr><td style=’text-align:right’><b>FirstName</b>:</td>
<td>Michael</td></tr><tr>
<td style=’text-align:right’><b>LastName</b>:</td><td>Jackson</td></tr>
<tr><td style=’text-align:right’>
<b>Job<b/>:</td><td>Artist</td></tr></table>”;
NSString *encodedBody =
[emailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString =
[NSString stringWithFormat:@”mailto:myname@gmail.com?subject=Hello&body=%@”, encodedBody];
NSURL *url = [[NSURL alloc] initWithString:urlString];
[[UIApplication sharedApplication] openURL:url];

iPhone simulator does not have a Mail.app. You can only email url in device. However, you can consider to use compiler statements to create specific simulator/device code. You can use UIAlertView to preview the email string. Consider using UITextView to preview the text and UIWebView to preview HTML in the simulator.

#if TARGETIPHONESIMULATOR
    //compiler specific code
#else
    // device specific code
#endif

Sending image in iPhone Mail App

November 16, 2009 § Leave a comment

Embedded image is the only way to include image within Mail App inside iPhone App.

No API is allowed to access Mail attachments
Mail URL does not support attachments

Other Email Image Options:
1) Create your build-in SMTP client
2) Send image to a 3rd party server and send the email from web server
3) Save image to Photo library where you can email your photo

Use <b> before and after <img> tag to avoid Gmail or Hotmail to strip your <img> image tag after sending email out from iphone

iPhone Objective-C Adding UITextView upon viewDidLoad

November 10, 2009 § 2 Comments

This is a simple approach to draw a UITextView, insert content from a text file within Xcode project, disable editing, and customize font style during loading app.

– (void)viewDidLoad {

[super viewDidLoad];
CGRect viewRect = CGRectMake(0.0, 0.0, 320.0, 420.0);
myTextView = [[UITextView alloc] initWithFrame:viewRect];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [documentsDirectory stringByAppendingPathComponent:@”sample.txt”];
NSString *filenameString = [NSString stringWithContentsOfFile:filename];
myTextView.text = filenameString;
myTextView.editable = NO;
myTextView.textAlignment = UITextAlignmentCenter;
UIFont *myFont = [UIFont fontWithName:@”Helvetica” size:10.0];
// add Font color
myTextView.font = myFont;
[self.view addSubview:myTextView];
}

Supported Audio Formats in Playback & Recording

July 7, 2009 § 2 Comments

Supported Audio Playback formats:

  • AAC
  • HE-AAC
  • AMR (Adaptive Multi-Rate, a format for speech)
  • ALAC (Apple Lossless)
  • iLBC (internet Low Bitrate Codec, another format for speech)
  • IMA4 (IMA/ADPCM)
  • linear PCM (uncompressed)
  • µ-law and a-law
  • MP3 (MPEG-1 audio layer 3

Supported Audio Recording Formats:

  • ALAC (Apple Lossless)
  • iLBC (internet Low Bitrate Codec, for speech)
  • IMA/ADPCM (IMA4)
  • linear PCM
  • µ-law and a-law

AAC, MP3, and ALAC (Apple Lossless) Playback for AAC, MP3, and ALAC sounds can use efficient hardware-based decoding on iPhone OS–based devices, but these codecs all share a single hardware path. The device can play only a single instance of one of these formats at a time through hardware. If the user is playing a sound in one of these three formats in the iPod application, then your application—to play along over that audio—will employ software decoding. AAC, ALAC, MP3 can employ hardware decoding for playback.

Linear PCM and IMA4 (IMA/ADPCM You can play multiple linear PCM or IMA4 sounds simultaneously in iPhone OS without incurring CPU resource problems. It is same for the AMR and iLBC speech-quality formats, and for the µ-law and a-law compressed formats.

The device can play only a single instance of one of these formats at a time through hardware. For example, if you are playing a stereo MP3 sound, a second simultaneous MP3 sound will use software decoding. Similarly, you cannot simultaneously play an AAC and an ALAC sound using hardware. If the iPod application is playing an AAC sound in the background, your application plays AAC, ALAC, and MP3 audio using software decoding.

To play multiple sounds with best performance, or to efficiently play sounds while the iPod is playing in the background, use linear PCM (uncompressed) or IMA4 (compressed) audio.

Measurement of Audio Latency

July 7, 2009 § Leave a comment

Latency = buffer duration + inherent hardware latency

In other words, latency is to get the last sample of audio in its buffer out of the headphones or speaker.

Reference here.

Audio References:
Subfruther.com
Michael Tyson

Why you should use CAF instead of AIF & WAV?

July 7, 2009 § 2 Comments

iPhone OS have a native audio file format, the Core Audio Format (or CAF) file format. It is available in iphone OS 2.0 +. it can contain any audio data format supported on a platform. CAF files have no size restrictions—unlike .aif and .wav files—and can support a wide range of metadata, such as channel information and text annotations.

This is command line to convert audio into Little-Endian 16-it 44,100 sample rate format in .caf.

/usr/bin/afconvert -f caff -d LEI16@44100 inputSoundFile.aiff outputSoundFile.caf

It seems like Audio Toolbox methods handle multiple formats. However, if you use the audio in the right format then iphone won’t have to do it at play time.

Audio Toolkit is fine if you have a button and simple UI interaction. It doesn’t play immediately. You can’t match specific frame with specific sound effects. It is late by many frames or the entire audio will pause and wait for audio toolbox to load the sound into the buffer. It is just not good for game or music app.

SDK 3.0 Upgrades on AVFoundation Framework

July 7, 2009 § Leave a comment

SDK 3.0 has added the following APIs in AVFoundation framework

  • AVAudioRecorder.h
  • AVAudioSession.h
  • AVAudioSettings.h

Existing APIs in SDK 2.2.1:

  • AVAudioPlayer.h
  • AVFoundation.h

Upgrade in Utility App from SDK 2.2.1 to SDK 3.0

July 7, 2009 § 1 Comment

In SDK 2.2.1, infoButton is just a UIButton that is controlled by the toggleView: method in RootViewController.h.

SDK 3.0 reveals a new architecture. RootViewController .h & .m files are eliminated from the directory of “Application Delegate”. <FlipsideViewControllerDelegate> and showInfo: method are added to the MainViewConroller .h file.

New architecture in SDK 3.0 is a better implementation because it eliminates the complexity of RootViewController. It is complicated to pass data between MainViewController and FlipsideViewController via the middleman, RootViewController.

In SDK 2.2.1 RootViewController.m:

#import <UIKit/UIKit.h>
@class MainViewController;
@class FlipsideViewController;
@interface RootViewController : UIViewController {
UIButton *infoButton;
MainViewController *mainViewController;
FlipsideViewController *flipsideViewController;
UINavigationBar *flipsideNavigationBar;
}
@property (nonatomic, retain) IBOutlet UIButton *infoButton;
@property (nonatomic, retain) MainViewController *mainViewController;
@property (nonatomic, retain) UINavigationBar *flipsideNavigationBar;
@property (nonatomic, retain) FlipsideViewController *flipsideViewController;
– (IBAction)toggleView;
@end

In RootViewController.m:

– (IBAction)toggleView {
/*
This method is called when the info or Done button is pressed.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
if (flipsideViewController == nil) {
[self loadFlipsideViewController];
}
UIView *mainView = mainViewController.view;
UIView *flipsideView = flipsideViewController.view;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
if ([mainView superview] != nil) {
[flipsideViewController viewWillAppear:YES];
[mainViewController viewWillDisappear:YES];
[mainView removeFromSuperview];
[infoButton removeFromSuperview];
[self.view addSubview:flipsideView];
[self.view insertSubview:flipsideNavigationBar aboveSubview:flipsideView];
[mainViewController viewDidDisappear:YES];
[flipsideViewController viewDidAppear:YES];
} else {
[mainViewController viewWillAppear:YES];
[flipsideViewController viewWillDisappear:YES];
[flipsideView removeFromSuperview];
[flipsideNavigationBar removeFromSuperview];
[self.view addSubview:mainView];
[self.view insertSubview:infoButton aboveSubview:mainViewController.view];
[flipsideViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
}
[UIView commitAnimations];
}

In SDK 3.0 MainViewController.h:

#import “FlipsideViewController.h”
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
}
– (IBAction)showInfo;
@end

In MainViewController.m:
– (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
– (IBAction)showInfo {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@”FlipsideView” bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}

Objective-C Audio experiment

July 5, 2009 § Leave a comment

I couldn’t find any good information on Audio performance and quality. I am sharing my experiement here.

System Audio Services, declared in AudioToolbox/AudioServices.h only supports .caf, .aif, or .wav files. The audio data in the file must be in PCM or IMA/ADPCM (IMA4) format.  The file’s audio duration must be less than 30 seconds.

AVAudioPlayer supports .m4a, .mp3, .wav, .aif, and .caf.

Simulator is not a reliable tool to test and verify audio performance and quality.

  • AudioServicePlaySystemSound doesn’t play 128 bitrate 44,100 sampling rate wave, aif and caf audio files on simulator even it plays on device
  • Simulator won’t be able to reproduce the speaker sound
  • Simulator can’t tell the audio performance between AudioServicePlaySystemSound & AVAudioPlayer

Using the same .wav audio file, by default, AVAudioPlayer is much much louder than AudioServicePlaySystemSound on device. Both sound the same on simulator. I suspect iphone use different hardware mechanism to play AudioServicePlaySystemSound and AVAudioPlayer.

I don’t notice much differences in audio quality among audio formats in .wav, .aif, .caf, .mp3, .m4a in device & simulator. However for the same audio file, the file sizes are much smaller in m4a (44KB), and mp3 (96MB), compared to wav (1MB), AIF (1MB), and CAF (1MB). File sizes do not correspond to rendering time based on profiling on simulator.

Where Am I?

You are currently browsing the Product Design category at Web Builders.