'system' is unavailable: not ava

2021-10-20  本文已影响0人  清宵寒夜

cocoa2d导出xcode工程用xcode11及以上版本编译报错:'system' is unavailable: not available on iOS,这是因为iOS11+废弃了system,解决方案如下:
一、修改文件CCFileUtils.cpp
1.1在#include <dirent.h>下新增如下代码(在1231行左右)

#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
#include <ftw.h>
#endif

namespace
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
    int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
    {
        int rv = remove(fpath);

        if (rv)
            perror(fpath);

        return rv;
    }
#endif
}

1.2 在bool FileUtils::removeDirectory(const std::string& path)方法下实现改成下面的方式

bool FileUtils::removeDirectory(const std::string& path)
{
#if !defined(CC_TARGET_OS_TVOS)

#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
    if (nftw(path.c_str(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS) == -1)
        return false;
    else
        return true;
#else
    std::string command = "rm -r ";
    // Path may include space.
    command += "\"" + path + "\"";
    if (system(command.c_str()) >= 0)
        return true;
    else
        return false;
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)

#else
    return false;
#endif
#endif // !defined(CC_TARGET_OS_TVOS)
}

二、修改文件cocos2d_lua/external/lua/Ioslib.c
2.1在#include "lualib.h"行下新增代码

    #include <ftw.h> 
   int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW     *ftwbuf) 
    { 
            int rv = remove(fpath); 
     
            if (rv) 
            perror(fpath); 
     
            return rv; 
    } 

2.2替换方法: lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));

lua_pushinteger(L, nftw(luaL_optstring(L, 1, NULL), unlink_cb, 64, FTW_DEPTH | FTW_PHYS));

上一篇下一篇

猜你喜欢

热点阅读