Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should you convert push token data to String for viewing/debugging/logging/placing into http to send to a server?

Tags:

xcode

ios

swift

With XCode 11 I am no longer able to view the full value for a push token. Here's some example code to illustrate:

func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType)
{
    var method1 = NSString(format: "%@", credentials.token as CVarArg) as String
    print("method1: \(method1)")

    method1 = method1.replacingOccurrences(of: " ", with: "")
    method1 = method1.replacingOccurrences(of: "<", with: "")
    method1 = method1.replacingOccurrences(of: ">", with: "")
    print("method1 again: \(method1)")

    let method2 = String(decoding: credentials.token, as: UTF8.self)
    print("method2: \(String(describing: method2))")

    let method3 = credentials.token.description as String
    print("method3: \(method3)")

However when the above code is run with Xcode 11, this is the output:

method1: {length = 32, bytes = 0x5b3f44e0 6d2c5ee5 5252d3db f5bb915b ... 12844aeb 13259e7e }
method1 again: {length=32,bytes=0x5b3f44e06d2c5ee55252d3dbf5bb915b...12844aeb13259e7e}
method2: [?D�m,^�RR�����[����>��J�%�~
method3: 32 bytes

As viewing the variables in Xcode:

enter image description here

In previous versions of Xcode, method1 would be logged/viewable as something like this:

44154da73234500153106883ffc1071fa59c0d24f1a1d29ea70871e5aa8dbb41

But now its this:

0x5b3f44e06d2c5ee55252d3dbf5bb915b...12844aeb13259e7e

There is ... in the middle of it.

How can I just log/interactively view the contents of credentials.token with Xcode 11?

I want to copy/paste the value into a php script to manually send push message to the app for testing purposes.

like image 710
Gruntcakes Avatar asked Jun 28 '19 20:06

Gruntcakes


2 Answers

This

    let tokenParts = credentials.token.map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()
    print("Device Token: \(token)")
like image 60
Gruntcakes Avatar answered Nov 14 '22 11:11

Gruntcakes


Maybe try printing out each character--rather than the whole string:

let str = "44154da73234500153106883ffc1071fa59c0d24f1a1d29ea70871e5aa8dbb41"
let tokenData = Data(str.utf8)

let tokenStr = String(decoding: tokenData, as: UTF8.self)
//Supposedly, that is a failable String initializer and therefore it returns an optional type.

print(str)
if let tokenStr = tokenStr {
    for c in tokenStr {
        print(c, terminator: "")

    }
}

output:

44154da73234500153106883ffc1071fa59c0d24f1a1d29ea70871e5aa8dbb41  
44154da73234500153106883ffc1071fa59c0d24f1a1d29ea70871e5aa8dbb41
like image 29
7stud Avatar answered Nov 14 '22 09:11

7stud