Offline usage - xamoom/xamoom-ios-sdk GitHub Wiki

Since version 3.0.0 of the SDK you can also save the data offline. We use CoreData to save the entities in the XamoomSDK-Bundle.

There are two different ways to save entities offline. There is the manual approach, where you save the entities manually and have to remember in your project what you saved, so you can delete it again. The other approach is to use a storage module, that will manage the data.

Enable Offline Mode

To enable the offline enbale it during API setup.

api.isOffline = true

When offline mode is enabled you are able to load offline data.

Save offline

We automatically download files from XMMContent, XMMContentBlock and XMMSpot. You can save these entities with an callback:

content.saveOffline({ (url, data, error) in
   // Your logic here
})

To save the content´s data offline call the saveOffline function of the XMMContent class.

Get offline data

If offline mode is enabled you can load the saved content like this

api.content(withID: contentId, completion: { (content, error) in
   if let error = error {
      print("Error: \(error)")
   } else if let  = content {
      print("Offline content: \(content)")
   }
})

Delete offline data

Delete entities from offline storage by calling deleteOfflineCopy.

content.deleteOfflineCopy()

Offline Spots

To use Spots offline you can download and store them with the XMMOfflineStorageTagModule class. To download all spots with the tag "offline" add the following lines to your code.

let module = XMMOfflineStorageTagModule(api: self.api)
module.downloadAndSave(withTags: ["offline"], downloadCompletion: { (url, data, error) in
   if let error = error {
      print("Error: \(error)")
   } else {
      print("Data: \(data)")
      print("URL: \(url)")
   }
}, completion: { (spots, error) in
   if let error = error {
      print("Error: \(error)")
   } else {
      print("Spots count: \(spots.count)")
   }
})

You are also able to delete all loaded spots with the deleteSavedDataWithTags: function of XMMOfflineStorageTagModule.

module.deleteSavedData(withTags: ["offline"])

XMMFileManager

If you want to use offline saved files outside of XMMContentBlocks you can do that by using the XMMOfflineFileManager.

let fileManager = XMMOfflineFileManager()

// Get data for url
let data = try? fileManager.savedData(fromUrl: "your_data_url") // Call can throw, optional handling required

// Get image for url
let image = try? fileManager.savedImage(fromUrl: "your_image_url") // Call can throw, optional handling required

// Get local file url for url 
let localUrl = fileManager.url(forSavedData: "your_url")

XMMOfflineDownloadManager

This is the place were the magic happens. This class starts and handles the download tasks whenever saveFileFromUrl:completion: was called. You do not need to do anything with this class, because everything happens automatically here, expect you set the [startDownloadAutomatically] property to false.

If you set startDownloadAutomatically to false you have to manually call startDownloads.