Skip to content

Implementation of NSCoding for URLCredential #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ There is no _Complete_ status for test coverage because there are always additio
|------------------------------|-----------------|---------------|--------------------------------------------------------------------------------------------------------------------|
| `URLAuthenticationChallenge` | Unimplemented | None | |
| `URLCache` | Unimplemented | None | |
| `URLCredential` | Mostly Complete | Incomplete | `NSCoding` and `NSCopying` remain unimplemented |
| `URLCredential` | Mostly Complete | Incomplete | `NSCopying` remains unimplemented |
| `URLCredentialStorage` | Unimplemented | None | |
| `NSURLError*` | Complete | N/A | |
| `URLProtectionSpace` | Unimplemented | None | |
Expand Down
36 changes: 34 additions & 2 deletions Foundation/NSURLCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,32 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
*/

public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

func bridgeString(_ value: NSString) -> String? {
return String._unconditionallyBridgeFromObjectiveC(value)
}

let encodedUser = aDecoder.decodeObject(forKey: "NS._user") as! NSString
self._user = bridgeString(encodedUser)!

let encodedPassword = aDecoder.decodeObject(forKey: "NS._password") as! NSString
self._password = bridgeString(encodedPassword)!

let encodedPersistence = aDecoder.decodeObject(forKey: "NS._persistence") as! NSNumber
self._persistence = Persistence(rawValue: encodedPersistence.uintValue)!
}

open func encode(with aCoder: NSCoder) {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

aCoder.encode(self._user._bridgeToObjectiveC(), forKey: "NS._user")
aCoder.encode(self._password._bridgeToObjectiveC(), forKey: "NS._password")
aCoder.encode(self._persistence.rawValue._bridgeToObjectiveC(), forKey: "NS._persistence")
}

static public var supportsSecureCoding: Bool {
Expand All @@ -84,6 +105,17 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
return self
}

open override func isEqual(_ object: Any?) -> Bool {
if let other = object as? URLCredential {
return other === self
|| (other._user == self._user
&& other._password == self._password
&& other._persistence == self._persistence)
}

return false
}

/*!
@method persistence
@abstract Determine whether this credential is or should be stored persistently
Expand Down
9 changes: 8 additions & 1 deletion TestFoundation/TestNSURLCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class TestNSURLCredential : XCTestCase {
static var allTests: [(String, (TestNSURLCredential) -> () throws -> Void)] {
return [
("test_construction", test_construction),
("test_copy", test_copy)
("test_copy", test_copy),
("test_NSCoding", test_NSCoding)
]
}

Expand All @@ -39,4 +40,10 @@ class TestNSURLCredential : XCTestCase {
let copy = credential.copy() as! URLCredential
XCTAssertTrue(copy.isEqual(credential))
}

func test_NSCoding() {
let credentialA = URLCredential(user: "swiftUser", password: "swiftPassword", persistence: .forSession)
let credentialB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: credentialA)) as! URLCredential
XCTAssertEqual(credentialA, credentialB, "Archived then unarchived url credential must be equal.")
}
}