Last active
April 8, 2018 06:44
-
-
Save amosavian/6959baeaefd58760c5d8f7927d3ed091 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func correctFuckingResumeData(data: NSData?) -> NSData? { | |
let kResumeCurrentRequest = "NSURLSessionResumeCurrentRequest" | |
let kResumeOriginalRequest = "NSURLSessionResumeOriginalRequest" | |
guard let data = data, let resumeDictionary = (try? NSPropertyListSerialization.propertyListWithData(data, options: [.MutableContainersAndLeaves], format: nil)) as? NSMutableDictionary else { | |
return nil | |
} | |
resumeDictionary[kResumeCurrentRequest] = correctFuckingRequestData(resumeDictionary[kResumeCurrentRequest] as? NSData) | |
resumeDictionary[kResumeOriginalRequest] = correctFuckingRequestData(resumeDictionary[kResumeOriginalRequest] as? NSData) | |
let result = try? NSPropertyListSerialization.dataWithPropertyList(resumeDictionary, format: NSPropertyListFormat.XMLFormat_v1_0, options: NSPropertyListWriteOptions()) | |
return result | |
} | |
func correctFuckingRequestData(data: NSData?) -> NSData? { | |
guard let data = data else { | |
return nil | |
} | |
if NSKeyedUnarchiver.unarchiveObjectWithData(data) != nil { | |
return data | |
} | |
guard let archive = (try? NSPropertyListSerialization.propertyListWithData(data, options: [.MutableContainersAndLeaves], format: nil)) as? NSMutableDictionary else { | |
return nil | |
} | |
// Rectify weird __nsurlrequest_proto_props objects to $number pattern | |
var k = 0 | |
while archive["$objects"]?[1].objectForKey("$\(k)") != nil { | |
k += 1 | |
} | |
var i = 0 | |
while archive["$objects"]?[1].objectForKey("__nsurlrequest_proto_prop_obj_\(i)") != nil { | |
let arr = archive["$objects"] as? NSMutableArray | |
if let dic = arr?[1] as? NSMutableDictionary, let obj = dic["__nsurlrequest_proto_prop_obj_\(i)"] { | |
dic.setObject(obj, forKey: "$\(i + k)") | |
dic.removeObjectForKey("__nsurlrequest_proto_prop_obj_\(i)") | |
arr?[1] = dic | |
archive["$objects"] = arr | |
} | |
i += 1 | |
} | |
if archive["$objects"]?[1]["__nsurlrequest_proto_props"] != nil { | |
let arr = archive["$objects"] as? NSMutableArray | |
if let dic = arr?[1] as? NSMutableDictionary, let obj = dic["__nsurlrequest_proto_props"] { | |
dic.setObject(obj, forKey: "$\(i + k)") | |
dic.removeObjectForKey("__nsurlrequest_proto_props") | |
arr?[1] = dic | |
archive["$objects"] = arr | |
} | |
} | |
// Rectify weird "NSKeyedArchiveRootObjectKey" top key to NSKeyedArchiveRootObjectKey = "root" | |
if archive["$top"]?["NSKeyedArchiveRootObjectKey"] != nil { | |
archive["$top"]?.setObject(archive["$top"]?["NSKeyedArchiveRootObjectKey"], forKey: NSKeyedArchiveRootObjectKey) | |
archive["$top"]?.removeObjectForKey("NSKeyedArchiveRootObjectKey") | |
} | |
// Re-encode archived object | |
let result = try? NSPropertyListSerialization.dataWithPropertyList(archive, format: NSPropertyListFormat.BinaryFormat_v1_0, options: NSPropertyListWriteOptions()) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please check http://stackoverflow.com/questions/39346231/