Can a smartphone get viruses, and how do you prevent them?
Yes and no.
If you don't jailbreak your phone, and the apps you put on it come from either the Google Play store (Android) or App Store (Apple), then no. It isn't impossible, but it is so improbable that it might as well be impossible. Apple and Google curate their app stores to prevent bad things from happening to users' phones from an app they sell.
Curation doesn't 100% guarantee an app won't be malicious in some way, but we do know that Apple is very aggressive at addressing issues as soon as they are brought to its attention. We know this because it has happened. There were several apps that made it into the App Store that contained a virus. As soon as Apple realized what happened, they removed the apps. The actual number of iPhones on the planet that were infected was insignificant compared to the installed base.
Also, due to the way iOS is made, it is almost impossible that a virus on one iPhone would infect another. (I say almost because we should never take for granted the efforts of those who would spend their lives trying to undermine ours.)
If, however, you jailbreak your iPhone, or download apps from a non-trusted source, it is very possible (and some would say even likely) you will get a virus or malware on your phone.
Don't jailbreak, only download apps from the sources above, and your phone will be fine.
Also asked as
Can an iPhone get a Trojan virus?
If this helped you, consider buying me a slice of pizza to help keep the site running. If this solved a big problem for you, consider buying me the whole pie!
Need help with this?
I help people in New Orleans and online with Apple gear, websites and more. Contact me or see more answers in the Knowledgebase.