开发者

Cocoa Webkit visit url without typing http://?

Is there a way to visit a 开发者_JAVA百科website without typing http://?


If you're looking to duplicate Safari's "protocol guessing" or even "TLD guessing", you'll have to implement this yourself.

From a user's perspective, you usually don't have to enter the protocol into a web browser because it's safe to assume the protocol is "HTTP". From the developer's perspective, you'll need to specify the protocol when forming the URL.

Since you tagged this question "webkit", I'll assume you're working with a WebView. From the documentation on -[WebView setMainFrameURL:]:

This method is functionally equivalent
to invoking [[webView mainFrame]
loadRequest:[NSURLRequest
requestWithURL: [NSURL
URLWithString:URLString]]].

Note, the poorly-named -setMainFrameURL: method accepts an NSString, not an NSURL as you'd expect. So considering this, you must still provide a valid URL string in order to get a valid URL.

In the case you mentioned in your comment, however, it's important to note http://example.com is as valid as http://www.example.com. This is handled by the target domain's name server and has nothing to do with whether the URL is valid or not. This is because example.com likely still resolves to an actual host (i.e. the record for the domain without a host forwards to www). Some domains don't do this, so you may want to handle this case (invalid host name) separately by trying to prepend www if absent. The important thing is valid URL is not valid domain.

All this considered, you'll have to roll your own "guessing" solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜