代码之家  ›  专栏  ›  技术社区  ›  matteodv

检查URL是否具有http://prefix

  •  16
  • matteodv  · 技术社区  · 15 年前


    在我的应用程序中,当用户添加对象时,还可以为此对象添加链接,然后可以在Web视图中打开该链接。
    我试图保存一个没有http://prefix的链接,然后在webview中打开它,但无法打开它!
    在WebView开始加载之前,是否有方法检查保存的URL是否具有http://prefix?如果还没有,我该如何向URL添加前缀?
    谢谢!

    8 回复  |  直到 8 年前
        1
  •  42
  •   Felix Lapalme    11 年前

    您可以使用 - (BOOL)hasPrefix:(NSString *)aString 方法来查看包含URL的nsstring是否以http://prefix开头,如果不是,则添加前缀。

    NSString *myURLString = @"www.google.com";
    NSURL *myURL;
    if ([myURLString.lowercaseString hasPrefix:@"http://"]) {
        myURL = [NSURL URLWithString:myURLString];
    } else {
        myURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@",myURLString]];
    }
    

    我现在不在我的Mac上,无法编译/测试这段代码,但我相信上面的代码可以工作。

        2
  •  15
  •   tc.    15 年前
    NSString * urlString = ...;
    NSURL * url = [NSURL URLWithString:urlString];
    if (![[url scheme] length])
    {
      url = [NSURL URLWithString:[@"http://" stringByAppendingString:urlString]];
    }
    
        3
  •  4
  •   anoop4real    9 年前

    我用swift编写了一个字符串扩展,以查看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.
    }
    
        4
  •  3
  •   Undo ptrk    12 年前

    我不确定是否有任何方法可以检查它,但您可以在代码中检查它。

    尝试使用

    NSRange range = [urlString rangeOfString:@"http://"];
    if (range.location != NSNotFound)
        // Add http://  
    
        5
  •  2
  •   shim    8 年前

    最好使用 scheme 上的属性 URL 对象:

    extension URL {
        var isHTTPScheme: Bool {
            return scheme?.contains("http") == true // or hasPrefix
        }
    }
    

    示例用法:

    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)
    }
    
        6
  •  1
  •   bobics    12 年前

    如果要检查“http://”,可能需要不区分大小写的搜索:

    // probably better to check for just http instead of http://
    NSRange prefixRange = 
        [temp rangeOfString:@"http" 
                    options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
    if (prefixRange.location == NSNotFound)
    

    尽管我认为根据您的情况,URL方案检查是一个更好的答案,因为根据您的用例,URL可以以http或https和其他前缀开头。

        7
  •  0
  •   JuJoDi    12 年前

    首先,您应该为nsurl:file>new file>objective-c类别创建一个新类别。你可以按照httpurlWithString的行来调用这个类别,将其设置为nsurl的类别,然后按Next并将其添加到你的目标中。然后在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
    

    在Web视图中打开链接只需

    NSString *urlString = @"www.google.com";
    NSURL *url = [NSURL HTTPURLFromString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView.mainFrame loadRequest:request];
    
        8
  •  0
  •   d0ping    8 年前

    您可以使用Scheme属性来签出它。例如。。。

    if ([yourURL.scheme isEqualToString:@"http"] || [yourURL.scheme isEqualToString:@"https"]) {
        ...
    }