Solution to "call to unavailable function 'system': not available on iOS"

From iOS 11 if we call system() we get something like:

error: call to unavailable function 'system': not
      available on iOS
        system("reboot");
        ^~~~~~
/opt/theos/sdks/iPhoneOS11.2.sdk/usr/include/stdlib.h:195:6: note: 
      candidate function has been explicitly made unavailable
int      system(const char *) __DARWIN_ALIAS_C(system);
         ^
1 error generated.

An alternative is to use posix_spawn instead like this:

#import <spawn.h>

extern char **environment;

int run_cmd(const char *cmd)
{
	pid_t pid;
	const char *argv[] = {"sh", "-c", cmd, NULL};

	int status = posix_spawn(&pid, "/bin/sh", NULL, NULL, (char* const*)argv, environment);
	if (status == 0) {
		if (waitpid(pid, &status, 0) == -1) {
			perror("waitpid");
		}
	}

	return status;
}
void your_function(void)
{
	run_cmd("reboot");
}
4 个赞

顶顶:smile:

It does exist but is marked as __IOS_PROHIBITED

狗神 这样定义环境变量 extern char **environment; 编译的时候报这样的错误
“_environment”, referenced from:
-[xxx xxx] in xxx.m.274b3df1.o

折腾一段时间后, 发现这样定义
extern char **environ , 就能编译通过 , 不知道为什么, 希望前辈指点

好吧 =-=

这个错了或者是改了Extend to static,命令不生效,你这不提示我还真找不到边了。

直接修改stdlib.h就行了
修改前:


修改后:

少删了一行192 问题不大

去掉函数的调用限制又没啥。。。

iOS13使用没问题,在iOS14里面好像没用