我有一個應用程序,我有兩個文本框和一個按鈕。在第一個文本框中輸入用戶的用戶名,在第二個文本框中輸入用戶的密碼。當用戶輸入用戶名和密碼並點擊創建用戶按鈕時,將調用一個後端api,它將註冊用戶的用戶名和密碼,並且當時我想在加密的本地SQLite數據庫中輸入用戶的用戶名和密碼格式。如何在SQLite數據庫中只插入一個用戶名和密碼?
當用戶註冊時,該特定用戶的用戶名應顯示在第一個文本字段中,並且不應該可編輯,並且密碼字段應該是可編輯的。只有一個用戶被允許註冊整個應用程序。
我有一個應用程序,我有兩個文本框和一個按鈕。在第一個文本框中輸入用戶的用戶名,在第二個文本框中輸入用戶的密碼。當用戶輸入用戶名和密碼並點擊創建用戶按鈕時,將調用一個後端api,它將註冊用戶的用戶名和密碼,並且當時我想在加密的本地SQLite數據庫中輸入用戶的用戶名和密碼格式。如何在SQLite數據庫中只插入一個用戶名和密碼?
當用戶註冊時,該特定用戶的用戶名應顯示在第一個文本字段中,並且不應該可編輯,並且密碼字段應該是可編輯的。只有一個用戶被允許註冊整個應用程序。
您可以使用NSUserDefaults在本地保存它。以下代碼用於保存數據。
perf = [NSUserDefaults standardUserDefaults];
[perf setObject:@"1" forKey:@"remember"];
[perf setObject:emailAddresTextField.text forKey:@"email"];
[perf setObject:passwordTextField.text forKey:@"password"];
[perf synchronize];
而且用於檢索數據使用下面的代碼
perf=[NSUserDefaults standardUserDefaults];
NSString *remString = [perf stringForKey:@"remember"];
if ([remString isEqualToString:@"1"]) {
emailAddresTextField.text=[perf stringForKey:@"email"];
passwordTextField.text = [perf stringForKey:@"password"];
[rememberBtn setTag:1];
[rememberBtn setImage:[UIImage imageNamed:@"on_radioButton.png"] forState:0];
}
那麼userdefaults是不是真的安全地存儲用戶憑據...你真的不想使用這種方法的機密信息... – CyberK
第一步是檢查數據庫中的資源文件夾中,並創建數據庫: -
1)
-(void)checkAndCreateDatabase{
databaseName = @"databasename.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
BOOL success;
// Create a FileManager object, we will use this to check the status of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success) {
return;
}
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
2)調用函數來檢索數據: -
-(NSMutableArray *) readFromDatabase:(NSString *)query{
// Setup the database object
sqlite3 *database;
// Init the animals Array
returnArray = [[NSMutableArray alloc] init];
NSString *readCommand= query;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = [readCommand UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
[returnArray addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]];
[returnArray addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return returnArray;
}
3)檢查該retrun數組爲空或零然後調用在步驟
4限定的插入功能)插入到數據庫中
-(void)insertIntoDatabase:(NSString *)username:(NSString *)password{
// Setup the database object
sqlite3 *database;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
NSString *[email protected]"Insert into tableName values(username,password)";
const char *sqlStatement = [insertCommand UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {}
sqlite3_finalize(compiledStatement);
}
// Release the compiled statement from memory
}
sqlite3_close(database);
}
anil我試過了,但它不工作 – Rani
確保你已經創建資源文件夾中的sqlite數據庫在表中包含兩個字段username和password。 –
不要使用一個SQLite DBS到存儲用戶憑證。通過逆向工程,獲取數據非常簡單。這是因爲您可能在用於加密/解密密碼的程序中具有一些價值。爲了安全起見,請使用鑰匙串。 github上有幾個項目,這使得使用鑰匙鏈非常容易。沒有更安全的方式來保存憑證,然後鑰匙鏈,所以請使用它來存儲您的憑據!
由於cyberK表示它不安全。對於使用鑰匙鏈,你可以參考這個post
在添加用戶之前,檢查你的數據庫是否是他們的任何用戶,如果有,則彈出其他明智的註冊表。 – Ron
@Ron其實我很困惑,你可以詳細解釋 – Rani
當你的登錄視圖是打開在視圖中確實出現你把一個選擇查詢爲你的用戶表它返回數組的用戶,並檢查它的計數是0或不,如果零然後新用戶註冊,否則彈出用戶一個用戶已經註冊。 – Ron