4. 宿主程序存储数据管理器代码

/** 1.更新数据 */
- (void)updateDataWithCompletion:(nullable void (^)(NSError *_Nullable error))completion{
    CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
    [manager reloadExtensionWithIdentifier:IdentifierExtension completionHandler:^(NSError * _Nullable error) {
        NSLog(@"%s %@", __FUNCTION__, error);
        completion(error);
    }];
}

/** 2.获取权限 */
- (void)getEnabledStatusWithCompletionHandler:(void (^)(CXCallDirectoryEnabledStatus enabledStatus, NSError *_Nullable error))completion{
    CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
    [manager getEnabledStatusForExtensionWithIdentifier:IdentifierExtension completionHandler:^(CXCallDirectoryEnabledStatus enabledStatus, NSError * _Nullable error) {
        completion(enabledStatus, error);
    }];
}

/** 3.保存通讯录来电信息 */
- (void)saveDataWithCompletion:(void(^)(BOOL success))completion{
    NSString *filePathIdentification = [self readPathIdentification];
    //    [[NSFileManager defaultManager] removeItemAtPath:filePathIdentification error:nil];
    if (self.arrayCall.count > 0) {
        BOOL success = [self.arrayCall writeToFile:filePathIdentification atomically:YES];
        if (success) {
            completion(YES);
        }else {
            completion(NO);
        }
    }else{
        completion(YES);
    }
}
- (void)saveKHDataWithCompletion:(void(^)(BOOL success))completion{
    NSString *filePathIdentification = [self readKeHuPlistPath];
    //    [[NSFileManager defaultManager] removeItemAtPath:filePathIdentification error:nil];
    if (self.khCall.count > 0) {
        BOOL success = [self.khCall writeToFile:filePathIdentification atomically:YES];
        if (success) {
            completion(YES);
        }else {
            completion(NO);
        }
    }else{
        completion(YES);
    }
}
- (void)saveLXRDataWithCompletion:(void(^)(BOOL success))completion{
    NSString *filePathIdentification = [self readLXRPlistPath];
    //    [[NSFileManager defaultManager] removeItemAtPath:filePathIdentification error:nil];
    if (self.lxrCall.count > 0) {
        BOOL success = [self.lxrCall writeToFile:filePathIdentification atomically:YES];
        if (success) {
            completion(YES);
        }else {
            completion(NO);
        }
    }else{
        completion(YES);
    }
}

/** 4.读取来电信息 */
-(NSArray *)readData
{
    NSString *filePathIdentification = [self readPathIdentification];
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePathIdentification];
    NSDictionary *dic = [arr firstObject];
    NSArray *dataArr = [dic allKeys];
    return [self arraySort:dataArr ASC:YES];
}
-(NSArray *)readKHData
{
    NSString *filePathIdentification = [self readKeHuPlistPath];
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePathIdentification];
    NSDictionary *dic = [arr firstObject];
    NSArray *dataArr = [dic allKeys];
    return [self arraySort:dataArr ASC:YES];
}
-(NSArray *)readLXRData
{
    NSString *filePathIdentification = [self readLXRPlistPath];
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePathIdentification];
    NSDictionary *dic = [arr firstObject];
    NSArray *dataArr = [dic allKeys];
    return [self arraySort:dataArr ASC:YES];
}

/** 1.获取通讯录扩展文件位置 */
- (NSString *)readPathIdentification{
    NSURL *fileUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@""];
    NSString *filePath = [fileUrl.absoluteString substringFromIndex:(@"file://".length)];
    NSString *filePathIdentification = [filePath stringByAppendingString:@""];
    return filePathIdentification;
    
}
//获取客户Plist路径
-(NSString *)readKeHuPlistPath{
    NSURL *fileUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@""];
    NSString *filePath = [fileUrl.absoluteString substringFromIndex:(@"file://".length)];
    NSString *filePathIdentification = [filePath stringByAppendingString:@""];
    
    return filePathIdentification;
}
//获取客户Plist路径
-(NSString *)readLXRPlistPath{
    NSURL *fileUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@""];
    NSString *filePath = [fileUrl.absoluteString substringFromIndex:(@"file://".length)];
    NSString *filePathIdentification = [filePath stringByAppendingString:@""];
    
    return filePathIdentification;
}

/** 2.数组排序 */
- (NSArray *)arraySort:(NSArray *)array ASC:(BOOL)ASC
{
    
    if (array == nil || [array count] == 0) {
        return nil;
    }
    
    return [array sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
        if(ASC){
            if([obj1 integerValue] > [obj2 integerValue]) {
                return(NSComparisonResult)NSOrderedDescending;
            }
            if([obj1 integerValue] < [obj2 integerValue]) {
                return(NSComparisonResult)NSOrderedAscending;
            }
        }else{
            if([obj1 integerValue] < [obj2 integerValue]) {
                return(NSComparisonResult)NSOrderedDescending;
            }
            if([obj1 integerValue] > [obj2 integerValue]) {
                return(NSComparisonResult)NSOrderedAscending;
            }
        }
        return(NSComparisonResult)NSOrderedSame;
    }];
}

- (NSMutableArray *)arrayCall
{
    if (!_arrayCall) {
        if ([self readData].count > 0) {
            _arrayCall = [self readData].mutableCopy;
        }else {
            _arrayCall = @[].mutableCopy;
        }
    }
    return _arrayCall;
}
-(NSMutableArray *)khCall{
    if (!_khCall) {
        if ([self readKHData].count > 0) {
            _khCall = [self readKHData].mutableCopy;
        }else {
            _khCall = @[].mutableCopy;
        }
    }
    return _khCall;
}
-(NSMutableArray *)lxrCall{
    if (!_lxrCall) {
        if ([self readLXRData].count > 0) {
            _lxrCall = [self readLXRData].mutableCopy;
        }else{
            _lxrCall = @[].mutableCopy;
        }
    }
    return _lxrCall;
}

你可能感兴趣的:(4. 宿主程序存储数据管理器代码)