Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Reading and writing files from an iOS FireMonkey application

Author: dragonhome

Here’s an example of how you can read and write files in your iOS FireMonkey application.

Enjoy!

{$IFDEF FPC}
var
content : NSString;
{$ENDIF}

{$IFDEF FPC}
function MyFileName : NSString;
var
paths : NSArray;
fileName : NSString;
begin
paths := NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, True);
fileName := paths.objectAtIndex(0);
fileName.stringByAppendingString(NSSTR(PChar(‘/myfile.txt’)));
Result := fileName;
end;
{$ENDIF}

procedure TForm1.Button1Click(Sender: TObject);
begin
// Write to file
{$IFDEF FPC}
Label2.Text := ‘Wrote to: ‘+String(MyFileName.UTF8String);
content := NSSTR(PChar(String(Memo1.Text)));
content.writeToFile_atomically_encoding_error(MyFileName,False,NSStringEncodingConversionAllowLossy,nil);
content.release;
{$ENDIF}
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
// Read from file
{$IFDEF FPC}
Label2.Text := ‘Read from ‘+String(MyFileName.UTF8String);
content.InitWithContentsOfFile_usedEncoding_error(MyFileName,nil,nil);
Memo1.Text := String(content.UTF8String);
content.release;
{$ENDIF}
end;

Interested in learning how to read and write files from an iOS FireMonkey application? You may use the App Builder Software and make the most of it. 
Try the free tool here.
Exit mobile version