class APNSHelperSwift: NSObject{
On Mar 12, 2021, at 10:55 AM, Alex Zavatone via groups.io <zav@...> wrote:Sometimes we want to get the string value of the device token after requesting push auth from APNS in didRegisterForRemoteNotificationWithDeviceToken.Here’s how you can get that string value in Objective-C and Swift.Cheers,Alex Zavatone//// APNSHelper.h// pushy//// Created by Alex Zavatone on 3/10/21.// Copyright © 2021. All rights reserved.//#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface APNSHelper : NSObject+ (NSString *)stringFromDeviceToken:(NSData *)deviceToken;@endNS_ASSUME_NONNULL_END//// APNSHelper.m// pushy//// Created by Alex Zavatone on 3/10/21.// Copyright © 2021. All rights reserved.//#import "APNSHelper.h"@implementation APNSHelper+ (NSString *)stringFromDeviceToken:(NSData *)deviceToken{ NSUInteger length = deviceToken.length; if (length == 0) { return nil; } const unsigned char *buffer = deviceToken.bytes; NSMutableString *hexString = [NSMutableString stringWithCapacity:(length * 2)]; for (int charIndex = 0; charIndex < length; charIndex++) { [hexString appendFormat:@"%02x", buffer[charIndex]]; } return [hexString copy];}@end//// APNSHelperSwift.swift// pushy//// Created by Alex Zavatone on 3/11/21.//import Foundation@objc(APNSHelperSwift)class APNSHelperSwift: NSObject{ @objc(stringFromDeviceToken:) public class func stringFromDeviceToken(deviceToken: Data?) -> String? { if (deviceToken == nil) { return nil; } let length = deviceToken!.count * MemoryLayout<Int16>.stride if length == 0 { return nil } var hexString = "" var iter = deviceToken!.makeIterator() while true { guard let char = iter.next() else { break } hexString += String(format: "%02x", char) } return hexString }}
© 2023 Groups.io