Reputation: 41
I'm writing an app on the iPhone using Qt5.2. When the app is running I'd like to save some information from the user input to a file so the next time the user start the app he will have the save information.
I tried to do this:
QString cur_dir = QDir::currentPath();
QString init_file = cur_dir+"/init.xml";
QSettings settings( init_file, QSettings::IniFormat );
settings.setValue("General/SavedVariable", sel_label->text() );
but the file was not created when i verify doing this:
if( !QFileInfo( init_file ).exists() )
std::cout << "FILE DOES NOT EXISTS " << std::endl;
else
std::cout << "FILE EXISTS " << std::endl;
Upvotes: 2
Views: 1985
Reputation:
Applications are permitted/expected to write to the iOS Documents directory. Data written here is persistent and will be backed up during the iTunes backup process.
#include <QStandardPaths>
QFile myfile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).append("/Documents/mystuff.txt"));
myfile.open(QIODevice::ReadWrite);
Upvotes: 0
Reputation: 514
Actually in IOS you can't save your files in current directory(not bad to check this). I used QStandardPaths class and wrote this code to make it cross-platform:
QString path = QStandardPaths::standardLocations( QStandardPaths::AppDataLocation ).value(0);
QDir myDir(path);
if (!myDir.exists()) {
myDir.mkpath(path);
}
QDir::setCurrent(path);
Now, it's okay to make your files or directories here in current dir.
Upvotes: 0
Reputation: 41
I found a solution. You can not save a file in the Bundle directory. You have to save in the Documents directory. Below is the code I used to do that:
{
QString cur_dir = QDir::currentPath();
int found = cur_dir.lastIndexOf( "/" );
QString leftSide = cur_dir.left(found+1);
leftSide += "Documents";
init_file = leftSide+"/init.xml";
if( !QFileInfo( init_file ).exists() )
writeConfig();
else
readConfig();
}
void
writeConfig( void )
{
QSettings settings( init_file, QSettings::IniFormat );
settings.setValue("General/SavedVariable", sel_label->text() );
}
void
readConfig( void )
{
QSettings settings( init_file, QSettings::IniFormat );
QString save_var = settings.value( "General/SavedVariable" ).toString();
sel_label->setText( save_var );
}
This is one way. You could also add to your project a objective C file .mm to your qt project and get the Documents path directly.
NSString *rootPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
Upvotes: 2