6

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.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

2 Answers2

3

This

    let tokenParts = credentials.token.map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()
    print("Device Token: \(token)")
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
2

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
7stud
  • 46,922
  • 14
  • 101
  • 127
  • @Gruntcakes, Also see: https://stackoverflow.com/questions/39584707/nslog-on-devices-in-ios-10-xcode-8-seems-to-truncate-why – 7stud Jun 29 '19 at 17:51
  • That doesn't work in the context of push token, for example let tokenStr = String(decoding: credentials.token, as: UTF8.self) for c in tokenStr { print(c, terminator: "") } logs this: [?D�m,^�RR�����[����>��J�%�~ – Gruntcakes Jul 01 '19 at 15:46
  • The link suggests to use printf. Neither NSLog, print, print4, remove the truncated ... – Gruntcakes Jul 01 '19 at 15:47
  • @Gruntcakes, I think your output in the comment(and in your question!) means that there an non UTF8 characters in the string--in other words the encoding of the string is not UTF8. – 7stud Jul 01 '19 at 16:49
  • I looked around the String class, and it looks like Strings only have UTF-8 and UTF-16 *views*. What happens if you try: `let tokenStr = String(decoding: tokenData, as: UTF16.self)` – 7stud Jul 02 '19 at 03:41