美文网首页iOS 逆
升级 Xcode 9.0 后在 iOS 11 中移除 syste

升级 Xcode 9.0 后在 iOS 11 中移除 syste

作者: 貘鸣 | 来源:发表于2017-09-22 17:47 被阅读245次

在项目中使用到了一个库, 里面调用了 system. system 调用主要是为了获取一个返回值 stat.

const char *cmd = luaL_optstring(L, 1, NULL);
int stat = system(cmd);
if (cmd != NULL)
        return luaL_execresult(L, stat);
    else {
        lua_pushboolean(L, stat);  /* true if there is a shell */
        return 1;
    }

然后在 Xcode9 里面, 当前 system 已经被移除了... 因为 baseSDK 升级到了 iOS 11.0:

__swift_unavailable_on("Use posix_spawn APIs or NSTask instead.", "Process spawning is unavailable")
__API_AVAILABLE(macos(10.0)) __IOS_PROHIBITED
__WATCHOS_PROHIBITED __TVOS_PROHIBITED
int  system(const char *) __DARWIN_ALIAS_C(system);

按照上面的说法, 需要用 posix_spawn API 或者是 NSTask 来代替 system.

故参考前人的解决方式, 准备替换掉 system 的调用.

首先引入头文件和一个外部变量:

#include <spawn.h>
extern char **environ;

然后再替换掉之前的 system 调用:
替换前:

static int os_execute (lua_State *L) {
    const char *cmd = luaL_optstring(L, 1, NULL);
    int stat = system(cmd);
    if (cmd != NULL)
        return luaL_execresult(L, stat);
    else {
        lua_pushboolean(L, stat);  /* true if there is a shell */
        return 1;
    }
}

替换后:

static int os_execute (lua_State *L) {
    const char *cmd = luaL_optstring(L, 1, NULL);
    pid_t pid;
    char* argv[]  =
    {
        cmd,
        NULL
    };
    int result = posix_spawn(&pid, argv[0], NULL, NULL, argv, environ);
    waitpid(pid, NULL, 0);
    if (cmd != NULL)
        return luaL_execresult(L, result);
    else {
        lua_pushboolean(L, result);  /* true if there is a shell */
        return 1;
    }
}

这个只是临时解决方案, 最好还是给作者提一个 issue...

相关文章

网友评论

    本文标题:升级 Xcode 9.0 后在 iOS 11 中移除 syste

    本文链接:https://www.haomeiwen.com/subject/ipifextx.html