我可以在哪里存储 iOS 设备令牌供以后使用?
Where can I store the iOS device token for later use?
现在我正在通过 didRegisterForRemoteNotificationsWithDeviceToken
获取设备令牌。但是,我稍后需要在我的用户登录后在我的应用程序中使用设备令牌。
我以后如何从应用程序的其他地方访问 deviceToken,如果我不能,how/where 我可以将它存储在 didRegisterForRemoteNotificationsWithDeviceToken
中以便以后检索它吗?
最简单的方法就是使用NSUserDefaults
节省:
NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
[ud setObject:myPushToken forKey:@"currentPushToken"];
[ud synchronize];
正在恢复:
NSString * currentPushToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentPushToken"];
这样的数据最好用keychain
。
将这个 class 函数添加到您的项目中
import UIKit
import Security
class Keychain {
class func save(key: String, data: NSData) -> Bool {
let query = [
kSecClass as String : kSecClassGenericPassword as String,
kSecAttrAccount as String : key,
kSecValueData as String : data ]
SecItemDelete(query as CFDictionaryRef)
let status: OSStatus = SecItemAdd(query as CFDictionaryRef, nil)
return status == noErr
}
class func load(key: String) -> NSData? {
let query = [
kSecClass as String : kSecClassGenericPassword,
kSecAttrAccount as String : key,
kSecReturnData as String : kCFBooleanTrue,
kSecMatchLimit as String : kSecMatchLimitOne ]
var dataTypeRef :Unmanaged<AnyObject>?
let status: OSStatus = SecItemCopyMatching(query, &dataTypeRef)
if status == noErr {
return (dataTypeRef!.takeRetainedValue() as NSData)
} else {
return nil
}
}
class func delete(key: String) -> Bool {
let query = [
kSecClass as String : kSecClassGenericPassword,
kSecAttrAccount as String : key ]
let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
return status == noErr
}
class func clear() -> Bool {
let query = [ kSecClass as String : kSecClassGenericPassword ]
let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
return status == noErr
}
}
然后你这样使用它
存储(pass是你的字符串,token)
if let data = pass.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: false) {
let saveSuccess = Keychain.save("Password", data: data)
}
加载
data = Keychain.load("Password")
var pass =NSString(data: data, encoding: UInt())
现在我正在通过 didRegisterForRemoteNotificationsWithDeviceToken
获取设备令牌。但是,我稍后需要在我的用户登录后在我的应用程序中使用设备令牌。
我以后如何从应用程序的其他地方访问 deviceToken,如果我不能,how/where 我可以将它存储在 didRegisterForRemoteNotificationsWithDeviceToken
中以便以后检索它吗?
最简单的方法就是使用NSUserDefaults
节省:
NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
[ud setObject:myPushToken forKey:@"currentPushToken"];
[ud synchronize];
正在恢复:
NSString * currentPushToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentPushToken"];
这样的数据最好用keychain
。
将这个 class 函数添加到您的项目中
import UIKit
import Security
class Keychain {
class func save(key: String, data: NSData) -> Bool {
let query = [
kSecClass as String : kSecClassGenericPassword as String,
kSecAttrAccount as String : key,
kSecValueData as String : data ]
SecItemDelete(query as CFDictionaryRef)
let status: OSStatus = SecItemAdd(query as CFDictionaryRef, nil)
return status == noErr
}
class func load(key: String) -> NSData? {
let query = [
kSecClass as String : kSecClassGenericPassword,
kSecAttrAccount as String : key,
kSecReturnData as String : kCFBooleanTrue,
kSecMatchLimit as String : kSecMatchLimitOne ]
var dataTypeRef :Unmanaged<AnyObject>?
let status: OSStatus = SecItemCopyMatching(query, &dataTypeRef)
if status == noErr {
return (dataTypeRef!.takeRetainedValue() as NSData)
} else {
return nil
}
}
class func delete(key: String) -> Bool {
let query = [
kSecClass as String : kSecClassGenericPassword,
kSecAttrAccount as String : key ]
let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
return status == noErr
}
class func clear() -> Bool {
let query = [ kSecClass as String : kSecClassGenericPassword ]
let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
return status == noErr
}
}
然后你这样使用它
存储(pass是你的字符串,token)
if let data = pass.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: false) {
let saveSuccess = Keychain.save("Password", data: data)
}
加载
data = Keychain.load("Password")
var pass =NSString(data: data, encoding: UInt())