In the US history, has a write-in candidate ever won a presidential election?
Technically, yes, a number of individuals have won presidential elections due to write-in campaigns, but these have always been due to the individual being a write-in in the presidential primaries.
For example, in the Republican primaries of Massachusetts in 1928, Herbert Hoover was not on the ballot for election candidates but instead was written in by over 100,000 voters, which aided in making him the republican candidate for presidential election, which he subsequently won. The same occurred with Richard Nixon, John F Kennedy, and Dwight Eisenhower in other regions and due to the same reasons. This practice often occurs mainly due to the candidate being unable to qualify for voting in these regions, but it is usually circumnavigated due to this rule.