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 a
MobileMapPackagepassing in the path to the mobile map package’s location on the device. - Load the mobile map package.
- Present
Expirationinformation to the user with theExpiration::message()andExpiration::dateTime()getters, which are set by the map author.
Relevant API
- Expiration
- MobileMapPackage
Offline Data
Read more about how to set up the sample’s offline data here.
| Link | Local Location |
|---|---|
| Lothian Rivers Anno MMPK | <userhome>/ArcGIS/Runtime/Data/mmpk/LothianRiversAnno.mmpk |
Tags
expiration, mmpk
Sample Code
// [WriteFile Name=HonorMobileMapPackageExpiration, Category=Maps]// [Legal]// 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.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "HonorMobileMapPackageExpiration.h"
// ArcGIS Maps SDK headers#include "CoreTypes.h"#include "Error.h"#include "Expiration.h"#include "MapQuickView.h"#include "MobileMapPackage.h"
// Qt headers#include <QDateTime>#include <QStandardPaths>#include <QtGlobal>
using namespace Esri::ArcGISRuntime;
// helper method to get cross platform data pathnamespace {QString defaultDataPath(){ QString dataPath;
#ifdef Q_OS_IOS dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);#else dataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);#endif
return dataPath;}}
// sample MMPK locationconst QString sampleMmpk {"/ArcGIS/Runtime/Data/mmpk/LothianRiversAnno.mmpk"};
HonorMobileMapPackageExpiration::HonorMobileMapPackageExpiration(QObject* parent /* = nullptr */): QObject(parent), m_dataPath(defaultDataPath() + sampleMmpk){ // connect to the Mobile Map Package instance to know when errors occur connect(MobileMapPackage::instance(), &MobileMapPackage::errorOccurred, [](const Error& e) { if (e.isEmpty()) { return; }
qDebug() << QString("Error: %1 %2").arg(e.message(), e.additionalMessage()); });
}
HonorMobileMapPackageExpiration::~HonorMobileMapPackageExpiration() = default;
void HonorMobileMapPackageExpiration::init(){ // Register the map view for QML qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<HonorMobileMapPackageExpiration>("Esri.Samples", 1, 0, "HonorMobileMapPackageExpirationSample");}
MapQuickView* HonorMobileMapPackageExpiration::mapView() const{ return m_mapView;}
// Set the view (created in QML)void HonorMobileMapPackageExpiration::setMapView(MapQuickView* mapView){ if (!mapView || mapView == m_mapView) return;
m_mapView = mapView;
emit mapViewChanged();
createMapPackage(m_dataPath);}
void HonorMobileMapPackageExpiration::createMapPackage(const QString& path){ // instatiate a mobile map package m_mobileMapPackage = new MobileMapPackage(path, this);
// wait for the mobile map package to load connect(m_mobileMapPackage, &MobileMapPackage::doneLoading, this, [this](const Error& error) { if (!error.isEmpty()) { qDebug() << QString("Package load error: %1 %2").arg(error.message(), error.additionalMessage()); return; }
if (!m_mobileMapPackage || !m_mapView || m_mobileMapPackage->maps().isEmpty()) { return; }
// Access Expiration Info Expiration expiration = m_mobileMapPackage->expiration(); if (!expiration.isEmpty()) { if (expiration.isExpired()) { const QString message = expiration.message(); const QString date = expiration.dateTime().toString(Qt::ISODate).split("T")[0]; const QString time = expiration.dateTime().time().toString(); m_expirationString = QString("%1\nExpired on %2 at %3 UTC").arg(message, date, time); emit expirationStringChanged();
// return if access after expiration is not allowed if (expiration.type() == ExpirationType::PreventExpiredAccess) return; } }
// set the map view's map to the first map in the mobile map package m_mapView->setMap(m_mobileMapPackage->maps().at(0)); });
m_mobileMapPackage->load();}// [WriteFile Name=HonorMobileMapPackageExpiration, Category=Maps]// [Legal]// 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.// [Legal]
#ifndef HONORMOBILEMAPPACKAGEEXPIRATION_H#define HONORMOBILEMAPPACKAGEEXPIRATION_H
// Qt headers#include <QObject>
namespace Esri::ArcGISRuntime{class MapQuickView;class MobileMapPackage;}
Q_MOC_INCLUDE("MapQuickView.h")
class HonorMobileMapPackageExpiration : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(QString expirationString MEMBER m_expirationString NOTIFY expirationStringChanged)
public: explicit HonorMobileMapPackageExpiration(QObject* parent = nullptr); ~HonorMobileMapPackageExpiration();
static void init();
signals: void mapViewChanged(); void expirationStringChanged();
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); void createMapPackage(const QString& path);
Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::MobileMapPackage* m_mobileMapPackage = nullptr;
QString m_dataPath; QString m_expirationString;};
#endif // HONORMOBILEMAPPACKAGEEXPIRATION_H// [WriteFile Name=HonorMobileMapPackageExpiration, Category=Maps]// [Legal]// 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.// [Legal]
import QtQuickimport QtQuick.Controlsimport Esri.Samples
Item {
// add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } }
// Declare the C++ instance which creates the map etc. and supply the view HonorMobileMapPackageExpirationSample { id: model mapView: view }
Rectangle { anchors { verticalCenter: parent.verticalCenter left: parent.left right: parent.right } height: 100 color: "#303030" opacity: 0.75 visible: model.expirationString.length > 0
Text { id: expirationText anchors.centerIn: parent width: parent.width horizontalAlignment: Text.AlignHCenter wrapMode: Text.WordWrap color: "white" text: model.expirationString } }}// [Legal]// 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.// [Legal]
// sample headers#include "HonorMobileMapPackageExpiration.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("HonorMobileMapPackageExpiration"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample HonorMobileMapPackageExpiration::init();
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive);#endif
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path engine.addImportPath(arcGISRuntimeImportPath);
// Set the source engine.load(QUrl("qrc:/Samples/Maps/HonorMobileMapPackageExpiration/main.qml"));
return app.exec();}// Copyright 2019 ESRI//// All rights reserved under the copyright laws of the United States// and applicable international laws, treaties, and conventions.//// You may freely redistribute and use this sample code, with or// without modification, provided you include the original copyright// notice and use restrictions.//// See the Sample code usage restrictions document for further information.//
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
HonorMobileMapPackageExpiration { anchors.fill: parent }}