Location Messaging in iOS Apps that use SDK Version 10

The steps for implementing location messaging have changed in version 10 of the SDK. To upgrade your app to version 10 of the SDK, first update the initialization code. Then, use these code examples to update the location messaging code in your app.

Enable Location Tracking 

Use this code example to enable location tracking. This step is required to send geofence and beacon messages to your app.

1MarketingCloudSdk.requestSdk { mc in
2    mc?.startWatchingLocation()
3}

Get Device Location 

This code example shows how to get the device’s last known location using version 10 of the SDK.

1MarketingCloudSdk.requestSdk { mc in
2  let location = mc?.lastKnownLocation()
3}

Background Location Updates 

Implement the handler for background location updates in your application delegate class. This code example shows how to implement the handler using version 10 of the SDK.

1MarketingCloudSdk.requestSdk { mc in
2  let location = mc?.lastKnownLocation()
3}

Segment Location Messages 

This code example shows how to implement location message segmentation using version 10 of the SDK.

1// Adopt the protocol in your app’s class, which implements the
2// sfmc_shouldShowLocationMessage:forRegion: method
3class AppDelegate: UIResponder, UIApplicationDelegate, LocationDelegate
4
5...
6// After configuring the SDK, set the location delegate to the class that
7// implements the sfmc_shouldShowLocationMessage:forRegion: method
8MarketingCloudSdk.requestSdk { mc in
9  mc?.setLocationDelegate(self)
10}
11
12// Implement the method to evaluate app logic to determine if a message should
13// be shown. This example uses MobilePush attributes and message custom keys to
14// implement simple matching logic. Your implementation will vary according to
15// your unique needs.
16func sfmc_shouldShowLocationMessage(
17  _ message: [AnyHashable : Any],
18  forRegion region: [AnyHashable : Any]
19) -> Bool {
20  var shouldShow = false
21  var attributesLoyaltyLevel = ""
22
23  // Get a MobilePush contact attribute previously set by your app from the SDK
24  // In this example, "loyaltyLevel" indicates a customer contact value that
25  // your app uses.
26  var attributes: [AnyHashable : Any]? = nil
27  MarketingCloudSdk.requestSdk { mc in
28      attributes = mc?.attributes()
29  }
30
31  if attributes?.index(forKey: "loyaltyLevel") != nil {
32    attributesLoyaltyLevel = attributes!["loyaltyLevel"] as! String
33  }
34
35  // get the custom key’s for this MobilePush Location message
36  if (message["keys"] != nil) {
37    let customKeys = message["keys"] as! [AnyHashable : Any]
38    // get the "loyaltyLevel" custom key’s value
39    let messageLoyaltyLevel = customKeys["loyaltyLevel"] as! String
40
41    // in this example, if the customer contact attribute is set to "gold"
42    // AND the message’s custom key is "gold", return true - this message
43    // is valid to show for this customer
44
45    // the SDK will display the location message, as the critera passed
46    if (attributesLoyaltyLevel == "gold" && messageLoyaltyLevel == "gold") {
47        shouldShow = true
48    }
49  }
50  return shouldShow
51}