簡體   English   中英

使用objective-c在后台輸入后如何將數據插入sqlite數據庫

[英]How to insert the data into sqlite database after entering in background in with objective-c

正在使用的代碼如下

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self performSelector:@selector(addContacts) withObject:nil afterDelay:0.1];
}

-(void)addContacts
{
    sqlite3_stmt    *statement;

    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat:
                               @"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")",
                               @"Anu", @"0009", @"Hyderabad"];

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(contactDB, insert_stmt,
                           -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {


        }
        else {

            }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);
    }

else
{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Information Alert"
                                                      message:@"Please enter the name "
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
}
}

但是它在后台時不起作用

您可以通過啟用后台任務從技術上做到這一點。 但是Apple僅允許某些任務(例如定位服務,音樂,語音等)的后台模式。如果您偽造音樂或其他允許的任務來執行此操作,則您的應用可能會被拒絕。

您可以告訴iOS,您打算執行短暫的后台任務,而無需任何額外的后台模式。 在開始時使用UIApplication的beginBackgroundTaskWithExpirationHandler: ,然后在完成時調用endBackgroundTask: iOS不保證將允許您執行后台任務。 有關更多信息,請參見這些方法的文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM