Skip to content

Commit ace3fb7

Browse files
committed
While using the VC the security scope is on & refactor load GPX File. Fixes #271
1 parent 5a7bd9f commit ace3fb7

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

Diff for: OpenGpxTracker/GPXFilesTableViewController.swift

+38-17
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
142142
}
143143
self.navigationItem.leftBarButtonItems = [folderButton]
144144

145+
_ = GPXFileManager.GPXFilesFolderURL.startAccessingSecurityScopedResource()
145146
// Initial load of the data
146147
let list: [GPXFileInfo] = GPXFileManager.fileList
147148
gpxFilesFound = list.count != 0
@@ -155,6 +156,7 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
155156

156157
/// Removes notfication observers
157158
deinit {
159+
GPXFileManager.GPXFilesFolderURL.stopAccessingSecurityScopedResource()
158160
NotificationCenter.default.removeObserver(self)
159161
}
160162

@@ -313,34 +315,53 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
313315

314316
/// Given a GPX file loads the file in the
315317
func loadGPXFile(gpxFileURL: URL) {
318+
319+
self.displayLoadingFileAlert(true)
316320
DispatchQueue.global(qos: .userInitiated).async {
317-
DispatchQueue.main.sync {
318-
self.displayLoadingFileAlert(true)
319-
}
320-
print("Load gpx File: \(gpxFileURL)")
321-
guard let gpx = GPXParser(withURL: gpxFileURL)?.parsedData() else {
322-
print("GPXFileTableViewController:: load of GPX file failed")
323-
self.displayLoadingFileAlert(false)
324-
return
325-
}
326-
327-
DispatchQueue.main.sync {
328-
self.displayLoadingFileAlert(false) {
329-
self.delegate?.didLoadGPXFileWithName(gpxFileURL.deletingPathExtension().lastPathComponent, gpxRoot: gpx)
321+
print("GPXFIlesTableViewController:: loadGPXFile Load gpx File: \(gpxFileURL)")
322+
if GPXFileManager.GPXFilesFolderURL.startAccessingSecurityScopedResource() {
323+
defer {
324+
GPXFileManager.GPXFilesFolderURL.stopAccessingSecurityScopedResource()
325+
}
326+
do {
327+
DispatchQueue.main.sync {
328+
self.displayLoadingFileAlert(true)
329+
}
330+
guard let gpx = GPXParser(withURL: gpxFileURL)?.parsedData() else {
331+
print("GPXFileTableViewController:: load of GPX file failed")
332+
DispatchQueue.main.sync {
333+
Toast.error("Could not open file")
334+
self.displayLoadingFileAlert(false)
335+
}
336+
return
337+
}
338+
DispatchQueue.main.sync {
339+
self.displayLoadingFileAlert(false) {
340+
self.delegate?.didLoadGPXFileWithName(gpxFileURL.deletingPathExtension().lastPathComponent, gpxRoot: gpx)
341+
self.dismiss(animated: true, completion: nil)
342+
self.presentingViewController?.dismiss(animated: true, completion: nil)
343+
}
344+
}
345+
}
346+
} else {
347+
print("Could not access folder: \(GPXFileManager.GPXFilesFolderURL)")
348+
DispatchQueue.main.sync {
349+
Toast.error("Could not open file")
350+
self.displayLoadingFileAlert(false)
330351
self.dismiss(animated: true, completion: nil)
331352
}
332353
}
333354
}
334-
335355
}
336-
356+
337357
/// Loads the GPX file that corresponds to rowIndex in fileList in the map.
338358
internal func actionLoadFileAtIndex(_ rowIndex: Int) {
339359
guard let gpxFileInfo: GPXFileInfo = (self.fileList.object(at: rowIndex) as? GPXFileInfo) else {
340360
print("GPXFileTableViewController:: actionLoadFileAtIndex(\(rowIndex)): failed to get fileURL")
341361
return
342362
}
343363
print("GPXFileTableViewController:: Load gpx File: \(gpxFileInfo.fileName)")
364+
print("GPXFileTableViewController:: Load gpx URL: \(gpxFileInfo.fileURL)")
344365
loadGPXFile(gpxFileURL: gpxFileInfo.fileURL)
345366
}
346367

@@ -365,7 +386,7 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
365386
self.present(alertController, animated: true, completion: nil)
366387
} else { // will dismiss alert
367388
activityIndicatorView.stopAnimating()
368-
self.presentingViewController?.dismiss(animated: true, completion: nil)
389+
//self.presentingViewController?.dismiss(animated: true, completion: nil)
369390
}
370391

371392
// if completion handler is used
@@ -450,7 +471,7 @@ class GPXFilesTableViewController: UITableViewController, UINavigationBarDelegat
450471
/// For reloading the tableView in `GPXFileTableViewController`
451472
///
452473
/// It gets from the `GPXFileManager` the`fileList`
453-
///
474+
///
454475
///
455476
@objc func reloadTableData() {
456477
print("TableViewController: reloadTableData")

0 commit comments

Comments
 (0)