The scholars have agreed that if the cure is not needed immediately like food then they refer to the following hadith “Verily Allah does not place a cure in something which he has made forbidden on you”
Some have taken this immediate need and made medicine like food, as both of them are essential for the continuation of life, this group have brought evidence which state that forbidden items used as medicine are permitted- as the prophet ﷺ permitted wearing silk for Abdur Rahman bin Awf and Al Zabeer bin Awam (RA) because they had itchiness and a skin condition, even though he forbid wearing silk.
And maybe this saying is closer to the origin of Isam which keeps the human life in mind in all its rulings and its commandments
The permission for taking medicine which contains a forbidden substance is allowed only in the following conditions:
1. There is a true danger to the health of the person if he doesn’t take this medicine
2. There is no other permissible medicine available which he can use instead of it
3. A muslim doctor has prescribed the medicine and you are confident in his experience and in his religion.
And from the medical authorities: There is no medical emergency which forces you to have something which is forbidden such as a medicine, but we suggest the principle as precautionary measure for the muslim where he may not have acces to anything except what is forbidden.