Llavero + distribución ad hoc

Para comenzar, algunos detalles: estamos desarrollando una aplicación para iOS que actualmente utiliza Swift / Xcode 6.1 GM 2 para el desarrollo.

Estamos teniendo algunos problemas confusos con el acceso de llavero al distribuir aplicaciones ad hoc y tenemos problemas para rastrear cuál es la razón. Todos los perfiles de aprovisionamiento coinciden con el nombre del paquete de nuestra aplicación. Usamos TestFlight para distribución, aunque no creo que sea el problema.

Solo hemos logrado que funcione en dispositivos iOS 7 que no tenían la aplicación instalada anteriormente. Ningún dispositivo iOS 8 funcionó ad hoc. Los errores que recibíamos al principio eran 25300 (errSecItemNotFound) y ahora, después de restablecer los perfiles de aprovisionamiento, obtenemos un 0 simple (tanto al guardar en la carga como si aún no se pueden recuperar datos). Todo funciona perfectamente al implementar compilaciones de desarrollo de Xcode.

He separado el código para el contenedor de llavero que usamos:

import UIKit
import Security

let serviceIdentifier = "com.Test.KeychainTest"

let kSecClassValue = kSecClass as NSString
let kSecAttrAccountValue = kSecAttrAccount as NSString
let kSecValueDataValue = kSecValueData as NSString
let kSecClassGenericPasswordValue = kSecClassGenericPassword as NSString
let kSecAttrServiceValue = kSecAttrService as NSString
let kSecMatchLimitValue = kSecMatchLimit as NSString
let kSecReturnDataValue = kSecReturnData as NSString
let kSecMatchLimitOneValue = kSecMatchLimitOne as NSString

class KeychainManager {

class func setString(value: NSString, forKey: String) {
    self.save(serviceIdentifier, key: forKey, data: value)
}

class func stringForKey(key: String) -> NSString? {
    var token = self.load(serviceIdentifier, key: key)

    return token
}

class func removeItemForKey(key: String) {
    self.save(serviceIdentifier, key: key, data: "")
}



class func save(service: NSString, key: String, data: NSString) {
    var dataFromString: NSData = data.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
    // Instantiate a new default keychain query
    var keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, key, dataFromString], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecValueDataValue])

    // Delete any existing items
    SecItemDelete(keychainQuery as CFDictionaryRef)

    if data == "" { return }

    // Add the new keychain item
    var status: OSStatus = SecItemAdd(keychainQuery as CFDictionaryRef, nil)
    var alertView = UIAlertView();
    alertView.addButtonWithTitle("Ok");
    alertView.title = "Status";
    alertView.message = "Saving \(status)";
    alertView.show();
}

class func load(service: NSString, key: String) -> NSString? {
    // Instantiate a new default keychain query
    // Tell the query to return a result
    // Limit our results to one item
    var keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, key, kCFBooleanTrue, kSecMatchLimitOneValue], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecReturnDataValue, kSecMatchLimitValue])

    var dataTypeRef :Unmanaged<AnyObject>?

    // Search for the keychain items
    let status: OSStatus = SecItemCopyMatching(keychainQuery, &dataTypeRef)
    var alertView = UIAlertView();
    alertView.addButtonWithTitle("Ok");
    alertView.title = "Status";
    alertView.message = "Loading \(status)";
    alertView.show();

    let opaque = dataTypeRef?.toOpaque()

    var contentsOfKeychain: NSString?

    if let op = opaque? {
        let retrievedData = Unmanaged<NSData>.fromOpaque(op).takeUnretainedValue()

        // Convert the data retrieved from the keychain into a string
        contentsOfKeychain = NSString(data: retrievedData, encoding: NSUTF8StringEncoding)
    } else {
        return nil
    }

    return contentsOfKeychain
}


}
¿Hay algo que nos hayamos perdido?¿Cuál es la mejor manera de solucionar este problema? No recibimos ningún error del llavero en la utilidad de configuración de registros / iPhone. Por el momento, solo pongo algunas alertas simples en el código para averiguar cuál es el estado de la operación.

Respuestas a la pregunta(2)

Su respuesta a la pregunta