It is very likely that this phrase originated in Wales around the mid-nineteenth century. But it were not the precise medicinal properties of the apple that were exalted. In Old English the word apple was used to describe any round fruit that grew on a tree. So, as they said in the old days:
eat fruit on going to bed and you'll keep the doctor from earning his bread.