The story
Of British origin; a similar proverb was recorded from Pembrokeshire, Wales, in 1866: “Eat an apple on going to bed, / And you’ll keep the doctor from earning his bread.” However, the idea that apples can cure disease is much older. In the play The Soddered Citizen (first performed c. 1631–1633) by the English author John Clavell (1601–1643), it is suggested that if a person who had heartburn had taken an apple and gone to bed, he would have been cured.
Etymology from Wiktionary, CC BY-SA 4.0.