All Products
Search
Document Center

Mobile Platform as a Service:Integrate iOS SDK

Last Updated:Oct 26, 2023

The Location-based Service (LBS) SDK is a set of simple LBS interfaces. You can use this set of LBS API to obtain positioning results.

Prerequisite

The project already gets access to mPaaS. For more information, see the following content: Integrate mPaaS based on an existing project and CocoaPods.

Add the SDK

Use CocoaPods plugin to add the Location-based Service SDK. Complete the following steps:

  1. In the Podfile file, use mPaaS_pod "mPaaS_LBS" to add mobile gateway component dependencies.image.png

  2. In the terminal, run pod install to complete integration.

Use the SDK

This topic describes how to use the LBS SDK in baseline 10.1.32 later versions on the official LBS demo.

The APMobileLBS module provides a method for obtaining the latitude and longitude of the current location.

Note: The LBS does not support reverse geographic query. To use reverse geocoding, you can call the AMAP API.

API description

To learn LBS API operations, see the following code and the parameter descriptions in the comments.

Use MPLBSConfiguration to set parameters

/**
 Configurations of the LBS
 */
@interface MPLBSConfiguration : NSObject

/** The expected precision of a single positioning operation, in meters. We recommend that you specify an acceptable positive number based on the business scenario. For example, set the parameter to 500 to indicate a range within 500 m. */
@property (nonatomic, assign) CLLocationAccuracy desiredAccuracy;

/** The time for caching received data of a single positioning operation, counting from the current time backward. We recommend that you specify a time longer than 30s. */
@property (nonatomic, assign) APCoreLocationCacheAvaliable cacheTimeInterval;

/** The timeout interval for a single positioning operation or reverse geographical query, in seconds. Default value: 2s. Minimum value: 2s. */
@property (nonatomic, assign) NSTimeInterval timeOut;

/** The level of the information obtained through reverse geographical query. Default value: APCoreLocationReGeoLevelDistrict. */
@property (nonatomic, assign) LBSLocationReGeoLevel reGeoLevel;

/** The location information obtained through reverse geographical query based on the latitude and longitude. */
@property (nonatomic, strong) CLLocation *reGeoLocation;

/** Specifies whether coordinates used for reverse geographical query are coordinates in AMAP. Default value: YES. This parameter is valid only when the reGeoLocation parameter is specified. */
@property (nonatomic, assign) BOOL reGeoCoordinateConverted;

/** Specifies whether to enable check-in. Default value: NO. If check-in is required, set this parameter to YES. */
@property (nonatomic, assign) BOOL needCheckIn;

/**
 * Specifies whether high-precision positioning is required. This parameter is not required for versions earlier than iOS 14. For iOS 14 and later, this parameter is set to NO (low precision) by default. If high-precision positioning is required, the business personnel must modify this parameter. 
 */
@property (nonatomic,assign) BOOL highAccuracyRequired;

@end

Use MPLBSLocationManager to initiate a positioning request

/**
 Callback blocks that are triggered based on positioning results

 @param success Whether positioning is successful
 @param locationInfo Location information
 @param error Error information of positioning failure
 */
typedef void(^MPLBSLocationCompletionBlock)(BOOL success,
                                            MPLBSLocationInfo *locationInfo,
                                            NSError *error);

/**
 LBS
 */
@interface MPLBSLocationManager : NSObject

/**
 Initialize

 @param configuration Parameter settings
 @return Instance
 */
- (instancetype)initWithConfiguration:(MPLBSConfiguration *)configuration;

/**
 Initiate a single positioning operation

 @param needReGeocode Indicates whether reverse geographic query is required. The LBS does not support reverse geographic query. You must set this parameter to NO. 
 @param block The callback block that is triggered after positioning ends.
 */
- (void)requestLocationNeedReGeocode:(BOOL)needReGeocode
                   completionHandler:(MPLBSLocationCompletionBlock)block;

MPLBSLocationInfo in callbacks

/**
 Reverse geographic information
 */
@interface MPLBSReGeocodeInfo : NSObject

@property (nonatomic, strong) NSString* country;        // The country.
@property (nonatomic, strong) NSString* countryCode;    // The country code.
@property (nonatomic, strong) NSString* province;      // The province.
@property (nonatomic, strong) NSString* city;           // The city.
@property (nonatomic, strong) NSString* district;       // The district.
@property (nonatomic, strong) NSString* street;         // The street.
@property (nonatomic, strong) NSString* streetCode;     // The street code.
@property (nonatomic, strong) NSString* cityCode;       // The city code.
@property (nonatomic, strong) NSString* adCode;         // The administrative district code.
@property (nonatomic, strong) NSArray* poiList;         // The POI information list.

@end

/**
 Data structure of location information in the positioning result
 */
@interface MPLBSLocationInfo : NSObject

@property (nonatomic, strong) CLLocation* location;         // The location information.
@property (nonatomic, strong) MPLBSReGeocodeInfo* rgcInfo;  // The reverse geographic information.

@end

Sample code

- (void)getLocation {
    MPLBSConfiguration *configuration = [[MPLBSConfiguration alloc] init];
    configuration.desiredAccuracy = kCLLocationAccuracyBest;

    self.locationManager = [[MPLBSLocationManager alloc] initWithConfiguration:configuration];
    [self.locationManager requestLocationNeedReGeocode:NO completionHandler:^(BOOL success, MPLBSLocationInfo * _Nonnull locationInfo, NSError * _Nonnull error) {
        NSString *message;
        if (success) {
            message = [NSString stringWithFormat:@"Positioning success. Longitude: %.5f. Latitude: %.5f. Precision: %.3f. Whether high precision is required: %d", locationInfo.location.coordinate.longitude, locationInfo.location.coordinate.latitude, locationInfo.location.horizontalAccuracy, !locationInfo.location.ap_lbs_is_high_accuracy_close];
        } else {
            message = [NSString stringWithFormat:@"%@", error];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            AUNoticeDialog *alert = [[AUNoticeDialog alloc] initWithTitle:@"Positioning result" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        });
    }];
}

Adaptation in iOS 14

In iOS 14, precise location is a permission option. You can turn on the option when you apply for positioning permissions. On the positioning permissions setting page, you can adjust the setting.

Adaptation of input parameters

In MPLBSConfiguration, add the highAccuracyRequired parameter. If highAccuracyRequired = YES is passed in, but high-precision positioning is disabled, a callback error occurs.

/**
 Configurations of the LBS
 */
@interface MPLBSConfiguration : NSObject

/**
 *  Specifies whether high-precision positioning is required. This parameter is not required for versions earlier than iOS 14. For iOS 14 and later, this parameter is set to NO (low precision) by default. If high-precision positioning is required, the business personnel must modify this parameter. 
 */
@property (nonatomic,assign) BOOL highAccuracyRequired;

@end
//If highAccuracyRequired = YES is passed in, but high-precision positioning is disabled, a callback error occurs.
Errorcode: APCoreLocationErrorCodeHighAccuracyAuthorization

Callback adaptation

If highAccuracyRequired = NO is passed in or high-precision positioning is not specified, the callback object CLLocation contains the ap_lbs_is_high_accuracy_close field. This field indicates whether high-precision positioning is disabled.

// Reconstructs the output parameter.
@interface CLLocation (APMobileLBS)
/*
 *  Specifies whether to disable high-precision positioning. Default value: NO
 */
@property(nonatomic,assign)BOOL ap_lbs_is_high_accuracy_close;
@end

Sample code

- (void)getLocationWithHighAccuracy {
    MPLBSConfiguration *configuration = [[MPLBSConfiguration alloc] init];
    configuration.desiredAccuracy = kCLLocationAccuracyBest;
    configuration.highAccuracyRequired = YES;

    self.locationManager = [[MPLBSLocationManager alloc] initWithConfiguration:configuration];
    [self.locationManager requestLocationNeedReGeocode:NO completionHandler:^(BOOL success, MPLBSLocationInfo * _Nonnull locationInfo, NSError * _Nonnull error) {
        NSString *message;
        if (success) {
            message = [NSString stringWithFormat:@"Positioning success. Longitude: %.5f. Latitude: %.5f. Precision: %.3f. Whether high precision is required: %d", locationInfo.location.coordinate.longitude, locationInfo.location.coordinate.latitude, locationInfo.location.horizontalAccuracy, !locationInfo.location.ap_lbs_is_high_accuracy_close];
        } else {
            message = [NSString stringWithFormat:@"%@", error];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            AUNoticeDialog *alert = [[AUNoticeDialog alloc] initWithTitle:@"Positioning result" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        });
    }];
}