在 Xcode/Objective C 中更正用户提交的 URL [英] Correcting user submitted URL in Xcode/Objective C

查看:62
本文介绍了在 Xcode/Objective C 中更正用户提交的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Xcode 中编写一个迷你浏览器,但是目前 UIWebView 只会加载包含 http://www 的 URL 用户使用 UITextField 提交他们的 URL,内容变成一个字符串.

I'm trying to programme a mini browser in Xcode however at the moment the UIWebView will only load URLs that include the http ://www The user submits their URL using a UITextField and the contents become a string.

我想知道是否有办法搜索提交的字符串并在需要的地方添加 http 或 www 或两者,或者格式化文本输入以便它自动检查是否使用了正确的地址.

I wondered if there was a way to either search the submitted string and add the http or www or both where required or format the text input so it automatically checks to see if the correct address is used.

谢谢

推荐答案

做这样的事情:

NSString *urlString = ... // the user entered URL string
if (![urlString hasPrefix:@"http://"]) {
    urlString = [@"http://" stringByAppendingString:urlString];
}

请注意,这只是帮助您入门的粗略建议.此代码不处理 URL 已具有前缀https://"或拼写错误(例如htp://")等情况.

Note that this is just a rough suggestion to get you started. This code doesn't handle cases such as the URL already having a prefix of "https://" or typos such as "htp://".

更好的方法可能是:

NSURL *url = [NSURL URLWithString:urlString];
NSString *scheme = [url scheme];
if (scheme.length == 0) {
    // The string has no scheme - add "http://"
} else {
    // check for valid schemes
}

这篇关于在 Xcode/Objective C 中更正用户提交的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆