<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.2.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLProtectionSpace/init(host:port:protocol:realm:authenticationMethod:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLProtectionSpace(im)initWithHost:port:protocol:realm:authenticationMethod:"
  },
  "title" : "init(host:port:protocol:realm:authenticationMethod:)"
}
-->

# init(host:port:protocol:realm:authenticationMethod:)

Creates a protection space object from the given host, port, protocol, realm, and authentication method.

```
init(host: String, port: Int, protocol: String?, realm: String?, authenticationMethod: String?)
```

## Parameters

`host`

The host name for the [`URLProtectionSpace`](/documentation/Foundation/URLProtectionSpace) object.

`port`

The port for the protection space object. If `port` is 0, the default port for the specified protocol is used, for example, port 80 for HTTP. Note that servers can, and do, treat these values differently.

`protocol`

The protocol for the protection space object. The value of `protocol` is equivalent to the scheme for a URL in the protection space, for example, “http”, “https”,  “ftp”, etc.

`realm`

A string indicating a protocol-specific subdivision of the host. `realm` may be `nil` if there is no specified realm or if the protocol doesn’t support realms.

`authenticationMethod`

The type of authentication to use. `authenticationMethod` should be set to one of the values in [NSURLProtectionSpace authentication method constants](/documentation/Foundation/nsurlprotectionspace-authentication-method-constants) or `nil` to use the default, [`NSURLAuthenticationMethodDefault`](/documentation/Foundation/NSURLAuthenticationMethodDefault).

## Return Value

A new protection space object, initialized with the given host, port, protocol, realm, and authentication method.

## Discussion

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)