0

わかりましたので、私はチタンの単純なキーチェーンモジュールをxcodeでしばらく書き込もうとしましたが、まだ正しくできていません。xcodeでプログラムを実行すると、ビルドが成功したと表示されますが、エミュレータを開いて実行しません。どのメソッドが問題を引き起こしているかを確認するためにコードをコメントアウトし始めました。これら 2 つのメソッドをコメントアウトすると、エミュレーターは正常に動作します。私はObjective Cとモジュールの作成が初めてなので、アドバイスは素晴らしいでしょう。私の主な質問は、これら 2 つの方法に何か問題があると思いますか。ご意見やアドバイスをいただければ幸いです。

+ (BOOL)setString:(NSString *)string forKey:(NSString *)key {
if (string == nil || key == nil) {
    return NO;
}

key = [NSString stringWithFormat:@"%@ - %@", [Keychain appName], key];

// First check if it already exists, by creating a search dictionary and requesting     that 
// nothing be returned, and performing the search anyway.
NSMutableDictionary *existsQueryDictionary = [NSMutableDictionary dictionary];

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

[existsQueryDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];

// Add the keys to the search dict
[existsQueryDictionary setObject:@"service" forKey:(id)kSecAttrService];
[existsQueryDictionary setObject:key forKey:(id)kSecAttrAccount];

OSStatus res = SecItemCopyMatching((CFDictionaryRef)existsQueryDictionary, NULL);
if (res == errSecItemNotFound) {
    if (string != nil) {
        NSMutableDictionary *addDict = existsQueryDictionary;
        [addDict setObject:data forKey:(id)kSecValueData];

        res = SecItemAdd((CFDictionaryRef)addDict, NULL);
        NSAssert1(res == errSecSuccess, @"Recieved %d from SecItemAdd!", res);
    }
} else if (res == errSecSuccess) {
    // Modify an existing one
    // Actually pull it now of the keychain at this point.
    NSDictionary *attributeDict = [NSDictionary dictionaryWithObject:data forKey:(id)kSecValueData];

    res = SecItemUpdate((CFDictionaryRef)existsQueryDictionary, (CFDictionaryRef)attributeDict);
    NSAssert1(res == errSecSuccess, @"SecItemUpdated returned %d!", res);

} else {
    NSAssert1(NO, @"Received %d from SecItemCopyMatching!", res);
}

return YES;

}

+ (NSString *)getStringForKey:(NSString *)key {

key = [NSString stringWithFormat:@"%@ - %@", [Keychain appName], key];

NSMutableDictionary *existsQueryDictionary = [NSMutableDictionary dictionary];

[existsQueryDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];

// Add the keys to the search dict
[existsQueryDictionary setObject:@"service" forKey:(id)kSecAttrService];
[existsQueryDictionary setObject:key forKey:(id)kSecAttrAccount];

// We want the data back!
NSData *data = nil;

[existsQueryDictionary setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];

OSStatus res = SecItemCopyMatching((CFDictionaryRef)existsQueryDictionary, (CFTypeRef *)&data);
[data autorelease];
if (res == errSecSuccess) {
    NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    return string;
} else {
    NSAssert1(res == errSecItemNotFound, @"SecItemCopyMatching returned %d!", res);
}       

return nil;
}
4

1 に答える 1

1

これらのメソッドはどこから呼び出していますか? それらはあなたのメインモジュールにありますか?最終的な JavaScript 呼び出しをどのようにしたいのかを教えていただければ、より自信を持って問題に対処できます。

私が目にする差し迫った問題の 1 つは、プリミティブ型 (たとえば BOOL) を Titanium に送り返すことができないことです。最初に数値に変換する必要があります。(心配しないでください、JavaScript とその真の値は BOOL のように使用できます!) それを乗り越えるのに役立つマクロがあります -- NSNumber* を返し、実際の戻り値を次のようにラップします: return NUMBOOL(YES); または NUMBOOL(NO); を返します。

もう1つはあなたの主張かもしれません。Kroll は単一の引数でメソッドを呼び出します。この引数から、与えられた引数を取得できます。JavaScript に公開されている場合、メソッド シグネチャは通常次のようになります。 -(void)mySpecialMethod:(id)args;

3 番目の問題は、メソッドの名前である可能性があります。「get」と「set」は Kroll の特別なキーワードで、プロパティで使用されます。JavaScript から myModule.property = 'something' と記述し、それによって object-c で -(void)setProperty:(id)args が呼び出されます。

最後に、これらをオブジェクト レベルのメソッドに対してクラス レベルのメソッドとして宣言した理由がわかりません。おそらく、これらの方法がどこで使用されているかについて詳しく説明していただければ、あなたが何をしようとしているのかを理解し、そこにたどり着くのを助けることができます.

それを過ぎたら、Titanium Mobile のコア ソース コードを見て、独自のモジュールで object-c を使用してできることとできないことを確認してください。

お役に立てれば!-ドーソン

于 2011-08-10T04:58:29.767 に答える