
Before you sell or give away your iPhone, the first thing you need to pay attention is these personal data in your iPhone. What you need to do to protect these data? Follow these steps to protect your data and get your iPhone to its factory default state.
1. Back up iPhone to iTunes or iCloud
Needless to say, in order to keep your previous data, backup is the first job. If you regularly backup your iPhone via iTunes or iCloud, this will not take much time.
For iCloud backups, go to Settings > iCloud > Backup, and tap Back Up Now.

For iTunes Backup, connect your iPhone to your computer, fire up iTunes and select your device. Under the Backups tab, choose “This computer” and then click “Back Up Now”.

2. Sign out of iCloud
If you have setup iCloud, it controls quite a lot of data on the iPad/iPhone. In many cases, the moment you sign out of the iCloud account, a lot of data disappears from the iPad/iPhone so you don’t have to remove them manually.
Tap Settings > iCloud, scroll down, and tap Sign Out.

You might also repeat the process for Settings > iTunes & App Store where you will be logged in.

3. Reset all settings, Erase data
Then you need to clearing out things internally with the iOS itself. Go to Settings > General > Reset, then tap Erase All Content and Settings.

Tips:
- This will completely erase your device, including any credit or debit cards you added for Apple Pay and any photos, contacts, music, or apps. It will also turn off iCloud, iMessage, FaceTime, Game Center, and other services.
- If you're using iOS 7 or later and have Find My iPhone turned on, your Apple ID and password will be required. After you provide your password, the device will be erased and removed from your account so that the next owner can activate it.
- Your content won't be deleted from iCloud when you erase your device.
Notes: Don't manually delete contacts, calendars, reminders, documents, photo streams, or any other iCloud data while signed in to your iCloud account, or the content will also be deleted from the iCloud servers and all of your devices that are signed in to iCloud.
That's it. Your iPhone or iPad will reboot after a few minutes and all your personal data should be gone and again look like the day you pulled it out of the box.