Toggle navigation
Find or Create a Group
EN
EN
DE
ES
FR
IT
UK
EN
DE
ES
FR
IT
UK
Help
Log In
Sign Up
Help
Log In
Sign Up
EN
EN
DE
ES
FR
IT
UK
EN
DE
ES
FR
IT
UK
Home
Messages
Hashtags
×
×
×
Close
Likes
cocoa@apple-dev.groups.io
Messages
Push Notification device token from APNS auth response.
×
Close
Search
Single
Toggle Dropdown
Messages
Topics
Expanded
Polls
previous page
#1354
next page
Push Notification device token from APNS auth response.
Alex Zavatone
#1354
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;
@end
NS_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
}
}
More
All Messages By This Member
previous page
View All 2 Messages In Topic
next page
previous page
#1354
next page
Join
{cocoa@apple-dev.groups.io to automatically receive all group messages.
×
Close
Report Message
Reason
Report to Moderators
I think this message isn't appropriate for our group. The Group moderators are responsible for maintaining their community and can address these issues.
Report to Groups.io Support
I think this violates the Terms of Service. This includes: harm to minors, violence or threats, harassment or privacy invasion, impersonation or misrepresentation, fraud or phishing.
Note:
Your email address is included with the abuse report.
×
Close
Verify Delete
Are you sure you wish to delete this message from the message archives of cocoa@apple-dev.groups.io?
This cannot be undone.
×
Close
Verify Repost
Are you sure you wish to repost this message?
More Options
More
Home
Hashtags
About
Features
Pricing
Updates
Terms
Help
Twitter
Toggle navigation
About
Features
Pricing
Updates
Terms
Help
©
2023
Groups.io