Check if an URL has got http:// prefix

后端 未结 8 770
野的像风
野的像风 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 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.

提交回复
热议问题