By default, iPhone doesn’t require you to enter a passcode to unlock it.
Choose General > Passcode Lock and enter a 4-digit passcode, then enter the passcode again to verify it. iPhone then requires you to enter the passcode to unlock it or to display the passcode lock settings.
Choose General > Passcode Lock, enter your passcode, and tap Turn Passcode Off, then enter your passcode again.
Choose General > Passcode Lock, enter your passcode, and tap Change Passcode. Enter your passcode again, then enter and reenter your new passcode.
If you forget your passcode, you must restore the iPhone software. See Updating and Restoring iPhone Software.
Choose General > Passcode Lock and enter your passcode. Tap Require Passcode, then select how long iPhone can be idle before you need to enter a passcode to unlock it.
Choose General > Passcode Lock and turn Voice Dial on or off.
Choose General > Passcode Lock, enter your passcode, and tap Erase Data to turn it on.
After ten failed passcode attempts, your settings are reset to their defaults and all your information and media is erased:
On iPhone 3GS: by removing the encryption key to the data (which is encrypted using 256-bit AES encryption)
On iPhone and iPhone 3G: by overwriting the data
IMPORTANT: You can’t use iPhone while data is being overwritten. This can take up to two hours or more, depending on the model and storage capacity of your iPhone. (On iPhone 3GS, the removal of the encryption key is immediate.)