Check if an URL has got http:// prefix

后端 未结 8 760
野的像风
野的像风 2021-02-07 08:45


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://

相关标签:
8条回答
  • 2021-02-07 08:55

    I wrote an extension for String in Swift, to see if url string got http or https

    extension String{
    
        func isValidForUrl()->Bool{
    
            if(self.hasPrefix("http") || self.hasPrefix("https")){
                return true
            }
            return false
        }
    }
    
    if(urlString.isValidForUrl())
        {
          //Do the thing here.
    }
    
    0 讨论(0)
  • 2021-02-07 09:01

    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)

    @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

    NSString *urlString = @"www.google.com";
    NSURL *url = [NSURL HTTPURLFromString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView.mainFrame loadRequest:request];
    
    0 讨论(0)
  • 2021-02-07 09:02

    You can use scheme property for check it out. For example...

    if ([yourURL.scheme isEqualToString:@"http"] || [yourURL.scheme isEqualToString:@"https"]) {
        ...
    } 
    
    0 讨论(0)
  • 2021-02-07 09:03

    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.

    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.

    0 讨论(0)
  • 2021-02-07 09:03

    If you're checking for "http://" you'll probably want case-insensitive search:

    // 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.

    0 讨论(0)
  • 2021-02-07 09:06
    NSString * urlString = ...;
    NSURL * url = [NSURL URLWithString:urlString];
    if (![[url scheme] length])
    {
      url = [NSURL URLWithString:[@"http://" stringByAppendingString:urlString]];
    }
    
    0 讨论(0)
提交回复
热议问题