Gheyb means for something to be hidden from one’s senses, and shahadah means openness and visibility. One thing can be gheyb for a person, while being shahadah for another. It depends on the individual’s existential boundaries and how much of the existential world they cover and encompass in knowledge. But keeping in mind the limited amount of encompassment of other than Allah (swt) and at the same time, His complete encompassment of everything, one can conclude that it is only Allah (swt) who knows of the unseen, because He is the only one to encompass everything in this existential world and nothing leaves His existential boundaries, and no one and thing can keep itself unknown and hidden from Him [even if it is of the unseen].
Therefore, He is the only one who has complete knowledge of both the gheyb and shahadah, and in reality, everything is considered shahadah to Him; no entity possesses such a level of knowledge. As for others, if they want to be aware of some of the unseen, it depends on their existential capacity and comprehension; they might become aware of some things while remaining totally uninformed or learned at a very low level regarding other secrets.
Of course, it must be noted that allocating knowledge of the unseen to Allah (swt) doesn’t mean that others can't learn of some of these secrets as a result of their worthiness and through Allah's (swt) teaching. This is a blessing that Allah (swt) bestows upon certain chosen servants of His, such as the prophets and apostles. This is why we see the prophets speaking of revelation, which is considered of the unseen, or we see them, foretelling future incidents that actually do take place at the time they have predicted.
Of course, it must be noted that allocating knowledge of the unseen to Allah (swt) doesn’t mean that others can't learn of some of these secrets as a result of their worthiness and through Allah's (swt) teaching. This is a blessing that Allah (swt) bestows upon certain chosen servants of His, such as the prophets and apostles. This is why we see the prophets speaking of revelation, which is considered of the unseen, or we see them, foretelling future incidents that actually do take place at the time they have predicted.
Keeping in mind what was said, one won't see any contradiction between verses and hadiths that say knowledge of the unseen only belongs to Allah (swt), and verses and hadiths that assert that the prophets and imams were also aware of the unseen. Harmony can be established between these two groups of verses and traditions like so:
1- When it is said that knowledge of the unseen only belongs to Allah (swt), it means that it originally belongs to Him and He is independent in possessing it, while others are dependent on Him and His teaching for learning from it.
2- It is only Allah (swt) who knows every detail of the unseen, while others can only become aware of some of it and only to some extent.
3- Allah (swt) is aware of all secrets of the unseen at the very moment, while others aren't aware right now, although it is possible for them to learn in the future and will only become aware if Allah (swt) wills and informs them. Imam Sadiq (as) has been narrated saying:
“Whenever the imam wishes to know something, Allah (swt) teaches him.”
As for verse 34 of surah Luqman, it says:
“Indeed the knowledge of the Hour is with Allah. He sends down the rain, and He knows what is in the wombs. No soul knows what it will earn tomorrow, and no soul knows in what land it will die. Indeed Allah is all-knowing, all-aware”.
There are two interpretations regarding this verse:
1- The five things mentioned in the verse, are all of the unseen that no one is aware of other than Allah (swt) and those whom he wishes to know. Sending down rain doesn’t just refer to its pouring down from the sky that weather forecasters can foretell, what is meant is precisely where, when and how much it is going to rain; such knowledge is only in Allah's (swt) possession and the only thing others can do is to try to guess each of them. The same goes for the fetus in the womb; what Allah (swt) means isn't that He only knows of its gender, what He is saying is that He is aware of everything pertaining to it: its gender, health, physical and spiritual traits and even its potentials and whether it will grow to be one of those who reaches salvation, or damnation. Clearly, such knowledge is only in Allah's (swt) possession. Now, if there are hadiths that say that the imams bear some of this knowledge, it is because there is no contradiction between having a portion of this knowledge through Allah's (swt) teaching, and the detailed knowledge of it belonging only to Allah (swt). Add to that the fact that they aren't independent in possessing this knowledge; it has been granted to them by Allah (swt) in an amount that He sees fit.
2- The other explanation of this verse is that in this verse, only the part that speaks of the time of the Day of Judgment is considered of the unseen knowledge, and Allah (swt) considers Himself to be the only one to know of it, and as the verse goes on, its tone changes saying that Allah (swt) sends rain and is aware of what is in the wombs of mothers, without mentioning anything about this knowledge only belonging to Him. On this basis, if in the future it becomes possible to precisely pinpoint where it is going to rain and when and its amount, there will be no contradiction with the verse. Also, in the fourth and fifth part of the verse, it has been stressed that no one knows of exactly where and when he/she will die. This not knowing is in exact accordance with the law of nature that says no one essentially knows of the unseen and future events and incidents. Nevertheless, this doesn’t disprove that Allah (swt) may sometimes inform some people of the future and as to where and when they will die.