How can I convert my device token (NSData) into an NSString?

后端 未结 30 1687
挽巷
挽巷 2020-11-28 18:31

I am implementing push notifications. I\'d like to save my APNS Token as a String.

- (void)application:(UIApplication *)application
didRegisterForRemoteNotif         


        
相关标签:
30条回答
  • 2020-11-28 19:10

    If anyone is looking for a way to do this in Swift:

    Swift 3 introduces the Data type, with value semantics. To convert the deviceToken to a String, you can do as follows:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print(token)
    }
    

    Old answer using NSData:

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
        var tokenString = ""
    
        for i in 0..<deviceToken.length {
            tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
        }
    
        print("tokenString: \(tokenString)")
    }
    
    0 讨论(0)
  • 2020-11-28 19:10

    In iOS 13 description will break so use this

    let deviceTokenString = deviceToken.map { String(format: "%02x", $0) }.joined()
    

    For clarity, let’s break this down and explain each part:

    The map method operates on each element of a sequence. Because Data is a sequence of bytes in Swift, the passed closure is evaluated for each byte in deviceToken. The String(format:) initializer evaluates each byte in the data (represented by the anonymous parameter $0) using the %02x format specifier, to produce a zero-padded, 2-digit hexadecimal representation of the byte / 8-bit integer. After collecting each byte representation created by the map method, joined() concatenates each element into a single string.

    P.S don't use description gives different string in iOS 12 and iOS 13 and not safe as per future scope. Developers shouldn’t have relied on a specific format for an object’s description.

    // iOS 12
    (deviceToken as NSData).description // "<965b251c 6cb1926d e3cb366f dfb16ffffd e6b9086a 8a3cac9e 5f857679 376eab7C>"
    
    // iOS 13
    (deviceToken as NSData).description // "{length = 32, bytes = 0x965b251c 6cb1926d e3cb366f dfb16ffffd ... 5f857679 376eab7c }"
    

    For more information read This.

    0 讨论(0)
  • 2020-11-28 19:10

    Using updateAccumulatingResult is more efficient than the various other approaches found here, so here's the Swiftiest way to stringify your Data bytes:

    func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.reduce(into: "") { $0 += String(format: "%.2x", $1) }
        print(token)
    }
    
    0 讨论(0)
  • 2020-11-28 19:10
    NSString *tokenString = [[newDeviceToken description] stringByReplacingOccurrencesOfString:@"[<> ]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [[newDeviceToken description] length])];
    
    0 讨论(0)
  • 2020-11-28 19:10

    The solution @kulss posted here, while lacking in elegance but having the virtue of simplicity no longer works in iOS 13, since description will work differently for NSData. You can still use debugDescription though.

    NSString * deviceTokenString = [[[[deviceToken debugDescription]
                         stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                        stringByReplacingOccurrencesOfString: @">" withString: @""] 
                       stringByReplacingOccurrencesOfString: @" " withString: @""];
    
    0 讨论(0)
  • 2020-11-28 19:11

    Functional Swift version

    One liner:

    let hexString = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data.bytes),
    count: data.length).map { String(format: "%02x", $0) }.joinWithSeparator("")
    

    Here's in a reusable and self documenting extension form:

    extension NSData {
        func base16EncodedString(uppercase uppercase: Bool = false) -> String {
            let buffer = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes),
                                                    count: self.length)
            let hexFormat = uppercase ? "X" : "x"
            let formatString = "%02\(hexFormat)"
            let bytesAsHexStrings = buffer.map {
                String(format: formatString, $0)
            }
            return bytesAsHexStrings.joinWithSeparator("")
        }
    }
    

    Alternatively, use reduce("", combine: +) instead of joinWithSeparator("") to be seen as a functional master by your peers.


    Edit: I changed String($0, radix: 16) to String(format: "%02x", $0), because one digit numbers needed to having a padding zero

    (I don't know yet how to mark a question as a duplicate of this other one, so I just posted my answer again)

    0 讨论(0)
提交回复
热议问题