Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Xcode/Objective C中更正用户提交的URL_Url_Uiwebview_Nsstring_Uitextfield - Fatal编程技术网

在Xcode/Objective C中更正用户提交的URL

在Xcode/Objective C中更正用户提交的URL,url,uiwebview,nsstring,uitextfield,Url,Uiwebview,Nsstring,Uitextfield,我试图在Xcode中编程一个迷你浏览器,但是目前UIWebView只加载包含http://www的URL,用户使用UITextField提交URL,内容变成字符串 我想知道是否有一种方法可以搜索提交的字符串并在需要时添加http或www或两者,或者格式化文本输入,以便它自动检查是否使用了正确的地址 谢谢这样做: NSString *urlString = ... // the user entered URL string if (![urlString hasPrefix:@"http://"

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

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


谢谢

这样做:

NSString *urlString = ... // the user entered URL string
if (![urlString hasPrefix:@"http://"]) {
    urlString = [@"http://" stringByAppendingString:urlString];
}
请注意,这只是一个粗略的建议,让你开始。此代码不处理诸如URL已具有前缀“https://”或键入“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
}

谢谢你/你能再解释一下你的第二个建议吗?我对这个很陌生。我想如果你有http而不是www,你就必须在一个特定的位置添加www?不需要“www”协议。你找到什么具体的解决办法了吗?像狩猎旅行一样得到绳子?