Save the data in the system key string. Even if the application is deleted, the data is saved.

Source: Internet
Author: User
Tags key string

@ Interface saveobject: nsobject

// Save data

+ (Void) Save :( nsstring *) Key Data :( ID) data;

// Load data

+ (ID) Load :( nsstring *) Key;

// Delete data

+ (Void) delete :( nsstring *) Key;

@ End

 

 

@ Implementation saveobject

// Obtain data

+ (Nsmutabledictionary *) keychainquery :( nsstring *) Key

{

If (key ){

Return [nsmutabledictionary dictionarywithobjectsandkeys:

(_ Bridge_transfer ID) ksecclassgenericpassword, (_ bridge_transfer ID) ksecclass,

Key, (_ bridge_transfer ID) ksecattrservice,

Key, (_ bridge_transfer ID) ksecattraccount,

(_ Bridge_transfer ID) ksecattraccessibleafterfirstunlock, (_ bridge_transfer ID) ksecattraccessible,

Nil];

}

Return nil;

}

// Save data

+ (Void) Save :( nsstring *) Key Data :( ID) data

{

If (Key & Data! = Nil ){

// Get search dictionary

Nsmutabledictionary * keychainquery = [self keychainquery: Key];

// Delete old item before add new item

Secitemdelete (_ bridge_retained cfdictionaryref) keychainquery );

// Add new object to search Dictionary (Attention: the data format)

[Keychainquery setobject: [nskeyedarchiver archiveddatawithrootobject: Data] forkey :( _ bridge_transfer ID) ksecvaluedata];

// Add item to keychain with the search dictionary

Secitemadd (_ bridge_retained cfdictionaryref) keychainquery, null );

}

}

// Load data

+ (ID) Load :( nsstring *) Key

{

Id ret = nil;

If (key ){

Nsmutabledictionary * keychainquery = [self keychainquery: Key];

// Configure the search setting

[Keychainquery setobject :( ID) kcfbooleantrue forkey :( _ bridge_transfer ID) ksecreturndata];

[Keychainquery setobject :( _ bridge_transfer ID) ksecmatchlimitone forkey :( _ bridge_transfer ID) ksecmatchlimit];

Cfdataref keydata = NULL;

If (secitemcopymatching (_ bridge_retained cfdictionaryref) keychainquery, (cftyperef *) & keydata) = noerr ){

@ Try {

Ret = [nskeyedunarchiver unarchiveobjectwithdata :( _ bridge_transfer nsdata *) keydata];

}

@ Catch (nsexception * E ){

Nslog (@ "unarchive of % @ failed: % @", key, e );

}

@ Finally {}

}

}

Return ret;

}

// Delete data

+ (Void) delete :( nsstring *) Key

{

Nsmutabledictionary * keychainquery = [self keychainquery: Key];

Secitemdelete (_ bridge_retained cfdictionaryref) keychainquery );

}

@ End

Save the data in the system key string. Even if the application is deleted, the data is saved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.