How can I convert my device token (NSData) into an NSString?

后端 未结 30 1690
挽巷
挽巷 2020-11-28 18:31

I am implementing push notifications. I\'d like to save my APNS Token as a String.

- (void)application:(UIApplication *)application
didRegisterForRemoteNotif         


        
相关标签:
30条回答
  • 2020-11-28 19:30

    This is a little bit shorter solution:

    NSData *token = // ...
    const uint64_t *tokenBytes = token.bytes;
    NSString *hex = [NSString stringWithFormat:@"%016llx%016llx%016llx%016llx",
                     ntohll(tokenBytes[0]), ntohll(tokenBytes[1]),
                     ntohll(tokenBytes[2]), ntohll(tokenBytes[3])];
    
    0 讨论(0)
  • 2020-11-28 19:30

    I've tried to test two different methods with format "%02.2hhx" and "%02x"

        var i :Int = 0
        var j: Int = 0
        let e: Int = Int(1e4)
        let time = NSDate.timeIntervalSinceReferenceDate
        while i < e {
            _ =  deviceToken.map { String(format: "%02x", $0) }.joined()
            i += 1
        }
        let time2 = NSDate.timeIntervalSinceReferenceDate
        let delta = time2-time
        print(delta)
    
        let time3 = NSDate.timeIntervalSinceReferenceDate
        while j < e {
            _ =  deviceToken.reduce("", {$0 + String(format: "%02x", $1)})
            j += 1
        }
        let time4 = NSDate.timeIntervalSinceReferenceDate
        let delta2 = time4-time3
        print(delta2)
    

    and the result is that the fastest is "%02x" at average 2.0 vs 2.6 for the reduced version:

    deviceToken.reduce("", {$0 + String(format: "%02x", $1)})
    
    0 讨论(0)
  • 2020-11-28 19:30

    Here's how you do it in Xamarin.iOS

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        var tokenStringBase64 = deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None);
        //now you can store it for later use in local storage
    }
    
    0 讨论(0)
  • 2020-11-28 19:30

    Swift:

    let tokenString = deviceToken.description.stringByReplacingOccurrencesOfString("[ <>]", withString: "", options: .RegularExpressionSearch, range: nil)
    
    0 讨论(0)
  • 2020-11-28 19:33
    var token: String = ""
    for i in 0..<deviceToken.count {
        token += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
    }
    
    print(token)
    
    0 讨论(0)
  • 2020-11-28 19:34

    Someone Helped me with this.I am just passing along

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    
        const unsigned *tokenBytes = [deviceToken bytes];
        NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                             ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                             ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                             ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    
        [[MyModel sharedModel] setApnsToken:hexToken];
    }
    
    0 讨论(0)
提交回复
热议问题