I want to get web pages links from the user, and load them using UIWebView, but I don't want the user to have to specify the "http(s)://www." part. Like the known web browsers, they don't force you to write that part.
Asked
Active
Viewed 396 times
1 Answers
0
From the first answer here it looks like you can load a webpage without specifying http://
in the URL.
If you want to let the user do what he/she wants and then eventually add http(s)://
yourself (if it's missing), this can be easily done by checking whether the user's URL has a http://
(or https://
) at the start with String's .hasPrefix(_:)
, and add it if it's not there.

Community
- 1
- 1

Federico Zanetello
- 3,321
- 1
- 25
- 22
-
I saw the answer you referred to before I post my question, but it didn't work with swift 3 / xcode 8.3. the problem is that I don't know what address the user will enter so I don't know does it use http or https, I found a solution though, I check like you said, if it doesn't have the scheme, I add "http://" always, and it works even with sites that use https, I'm not sure whether this is the best solution, but it's working, thanks for your help – M.Serag Apr 05 '17 at 09:57
-
If you scroll a bit down you'll see one with written swift 3 :) – Federico Zanetello Apr 05 '17 at 09:59
-
I meant that webview.loadRequest() doesn't load a request having a url without "http(s)://" part. – M.Serag Apr 05 '17 at 10:03
-
1Oh, ok. So, as I've written in my answer, you just need to make a control of the url and add ``http://`` if it's not there yet :) – Federico Zanetello Apr 05 '17 at 10:05
-
yes, but this works only if I added "http://", but if I added "https://", sites that doesn't use the secure protocol won't load. I feel I'm not able to make my self clear :), thanks for the help anyway :). I also check with NSRegularExperssion "https?://", so I can check user input once. – M.Serag Apr 05 '17 at 10:34