ios 检查 URL 是否有 http:// 前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3789126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if an URL has got http:// prefix
提问by matteodv
In my application, when the user add an object, can also add a link for this object and then the link can be opened in a webView.
I tried to save a link without http:// prefix, then open it in the webView but that can't open it!
Before webView starts loading, is there a method to check if the URL saved has got http:// prefix? And if it hasn't got it, how can I add the prefix to the URL?
Thanks!
在我的应用程序中,当用户添加一个对象时,也可以为这个对象添加一个链接,然后可以在一个 webView 中打开该链接。
我试图保存一个没有 http:// 前缀的链接,然后在 webView 中打开它,但无法打开它!
在 webView 开始加载之前,有没有办法检查保存的 URL 是否有 http:// 前缀?如果它没有得到它,我如何将前缀添加到 URL 中?
谢谢!
回答by Greg
You can use the - (BOOL)hasPrefix:(NSString *)aString
method on NSString to see if an NSString containing your URL starts with the http:// prefix, and if not add the prefix.
您可以使用- (BOOL)hasPrefix:(NSString *)aString
NSString 上的方法查看包含您的 URL 的 NSString 是否以 http:// 前缀开头,如果没有添加前缀。
NSString *myURLString = @"www.google.com";
NSURL *myURL;
if ([myURLString.lowercaseString hasPrefix:@"http://"]) {
myURL = [NSURL URLWithString:myURLString];
} else {
myURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@",myURLString]];
}
I'm currently away from my mac and can't compile/test this code, but I believe the above should work.
我目前不在我的 mac 上,无法编译/测试此代码,但我相信上述内容应该可行。
回答by tc.
NSString * urlString = ...;
NSURL * url = [NSURL URLWithString:urlString];
if (![[url scheme] length])
{
url = [NSURL URLWithString:[@"http://" stringByAppendingString:urlString]];
}
回答by anoop4real
I wrote an extension for String in Swift, to see if url string got http or https
我在 Swift 中为 String 写了一个扩展,看看 url 字符串是得到 http 还是 https
extension String{
func isValidForUrl()->Bool{
if(self.hasPrefix("http") || self.hasPrefix("https")){
return true
}
return false
}
}
if(urlString.isValidForUrl())
{
//Do the thing here.
}
回答by shim
Better to use the scheme
property on the URL
object:
最好scheme
在URL
对象上使用该属性:
extension URL {
var isHTTPScheme: Bool {
return scheme?.contains("http") == true // or hasPrefix
}
}
Example usage:
用法示例:
let myURL = https://stackoverflow.com/a/48835119/1032372
if myURL.isHTTPScheme {
// handle, e.g. open in-app browser:
present(SFSafariViewController(url: url), animated: true)
} else if UIApplication.shared.canOpenURL(myURL) {
UIApplication.shared.openURL(myURL)
}
回答by kthorat
I am not sure if there is any method to check that but you check it in the code.
我不确定是否有任何方法可以检查它,但您可以在代码中检查它。
try using
尝试使用
NSRange range = [urlString rangeOfString:@"http://"];
if (range.location != NSNotFound)
// Add http://
回答by bobics
If you're checking for "http://" you'll probably want case-insensitive search:
如果您正在检查“http://”,您可能需要不区分大小写的搜索:
// probably better to check for just http instead of http://
NSRange prefixRange =
[temp rangeOfString:@"http"
options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
if (prefixRange.location == NSNotFound)
Although I think the url scheme check is a better answer depending on your circumstances, as URLs can begin with http or https and other prefixes depending on what your use case is.
尽管我认为 url 方案检查是一个更好的答案,具体取决于您的情况,因为 URL 可以以 http 或 https 以及其他前缀开头,具体取决于您的用例是什么。
回答by JuJoDi
First, you should create a new category for NSURL: File > New File > Objective-C Category. You can call the category something along the lines of HTTPURLWithString, make it a category of NSURL, press next and add it to your target. Then in the NSURL+HTTPURLFromString.m implement the following message (and declare the message in your .h)
首先,您应该为 NSURL 创建一个新类别:File > New File > Objective-C Category。您可以按照 HTTPURLWithString 的方式调用该类别,使其成为 NSURL 的类别,然后按下一步并将其添加到您的目标中。然后在 NSURL+HTTPURLFromString.m 中实现以下消息(并在您的 .h 中声明该消息)
@implementation NSURL (HTTPURLFromString)
+(NSURL *)HTTPURLFromString:(NSString *)string
{
NSString *searchString = @"http";
NSRange prefixRange = [string rangeOfString:searchString options:(NSCaseInsensitiveSearch | NSAnchoredSearch)];
if (prefixRange.length == 4) {
return [NSURL URLWithString:string];
}
return [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", string]];
}
@end
To open a link in the WebView is simply
在 WebView 中打开链接很简单
NSString *urlString = @"www.google.com";
NSURL *url = [NSURL HTTPURLFromString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView.mainFrame loadRequest:request];
回答by d0ping
You can use scheme property for check it out. For example...
您可以使用方案属性进行检查。例如...
if ([yourURL.scheme isEqualToString:@"http"] || [yourURL.scheme isEqualToString:@"https"]) {
...
}