My app was working fine until I added the ability to export data as xsls file. The app crashes immediately when exporting the file. I used FolderPicker to get the path from user and then combine it with the file name. I m using closedXmL.Excel library to construct the excel file from a data table then use workbook.saveas() to save it to the path provided.
The app works fine on simulators on visual studio mac. But crashes on a real iPhone 13
here is the part of my code that deals with this:
[RelayCommand]
async Task Export()
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("Day");
dt.Columns.Add("Inc");
dt.Columns.Add("Exp");
dt.Columns.Add("Tip");
dt.Columns.Add("Net");
dt.Columns.Add("Wrks");
foreach (SqlRecord s in SelectedMonthRecords)
{
DataRow dr = dt.NewRow();
dr[0] = s.Date;
dr[1] = Convert.ToDouble(s.Income);
dr[2] = Convert.ToDouble(s.DailyExp);
dr[3] = Convert.ToDouble(s.Tip);
dr[4] = Convert.ToDouble(s.DailyNet);
dr[5] = Convert.ToDouble(s.Workers);
dt.Rows.Add(dr);
}
dt.AcceptChanges();
string filename = (DateTime.Now.Year.ToString() +
DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString());
XLWorkbook wb = new XLWorkbook();
var ws = wb.Worksheets.Add(dt, "Sheet 1");
CancellationToken ct = new CancellationToken();
var Pth = await PickFolderAsync(ct);
filename = filename + ".xlsx";
var path = Path.Combine(Pth, filename);
wb.SaveAs(path);
}
catch (Exception e)
{
throw;
}
async Task<string> PickFolderAsync(CancellationToken cancellationToken)
{
var result = await FolderPicker.Default.PickAsync(cancellationToken);
if (result.IsSuccessful)
{
//await Toast.Make($"The folder was picked: Name - {result.Folder.Name}, Path - {result.Folder.Path}", ToastDuration.Long).Show(cancellationToken);
}
else
{
//await Toast.Make($"The folder was not picked with error: {result.Exception.Message}").Show(cancellationToken);
}
return result.Folder.Path;
}
}
I m not sure if it relates to permissions on iOs but i have read in apple documentation that there is no need for permissions for file storing on iOs (unlike android).
Which line causes the crash and what exception is thrown?
The problem is, there is no exception thrown. The code works fine on simulator and while debugging. The file is exported normally. The problem only happens on a real iPhone device where I can not see any errors.
That sounds like a Linker problem. See learn.microsoft.com/en-us/dotnet/maui/ios/linking?tabs=vs