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

后端 未结 30 2004
挽巷
挽巷 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:29

    What about one line solution?

    Objective C

    NSString *token = [[data.description componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet]invertedSet]]componentsJoinedByString:@""];
    

    Swift

    let token = data.description.componentsSeparatedByCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet).joinWithSeparator("")
    

提交回复
热议问题