[Objective-C] 纯文本查看 复制代码
 //获取私有db文件
-(void)getVarPrivateDbFile:(void (^)(NSString *result))completion{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDirectoryEnumerator *dirEnumer = [fileManager enumeratorAtPath:@"/private"];
    NSString *nextItem = [NSString string];
    NSMutableArray *itemArr = [[NSMutableArray alloc] init];
    while (nextItem = [dirEnumer nextObject]) {
        if ([[nextItem pathExtension] isEqualToString:@"db"] || [[nextItem pathExtension] isEqualToString:@"sqlitedb"]) {
            [itemArr addObject:nextItem];
            
        }
    }
    
    [itemArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *dbPath = [NSString stringWithFormat:@"%@",obj];
        NSArray *pathComponents = [NSArray arrayWithArray:[dbPath pathComponents]];
        
        if ([pathComponents lastObject]) {
            NSString *fileName = [NSString stringWithFormat:@"%@",[pathComponents lastObject]];
            recordFileName = [[NSString alloc] initWithFormat:@"%@",fileName];   //文件名
            NSArray *tempCom = [fileName componentsSeparatedByString:@"."];
            if (tempCom && tempCom.count >0) {
                fileName = [tempCom firstObject];
                if ([fileName isEqualToString:@"call_history"]) {
                    NSLog(@"%@",dbPath);
                    historyPath = [[NSString alloc] initWithFormat:@"%@",dbPath];  //文件路径
                    completion(dbPath);
                }
            }
        }
    }];
}
-(void)readCallLogsWithFilePath:(NSString *)filePath{
    if (callLogArray == nil) {
        callLogArray = [[NSMutableArray alloc] init];
    }
    [callLogArray removeAllObjects];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL callLogFileExit = NO;
    callLogFileExit = [fileManager fileExistsAtPath:historyPath];
    if (callLogFileExit) {
        NSString *targetPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        targetPath  = [targetPath stringByAppendingPathComponent:recordFileName!=nil?recordFileName:@"callHistory.db"];
        [self writeFile:historyPath ToPath:targetPath];         //写入其他路径
        
        if ([fileManager isReadableFileAtPath:historyPath]) {
            
            sqlite3 *dataBase;
            if (sqlite3_open([filePath UTF8String], &dataBase) == SQLITE_OK) {
                sqlite3_stmt *SqlStmt;
                NSString *sqlSta = @"SELECT * FROM call;";
                int errorCode = sqlite3_prepare_v2(dataBase, [sqlSta UTF8String], -1, &SqlStmt, NULL);
                if (errorCode == SQLITE_OK) {
                    int count = 1;
                    
                    while (sqlite3_step(SqlStmt) == SQLITE_ROW) {
                        NSMutableDictionary *callLogItem = [NSMutableDictionary dictionary];
                        int numOfColums = sqlite3_column_count(SqlStmt);
                        NSString *data;
                        NSString *columnName;
                        
                        for (int  i =0; i<numOfColums; i++) {
                            columnName = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_name(SqlStmt, i)];
                            data = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(SqlStmt, i)];
                            
                            [callLogItem setObject:data forKey:columnName];
                        }
                        [callLogArray addObject:callLogItem];
                        count ++;
                    }
                }else{
                    NSLog(@"Failed to retrieve table");
                    NSLog(@"Error Code :%d",errorCode);
                }
            }
            
            NSLog(@"通话记录:%@",callLogArray);
        }
    }
}
    //文件写入
-(void)writeFile:(NSString *)filePath ToPath:(NSString *)path{
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];
    if (fileData.length >0) {
        if ([fileData writeToFile:path atomically:YES]) {
            NSLog(@"写入成功");
        }
    }
}