简体   繁体   中英

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

Code which am using are as follows

- (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];
}
}

But its not working when its in the background

You can do that technically by enabling background tasks. But apple allows background modes for certain tasks only such as location services, music, voip etc. If you fake a music or some other allowed task to do this, your app may be rejected.

You can tell iOS that you intend to perform a short-lived background task without any extra background modes. Use UIApplication's beginBackgroundTaskWithExpirationHandler: at the start and then call endBackgroundTask: when you're done. iOS does not guarantee that you will be allowed to perform your background task. For more information, see the documentation of those methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM