system函数被废除的替代⽅法
做越狱应⽤和插件开发,经常会调⽤ system 去执⾏系统命令,早在 Xcode 7,使⽤ system 函数提⽰警告:
'system' is deprecated: first deprecated in iOS 8.0 - Use posix_spawn APIs installd
只是警告,还是可以正常编译和使⽤,但是升级到 Xcode 9,system 函数就从 SDK 中移除了,不能再使⽤了,提⽰:'system' is unavailable: not available on iOS
替代的⽅法⼀般有三种,第⼀种是使⽤ posix_spawn,代码如下:
pid_t pid;
char *argv[] = {
"/bin/ls", //path
"-al", //parameter1
"/", //parameter2
NULL
};
posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL);
printf("pid=%d,child pid = %d\n",getpid(),pid);
int stat;
waitpid(pid,&stat,0);
printf("stat is %d\n",stat);
第⼆种是使⽤ NSTask,代码如下:
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/bin/ls";
task.arguments = [NSArray arrayWithObjects:
@"-al",
@"/",
nil];
[task launch];
[task waitUntilExit];
NSTask.h 头⽂件信息如下:
#import <Foundation/NSObject.h>
@class NSString, NSArray, NSDictionary;
@interface NSTask : NSObject
// Create an NSTask which can be run at a later time
// An NSTask can only be run once. Subsequent attempts to
/
/ run an NSTask will raise.
// Upon task death a notification will be sent
// { Name = NSTaskDidTerminateNotification; object = task; }
//
- (instancetype)init;
// set parameters
// these methods can only be done before a launch
// if not set, use current
// if not set, use current
// set standard I/O channels; may be either an NSFileHandle or an NSPipe
- (void)setStandardInput:(id)input;
-
(void)setStandardOutput:(id)output;
- (void)setStandardError:(id)error;
// get parameters
@property (NS_NONATOMIC_IOSONLY, copy) NSString *launchPath;
@property (NS_NONATOMIC_IOSONLY, copy) NSArray *arguments;
@property (NS_NONATOMIC_IOSONLY, copy) NSDictionary *environment;
@property (NS_NONATOMIC_IOSONLY, copy) NSString *currentDirectoryPath;
// get standard I/O channels; could be either an NSFileHandle or an NSPipe
- (id)standardInput;
- (id)standardOutput;
- (id)standardError;
/
/ actions
- (void)launch;
- (void)interrupt; // Not always possible. Sends SIGINT.
- (void)terminate; // Not always possible. Sends SIGTERM.
@property (NS_NONATOMIC_IOSONLY, readonly) BOOL suspend;
@property (NS_NONATOMIC_IOSONLY, readonly) BOOL resume;
// status
@property (NS_NONATOMIC_IOSONLY, readonly) int processIdentifier;
@property (NS_NONATOMIC_IOSONLY, getter=isRunning, readonly) BOOL running;
@property (NS_NONATOMIC_IOSONLY, readonly) int terminationStatus;
@end
@interface NSTask (NSTaskConveniences)
+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments;
// convenience; create and launch
- (void)waitUntilExit;
// poll the runLoop in defaultMode until task completes
@end
FOUNDATION_EXPORT NSString * const NSTaskDidTerminateNotification;
如果⾮要调⽤ system 函数不可,那就使⽤第三种⽅法,到 system 函数地址直接调⽤,⽅法参见: ,具体代码如下:
typedef int (*my_system) (const char *str);
int call_system(const char *str){system的头文件
//动态库路径
char *dylib_path = "/usr/lib/libSystem.dylib";
//打开动态库
void *handle = dlopen(dylib_path, RTLD_GLOBAL | RTLD_NOW); if (handle == NULL) {
//打开动态库出错
fprintf(stderr, "%s\n", dlerror());
} else {
//获取 system 地址
my_system system = dlsym(handle, "system");
//地址获取成功则调⽤
if (system) {
int ret = system(str);
return ret;
}
dlclose(handle); //关闭句柄
}
return -1;
}
这样 call_system 函数就相当于 system 的功能了,替换即可。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论