iOS - 在 C++ 中获取可写路径

iOS - Get a writable path in C++

我正在用原生 C++ 为 iOS 编写录音机,但我无法使用 Foundation API。

我需要这样的东西:

recordFilePath = 
(CFStringRef)[NSTemporaryDirectory() stringByAppendingPathComponent: @"recordedFile.caf"];

获取 iPhone 上的可写路径。

非常感谢。

评论OP后,使用Objective-C++的例子:

Utils.hpp:

#pragma once
#include <string>

std::string temporaryFilePath(const std::string &filename)

Utils.mm:

#import "Utils.hpp"
#import <Foundation/Foundation.h>

std::string temporaryFilePath(const std::string &filename)
{
    NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithUTF8String:filename.c_str()]];
    const char *rawFilePath = [filePath UTF8String];
    return rawFilePath;
}

WhereverYouWant.cpp:

#include "Utils.hpp"

...

std::string recordFilePath = temporaryFilePath("recordedFile.caf");

当然可以,但自信地说这是我客户的任务,他不要求任何外部功能或桥梁或任何涉及 Foundation 的核心 类。所以我不得不构建一个不引用 Foundation 的音频队列组件。

郑重声明,我这样做就成功了:

        const char *home = getenv("HOME");
        const char *subdir = "/Documents/";
        const char *file = "recordedFile.caf";

        char *recPath = (char*)(calloc(strlen(home) + strlen(subdir) 
                              + strlen(file) + 1, sizeof(char)));

        strcpy(recPath, home); // copy string one into the result.
        strcat(recPath, subdir); // append string two to the result.
        strcat(recPath, file); // append string three to the result.

        //recordFilePath is our CFStringRef
        recordFilePath = CFStringCreateWithCString(0, recPath, kCFStringEncodingUTF8);

        //recorder is an AQRecorder class like the one from SpeakHere code sample
          recorder->StartRecord(recordFilePath);