1

When using my app on iOS 13 the manufacturer data has changed format.

When accessing kCBAdvDataManufacturerData in iOS 12 I get in this format:

<13376400>

but in iOS 13 I get this format:

{length = 4, bytes = 0x13376400}

Does anyone know why this has changed?

How can I retrieve the "1337" part as a string?

This is how I access and print the data:

    NSData *manufacturerData = [advertisementData objectForKey:kCBAdvDataManufacturerData];
    NSString *manufacturerString = [NSString stringWithFormat:@"%@", manufacturerData];
    NSString *companyIdentifier = [manufacturerString substringWithRange:NSMakeRange(1, 4)];
    NSLog(@"%@", companyIdentifier);

Prints: leng

I tried manufacturerData.bytes but it gives me EXC_BAD_ACCESS error.

Rufus
  • 21
  • 4
  • The format hasn't changed, merely the output you get when you `print` an instance of`Data`. Show how you are accessing the data. – Paulw11 Sep 27 '19 at 10:03
  • https://stackoverflow.com/questions/58128802/ios-13-xcode-11-nsstring-initwithbytes-return-wrong-value#comment102648436_58128802 See the comments. `description` of `NSData` has changed. It was NEVER a good solution to use it in prod as such. – Larme Sep 27 '19 at 10:03
  • Possible duplicate of [Get device token for push notification](https://stackoverflow.com/questions/8798725/get-device-token-for-push-notification) – Larme Sep 27 '19 at 10:08
  • I am not using description. What am I doing wrong? – Rufus Sep 27 '19 at 11:53
  • `NSString *manufacturerString = [NSString stringWithFormat:@"%@", manufacturerData];` That's calling `description`. Cleary. – Larme Sep 27 '19 at 13:50
  • Cf. Doc: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1 & https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html – Larme Sep 27 '19 at 13:52

1 Answers1

3

Since iOS 13, the description of the kCBAdvDataManufacturerData NSData has been changed. To be able to extract and parse the advertisementDatayou should not base on description any more. Following is a Swift solution version that is working on both iOS 13 and old iOS versions:

According to your code above, you are able to extract the manufacturerData NSData.

let publicData = Data(bytes: manufacturerData.bytes, count: Int(manufacturerData.length))

let publicDataAsHexString = publicData.dataToHexString // this result is same what ever the iOS version.

//// Data extension
extension Data {
    var dataToHexString: String {
        return reduce("") {$0 + String(format: "%02x", $1)}
    }
}
user2167877
  • 1,676
  • 1
  • 14
  • 15