Access the expiration information of an expired mobile map package.
Use case
The data contained within a mobile map package (MMPK) may only be relevant for a fixed period of time. Using ArcGIS Pro, the author of an MMPK can set an expiration date to ensure the user is aware the data is out of date.
As long as the author of an MMPK has set an expiration date, the expiration date can be read even if the MMPK has not yet expired. For example, developers could also use this API to warn app users that an MMPK may be expiring soon.
How to use the sample
Load the app. The author of the MMPK used in this sample chose to set the MMPK's map as still readable, even if it's expired. The app presents expiration information to the user.
How it works
- Create an
AGSMobileMapPackage
object, passing in the URL to the mobile map package's location. - Load the mobile map package.
- Present the mobile map package's expiration information to the user:
- Use
AGSExpiration.message
to get the expiration message set by the author of the MMPK. - Use
AGSExpiration.dateTime
to get the expiration date set by the author of the MMPK.
- Use
Relevant API
- AGSExpiration
- AGSMobileMapPackage
Offline data
This sample uses the LothianRiversAnno - Expired mobile map package. It is downloaded from ArcGIS Online automatically.
Tags
expiration, mmpk
Sample Code
//
// Copyright © 2019 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
import ArcGIS
/// A view controller that manages the interface of the Honor Mobile Map Package
// Expiration Date sample.
class HonorMobileMapPackageExpirationDateViewController: UIViewController {
/// The map view managed by the view controller.
@IBOutlet weak var mapView: AGSMapView! {
didSet {
loadMobileMapPackage()
}
}
@IBOutlet weak var expirationView: UIView!
@IBOutlet weak var expirationMessageLabel: UILabel!
@IBOutlet weak var timeToExpirationLabel: UILabel!
/// The mobile map package used by this sample.
let mobileMapPackage = AGSMobileMapPackage(fileURL: Bundle.main.url(forResource: "LothianRiversAnno", withExtension: "mmpk")!)
/// The timer used to update the time-to-expiration label.
var timeToExpirationTimer: Timer?
/// Initiates loading of the mobile map package.
func loadMobileMapPackage() {
mobileMapPackage.load { [weak self] (error) in
let result: Result<Void, Error>
if let error = error {
result = .failure(error)
} else {
result = .success(())
}
self?.mobileMapPackageDidLoad(with: result)
}
}
/// Called in response to the mobile map package load operation completing.
///
/// - Parameter result: The result of the load operation.
func mobileMapPackageDidLoad(with result: Result<Void, Error>) {
switch result {
case .success:
mapView.map = mobileMapPackage.maps.first
// Is the package expired? If so, then we'll want to show the
// expiration view.
if let expiration = mobileMapPackage.expiration, expiration.isExpired {
expirationMessageLabel.text = expiration.message
// Does the package have an expiration date? If so, we'll want
// to show the time-to-expiration label.
if expiration.dateTime != nil {
let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [unowned self] (_) in
self.updateTimeToExpiration()
}
timer.tolerance = 0.1
timer.fire()
timeToExpirationTimer = timer
timeToExpirationLabel.isHidden = false
}
expirationView.isHidden = false
}
case .failure(let error):
presentAlert(error: error)
}
}
/// Updates the time-to-expiration label with an expiration message relative
/// to the current time.
func updateTimeToExpiration() {
guard let expirationDate = mobileMapPackage.expiration?.dateTime else {
return
}
let dateComponents = Calendar.current.dateComponents([.year, .day, .hour, .minute, .second], from: expirationDate, to: Date())
let text: String
if let string = DateComponentsFormatter.localizedString(from: dateComponents, unitsStyle: .abbreviated) {
text = String(format: "Expired %@ ago.", string)
} else {
text = ""
}
self.timeToExpirationLabel.text = text
}
override func viewDidLoad() {
super.viewDidLoad()
// Add the source code button item to the right of navigation bar.
(self.navigationItem.rightBarButtonItem as? SourceCodeBarButtonItem)?.filenames = [
"HonorMobileMapPackageExpirationDateViewController"
]
}
deinit {
timeToExpirationTimer?.invalidate()
}
}