Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a TiniFile from application to iOS

I am very new to the iOS platform. I am trying to save an INI file for my application. The problem is that I can't get a path with write permission.

Here is my code:

  ini := TIniFile.Create(GetHomePath + '/user.dat');
  try
    ini.WriteString('data','user',edtuser.Text);
    ini.WriteString('data','descr',edt1.Text);
  finally
    ini.Free;
  end;

I get an exception that the file can't be created. How can I get a writable path using Firemonkey?

like image 708
opc0de Avatar asked Oct 22 '13 11:10

opc0de


2 Answers

Use TPath.GetDocumentsPath (and use TPath.Combine instead of concatenation, to remove the hard-coded /):

uses
  System.IOUtils;


ini := TIniFile.Create(TPath.Combine(TPath.GetDocumentsPath, 'user.dat'));

Using TPath.GetDocumentsPath works across all supported platforms (Win32, Win64, OSX, iOS, and Android) transparently, and using TPath.Combine will automatically add the TPath.DirectorySeparatorChar, so you don't have to manually concatenate them.

If you prefer to do it yourself, though:

var
  IniName: string;
begin
  IniName := TPath.GetDocumentsPath + TPath.DirectorySeparatorChar + 'user.dat';
  Ini := TIniFile.Create(IniName);
  try
    // Rest of code
  finally
    Ini.Free;
  end;
end;
  
like image 62
Ken White Avatar answered Nov 07 '22 17:11

Ken White


May be this or this can help you

uses INIFiles;

function TForm6.MyINIFilePath: string;
begin
//  Result := GetHomePath + PathDelim + 'Library' + PathDelim+'My.ini';
  Result := GetHomePath + PathDelim + 'Documents' + PathDelim+'MyD.ini';
end; 
like image 23
Hugues Van Landeghem Avatar answered Nov 07 '22 16:11

Hugues Van Landeghem