"After he has honored me and has sent me against the nations that have plundered you -- for whoever touches you touches the apple of his eye -- I will surely raise my hand against them so that their slaves will plunder them."
    Zechariah 2:8-9

The first recorded example can be found in the works of Alfred the Great at the end of the ninth century. The ancients’ idea that the eye's pupil is apple-shaped and that eyes are particularly precious, appears in the Bible as many as six times. The Old Testament uses it as a reference to God's great love for His children.

Just as one's eyesight is cherished, so is the person or thing described as the apple of one's eye. "In old English the eye’s pupil was known as the apple," writes James Rogers in The Dictionary of Clichés (1985), "because it was thought to be spherical and solid. Since the pupil is a crucial and indispensable portion of the eye, it serves as a symbol of something cherished. An example in the Coverdale Bible of 1535 (Zechariah II, 8) is: ‘Who so toucheth you, shal touche the aple of his owne eye.’ The expression also appears in Deuteronomy XXXII, 10 as part of a song spoken by Moses: He found him in a desert land, and in the howling waste of the wilderness; he encircled him, he cared for him, he kept him as the aple of his eye.”

Robert Hendrickson also tells in Facts on File (1997) from “Encyclopedia of Word and Phrase Origins” :

    “That which one holds dearest, as in ‘You’re the apple of my eye.’ The phrase is from the Bible (Deut. 32:10), which says the Lord kept Israel ‘as the apple of his eye.’ ‘Pupillam,’ or pupil, is actually the Latin for the ‘apple’ of the phrase, but English translation of the Bible used ‘apple’ because this was the early word for the pupil of the eye, which was thought to be a solid apple-shaped body. Because it is so essential to sight, the eye’s apple, or pupil, is to be cherished and protected and ‘the apple of one’s eye’ came to mean anything extremely precious. The literal translation of the Hebrew phrase, incidentally, is ‘You are as the little man in the eye’ (one’s own reflection in the pupil of another’s eye).”
Mentioned in Proverbs 7 to as in, "keep my teachings as the apple of my eye" and as a loving request in Psalm 17 "Keep me as the apple of the eye, hide me under the shadow of thy wings."

Saint Patrick applies the Biblical meanings too in his confession. Kidnapped and sold into slavery in Ireland as a teen, he escapes then later returned as a missionary after a prophetic dream around 450 AD. He wrote:

    Therefore, on that day when I was rebuked, as I have just mentioned, I saw in a vision of the night a document before my face, without honour, and meanwhile I heard a divine prophecy, saying to me: 'We have seen with displeasure the face of the chosen one divested of (his good) name.' And he did not say 'You have seen with displeasure', but 'We have seen with displeasure' (as if He included Himself). He said then: 'He who touches you, touches the apple of my eye.'
As recently as Anglo-Saxon times, the same word, aeppel, meant both eye and apple. Shakespeare borrowed this metaphor using it in A Midsummer Night's Dream. He peers through the enchanted the eyes of lovers with faeris and managesto create so much mayhem. Oberon king of faeris, the brushes a flower across Demetrius’s eyes as he sleeps and fabricates this little sonnet:
    Flower of this purple dye,
    Hit with Cupid's archery,
    Sink in apple of his eye,
    When his love he doth espy,
    Let her shine as gloriously
    As the Venus of the sky.
    When thou wakest, if she be by,
    Beg of her for remedy.
The modern word pupil is from Latin, but it didn't appear in English until the sixteenth century. Pupus is Latin for boy and pupa for girl. Add to that the Roman diminutive iluss(a) for little boy or girl the dark circle of the eyes became pupillium since the tiny image reflected in the eye made everyone look puppets or childlike. Pupil is also the source for the other sense of pupil as in a schoolchild. A teacher's wish of course is to have their values reflected in their students and those who were prized most became the apple of the teacher's eye.

Today it has become a word play with the idea that one's beloved remains metaphorically in sight; as to hold something dear and precious most often as a favored son or daughter. Still going, strong jazz musician’s Fats Waller along with Joe Young wrote a song in 1932, as well as, Stevie Wonder in his easy breezy hit from 1972, You Are the Sunshine of My Life.