Skip to content

Commit d379c32

Browse files
authored
Merge pull request #708 from naithar/URLCredentials-NSCoding
2 parents af83051 + 7b571be commit d379c32

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

Docs/Status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ There is no _Complete_ status for test coverage because there are always additio
4747
|------------------------------|-----------------|---------------|--------------------------------------------------------------------------------------------------------------------|
4848
| `URLAuthenticationChallenge` | Unimplemented | None | |
4949
| `URLCache` | Unimplemented | None | |
50-
| `URLCredential` | Mostly Complete | Incomplete | `NSCoding` and `NSCopying` remain unimplemented |
50+
| `URLCredential` | Mostly Complete | Incomplete | `NSCopying` remains unimplemented |
5151
| `URLCredentialStorage` | Unimplemented | None | |
5252
| `NSURLError*` | Complete | N/A | |
5353
| `URLProtectionSpace` | Unimplemented | None | |

Foundation/NSURLCredential.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,32 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
6565
*/
6666

6767
public required init?(coder aDecoder: NSCoder) {
68-
NSUnimplemented()
68+
guard aDecoder.allowsKeyedCoding else {
69+
preconditionFailure("Unkeyed coding is unsupported.")
70+
}
71+
72+
func bridgeString(_ value: NSString) -> String? {
73+
return String._unconditionallyBridgeFromObjectiveC(value)
74+
}
75+
76+
let encodedUser = aDecoder.decodeObject(forKey: "NS._user") as! NSString
77+
self._user = bridgeString(encodedUser)!
78+
79+
let encodedPassword = aDecoder.decodeObject(forKey: "NS._password") as! NSString
80+
self._password = bridgeString(encodedPassword)!
81+
82+
let encodedPersistence = aDecoder.decodeObject(forKey: "NS._persistence") as! NSNumber
83+
self._persistence = Persistence(rawValue: encodedPersistence.uintValue)!
6984
}
7085

7186
open func encode(with aCoder: NSCoder) {
72-
NSUnimplemented()
87+
guard aCoder.allowsKeyedCoding else {
88+
preconditionFailure("Unkeyed coding is unsupported.")
89+
}
90+
91+
aCoder.encode(self._user._bridgeToObjectiveC(), forKey: "NS._user")
92+
aCoder.encode(self._password._bridgeToObjectiveC(), forKey: "NS._password")
93+
aCoder.encode(self._persistence.rawValue._bridgeToObjectiveC(), forKey: "NS._persistence")
7394
}
7495

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

108+
open override func isEqual(_ object: Any?) -> Bool {
109+
if let other = object as? URLCredential {
110+
return other === self
111+
|| (other._user == self._user
112+
&& other._password == self._password
113+
&& other._persistence == self._persistence)
114+
}
115+
116+
return false
117+
}
118+
87119
/*!
88120
@method persistence
89121
@abstract Determine whether this credential is or should be stored persistently

TestFoundation/TestNSURLCredential.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class TestNSURLCredential : XCTestCase {
2121
static var allTests: [(String, (TestNSURLCredential) -> () throws -> Void)] {
2222
return [
2323
("test_construction", test_construction),
24-
("test_copy", test_copy)
24+
("test_copy", test_copy),
25+
("test_NSCoding", test_NSCoding)
2526
]
2627
}
2728

@@ -39,4 +40,10 @@ class TestNSURLCredential : XCTestCase {
3940
let copy = credential.copy() as! URLCredential
4041
XCTAssertTrue(copy.isEqual(credential))
4142
}
43+
44+
func test_NSCoding() {
45+
let credentialA = URLCredential(user: "swiftUser", password: "swiftPassword", persistence: .forSession)
46+
let credentialB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: credentialA)) as! URLCredential
47+
XCTAssertEqual(credentialA, credentialB, "Archived then unarchived url credential must be equal.")
48+
}
4249
}

0 commit comments

Comments
 (0)