Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Key points of this paper:

  • Restrict merchants from locating their business addresses outside mainland China

Judge by latitude and longitude. Use autonavi SDK to judge. (If the location information is manually input, inverse geocoding is carried out to obtain the latitude and longitude for judgment)

  • Optimal location permissions detection logic: kCLAuthorizationStatusNotDetermined case processing

User does not select whether to use location service (popbox is not selected, or no popbox at all)

[AMapLocationKit] to use in iOS version 11 or more background location services, you need to implement amapLocationManager: doRequireLocationAuth: proxy method 】

I limit environment _ external fixed _ bit

Principle of restricting the location of the business address of the merchant outside the mainland: Determine whether the merchant is located in the mainland according to the latitude and longitude

1.1 Determine whether it is in continental area according to latitude and longitude

  • AMapLocationDataAvailableForCoordinate

/** Determine whether it is in continental area according to latitude and longitude */
+ (BOOL)inChineseMainlandWithCLLocation:(CLLocation *)location regeocode:(AMapLocationReGeocode *)regeocode{
    
    
    
    if(AMapLocationDataAvailableForCoordinate(location.coordinate)){// The current location is in mainland China, Hong Kong and Macao
        // Hong Kong Special Administrative Region
Macao Special Administrative Region
        ///// province/municipality
// @property (nonatomic, copy) NSString *province;

        if([regeocode.province isEqualToString:@" Hong Kong SAR"] || [regeocode.province isEqualToString:@" Macao SPECIAL Administrative Region"]) {return NO;

            
        }else{
            return YES; }}else{// Other regions
        
        return NO;

    }
    return YES;
}

Copy the code

usage

                

                CLLocation *location = [[CLLocation alloc]initWithLatitude:[array.lastObject doubleValue] longitude:[array.firstObject doubleValue]];
                
                AMapLocationReGeocode* regeocode = [AMapLocationReGeocode mj_objectWithKeyValues:geocodesDto.mj_keyValues];
                
                if([ERPAMapLocationTool inChineseMainlandWithCLLocation:location regeocode:regeocode]){
                    
                }else{
                    
                    [SVProgressHUD showInfoWithStatus:"Your latest location is not supported!"];

                    
                    return;
                }
                

                [weakSelf locotionRequestLat:[NSNumber numberWithDouble:location.coordinate.latitude].description Lon:[NSNumber numberWithDouble:location.coordinate.longitude].description adress:weakSelf.locationView.adressTextView.text];

Copy the code

1.2 Determine whether the fixed _ bit is in by inverse geocodingLarge _ lu

  • Reverse geocoding: convert latitude and longitude into detailed structured addresses, and return POI and AOI information of the surrounding area.

For example: 116.480881,39.989410 Translated address description: 6 Futong East Street, Chaoyang District, Beijing

  • Reverse geocoding is used to determine whether the processing flow is in the continent

1, request the business address location information of the user to enter the https://restapi.amap.com/v3/geocode/geo to latitude and longitude information 2, according to the latitude and longitude of the information and then to determine whether in mainland China

/** Use reverse geocoding to determine if it is mainland */
- (void)setupGeocode{

    __weak __typeof__(self) weakSelf = self;

    if (weakSelf.locationView.adressTextView.text.length > 0) {/ / 1, the judge management address information, not to reverse geocoding is empty (request ` https://restapi.amap.com/v3/geocode/geo ` the business address location information of user input to the latitude and longitude information)
        [SVProgressHUD showWithStatus:@" Locating.."];
        AFHTTPSessionManager *managers = [AFHTTPSessionManager manager];
        managers.requestSerializer = [AFHTTPRequestSerializer serializer];

        managers.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html".@"text/plain".@"application/json".@"text/javascript".nil];
        
        
        
// NSString *urlStr = [NSString stringWithFormat:@"https://restapi.amap.com/v3/geocode/geo"];
        NSString *urlStr = k_amap_geocode;
        
        
        NSDictionary *dict = @{@"key" : @"6ccba78b7" , @"address" : weakSelf.locationView.adressTextView.text};
        
        
        
        [managers GET:urlStr parameters:dict headers:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
            
            NSLog(@"downloadProgress: %@",downloadProgress);
            [SVProgressHUD dismiss];
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            
            NSLog(@" Merchant entry → Merchant details, reset _, if the entered address is only country + city (for example: Zhejiang, China), click OK, the conversion will fail when requesting the third-party geocoding information (Autonavi API), and the longitude and latitude information cannot be obtained. Failed to obtain latitude and longitude, please input detailed address!  responseObject: %@",responseObject);
            
            //status
// Returns the result status value
// The return value is 0 or 1. 0 indicates that the request failed. 1 indicates that the request is successful
            
            
            CRMgeoDto *dto = [CRMgeoDto mj_objectWithKeyValues:responseObject];
            
            if(dto.status.integerValue == 1) {// Obtain latitude and longitude, if failed, prompt [obtain latitude and longitude failed, please input accurate business address!
                
                
                void (^noLocationdataBlock)(void) = ^void(void) {
                    
                    [SVProgressHUD showInfoWithStatus:@" failed to get latitude and longitude, please enter the correct business address!"];
                     

                };
                
// responseObject: {
// status = 1;
// info = OK;
// infocode = 10000;
// count = 0;
// geocodes = (
/ /);
                if(dto.geocodes.count<=0){
                    
                    noLocationdataBlock();
                    return ;
                }
                
                CRMgeocodesDto *geocodesDto = dto.geocodes.firstObject;
                
                if([NSStringQCTtoll isBlankString:geocodesDto.location]){
                    
                    noLocationdataBlock();

                    return ;

                    
                }

                NSArray *array = [responseObject[@"geocodes"] [0] [@"location"] componentsSeparatedByString:@ ","];
                
                if(array.count<=1){
                    noLocationdataBlock();

                    return;
                }
                

                
                //2. Determine whether it is in mainland according to the obtained latitude and longitude information
                

                CLLocation *location = [[CLLocation alloc]initWithLatitude:[array.lastObject doubleValue] longitude:[array.firstObject doubleValue]];
                
                
                AMapLocationReGeocode* regeocode = [AMapLocationReGeocode mj_objectWithKeyValues:geocodesDto.mj_keyValues];
                
                if([ERPAMapLocationTool inChineseMainlandWithCLLocation:location regeocode:regeocode]){
                    
                    
                }else{
                    
                    [SVProgressHUD showInfoWithStatus:"Your latest location is not supported!"];
                    
                    return;
                }

                [weakSelf locotionRequestLat:[NSNumber numberWithDouble:location.coordinate.latitude].description Lon:[NSNumber numberWithDouble:location.coordinate.longitude].description adress:weakSelf.locationView.adressTextView.text];
                
                

                [weakSelf.locationView.adressTextView resignFirstResponder];
                
                
                weakSelf.locationView.frame = CGRectMake(0, kHeight, kWidth, KWratio(self.locationViewH));
                
                
                
                weakSelf.bacV.hidden = YES;

                
                
            }else{
                
                [SVProgressHUD showInfoWithStatus:Please enter the correct business address!];

            }
            
                
                
                
                

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [SVProgressHUD showInfoWithStatus:"Network error!"];
        }];
    } else {// Business address is empty
        [SVProgressHUD showInfoWithStatus:@" Please enter business address"]; }}Copy the code

II Test verification

Test method: modify bit _ information

Method 1: Use i4Tools to change the IP address of the real machine

  • With the help of i4Tools
Install command line tools code: xcode - select install since signing command code: codesign - f - s - - the deep/Applications/i4Tools appCopy the code

Method 2: Use the emulator to change the address

  • The test data
// province= Hong Kong Special Administrative Region
 latitude=22.26429541107516
 longitude=114.18135238995905
// province= Macao Special Administrative Region

 latitude=22.135584619138957
 longitude=113.57214697205735

2020- 04- 09 15:39:54.504424+0800 retail[8369:4238323] reGeocode: AMapLocationReGeocode: {formattedAddress: st. Francis hall area of the Macao special administrative region near the macau youth camp challenge; Country: China; Province: Macao Special Administrative Region; city:(null); The District of St. Francis of Assisi citycode:1853; adcode:820008; street:(null); number:(null); POIName: Macau Youth Challenge Camp; AOIName:(null); }121.524582.25.028822
2020- 04- 09 16:02:47.411507+0800 retail[8369:4238323] reGeocode: AMapLocationReGeocode: {formattedAddress: Taiwan (private) private seven Tian Zhen right brain education nursery; Country: China; Province C. city:(null); district:(null); citycode:1886; adcode:710000; street:(null); number:(null); POIName:(private) private shichida super right brain education nursery; AOIName:(null); }Copy the code

[for more latitude and longitude data: https://lbs.amap.com/console/show/picker]

Method 3: Modify the latitude and longitude information through the GPX file

IOS test tips: Modify latitude and longitude information with GPX files (simulate the location of iOS devices)

Related articles

III The detection logic of fixed _ bit permission

Optimized detection logic for fixed _ bit permissions:

IOS 13 new App location access “only allow once”

Test method: first set “Allow when using APP”, and then go to the system Settings to modify the permission of location information to “Ask next time”. Go back to your app and update your location information.

Positioning is kCLAuthorizationStatusNotDetermined permissions found before, You need to implement the amapLocationManager (amapLocationManager *) Manager doRequireLocationAuth (CLLocationManager*)locationManager method, To let the user choose

- (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager*)locationManager
{
    [locationManager requestAlwaysAuthorization];
}

Copy the code

IV. Risk merchant processing

Types of risks:

  • Theft is at risk of non-payment
  • Suspected fraud, electric fraud, cash risk
  • Gambling, abnormal amount, abnormal test:
  • Cross-border risk

For more, check out # Applets: iOS Reverse

see also

IOS orientation, the use of geographical/reverse geocoding: https://kunnan.blog.csdn.net/article/details/119685612