For example, how can you implement storing the profile name and password so that you do not need to re-enter anything when you restart the program?
Closed due to the fact that the question is too general for the participants cheops , zRrr , user194374, Nick Volynkin ♦ 14 Jun '16 at 8:20 .
Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .
- The question is not very clear, add more information ... - Victor Mishustin
2 answers
A better solution would be to use CoreData https://habrahabr.ru/post/191472/ More simple through NSUserDefaults
Create 2 property @property (strong, nonatomic) NSString * email; @property (strong, nonatomic) NSString * password; @property (strong, nonatomic) NSString * email; @property (strong, nonatomic) NSString * password;
Record:
[[NSUserDefaults standardUserDefaults] setValue:self.email forKey:@"email"]; [[NSUserDefaults standardUserDefaults] setValue:self.password forKey:@"password"]; [[NSUserDefaults standardUserDefaults] synchronize];
Reading:
self.email = [[NSUserDefaults standardUserDefaults] stringForKey:@"email"]; self.password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
There are many ways to store data between the launch of an application in iOS, it depends on the following transactions:
- Class singleton
NSUserDefaultsdata storage in the form of a key-value. Convenient for simple types and data structures. Not safe, because All data is stored in the project directory, without encryption. - Storing an object data model with encoding and decoding in
NSKeyedArchiver / NSKeyedUnarchiver. Data model classes must comply with theNSCodingprotocol. Simple to use, each class field will be stored with the appropriate key. Below is an example of storing data for a single AppUser class object:
interface AppUser : NSObject @property(nonatomic, strong) NSString *email, *password, *aToken; @property(nonatomic, strong) NSMutableArray *organisations; @property(nonatomic) BOOL isLogin; +(AppUser *) sharedUser; -(BOOL) saveAppUsersData; -(void) logout; @end# define archivePath (name) [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0] stringByAppendingString: [[@ "/" stringByAppendingString: (name)] stringByAppendingString: @ ". archive"]]
@implementation AppUser @synthesize password, email, isLogin, isAdmin, name, organisations, aToken; static AppUser *mSharedUser; +(AppUser *) sharedUser { @synchronized(self) { if (!mSharedUser) { mSharedUser = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath(@"AppUser")]; if (!mSharedUser) mSharedUser = [[self alloc] init]; } return mSharedUser; } } -(id) init { self = [super init]; if (self) { isLogin = NO; organisations = [NSMutableArray new]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:email forKey:@"email"]; [aCoder encodeObject:password forKey:@"password"]; [aCoder encodeBool:isLogin forKey:@"isLogin"]; [aCoder encodeObject:aToken forKey:@"aToken"]; [aCoder encodeObject:organisations forKey:@"organisations"]; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { email = [aDecoder decodeObjectForKey:@"email"]; password = [aDecoder decodeObjectForKey:@"password"]; isLogin = [aDecoder decodeBoolForKey:@"isLogin"]; organisations = [aDecoder decodeObjectForKey:@"organisations"]; aToken = [aDecoder decodeObjectForKey:@"aToken"]; if (!organisations) organisations = [NSMutableArray new]; } return self; } -(BOOL) saveAppUsersData { return [NSKeyedArchiver archiveRootObject:[AppUser sharedUser] toFile:archivePath(@"AppUser")]; } -(void) logout { NSError *error; [[NSFileManager defaultManager] removeItemAtPath:archivePath(@"AppUser") error:&error]; mSharedUser = [[AppUser alloc] init]; }
- Base data SQLLite, CoreData, Realm. The main way to create storage, data models and queries for their selection. The fastest and most reliable way. Multithreading support.
Realm is cross-platform and can be shared between iOS and Android. Regardless of whether you are working with Java, Objective-C, or Swift, you will use high-level models. https://realm.io