33

I have an NSString with an URL, in this way:

http://someurl.com/something

How would I get someurl.com only? I've already tried substringToIndex with no luck :(

Thanks in advance :)

pmerino
  • 5,900
  • 11
  • 57
  • 76

7 Answers7

100

Objective-C

NSString* urlString = @"http://someurl.com/something";
NSURL* url = [NSURL URLWithString:urlString];
NSString* domain = [url host];

Swift 2

var urlString = "http://someurl.com/something"
var url = NSURL(string: urlString)
var domain = url?.host

Swift 3+

var urlString = "http://someurl.com/something"
var url = URL(string: urlString)
var domain = url?.host
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
11

Use

[[NSURL URLWithString:@"http://someurl.com/something"] host]
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
6

Swift 4.2

I wrote extension for URL to take SLD. Seems, there is n

extension URL {
    /// second-level domain [SLD]
    ///
    /// i.e. `msk.ru, spb.ru`
    var SLD: String? {
        return host?.components(separatedBy: ".").suffix(2).joined(separator: ".")
    }
}
Alexey Lysenko
  • 1,890
  • 2
  • 12
  • 28
2

Swift 5

Extension to string, that gives you the domain name, while also removing "www."

extension String {
func StripURLMakeAttractive(_ url: String) -> String {
        
    let url = URL(string: url)
    guard let domain = url?.host else {return "error"}
    var modifiedDomain: String = ""
    
    var checkWWW: String = ""
    let checkIndexes = [0,1,2,3]

    for (index,char) in domain.enumerated() {
        if checkIndexes.contains(index){
            checkWWW.append(char)
        }
        if index > 3 && checkWWW.lowercased() == "www."  {
            modifiedDomain.append(char)
        }
    }
    
    if checkWWW.lowercased() == "www."{
        return modifiedDomain
    }
    else {
        return domain
    }
    
}

}

  • 1
    Works! I modified it to not accept any arguments and use self instead of the url param as I am calling this extension method using my URLString. – Binaya Thapa Magar May 21 '22 at 07:37
1

You should look at the host() method of the NSURL class.

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
Brian Lyttle
  • 14,558
  • 15
  • 68
  • 104
1

Swift 5.5

extension String {
    var onlyHost: String? {
        let url = URL(string: self)
        return url?.host
    }
}
Mamad Farrahi
  • 394
  • 1
  • 5
  • 20
  • Why an Extension to String? It doesnt't even reference `self`. Maybe you meant to write `func getHost() -> String?` without the extra String argument? – smat88dd Nov 17 '22 at 06:45
0

Swift 4

And if you are using Custom url schemes say for deeplinking, for eg:

myapp:homescreen

(and don't have the "forward slashes with host name" (//www.bbc)), one solution that worked for me to extract "homescreen" is by using the index method below and then pattern matching to scrape everything after ":"

let index = absoluteString.index(absoluteString.startIndex, offsetBy: 5)
String(absoluteString[index...])

url.host or absoluteURL.host is nil in this scenario

Naishta
  • 11,885
  • 4
  • 72
  • 54