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){
    
    //动态库路径
    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 的功能了,替换即可。


原文地址:https://www.exchen.net/ios-hacker-system-%E5%87%BD%E6%95%B0%E8%A2%AB%E5%BA%9F%E9%99%A4%E7%9A%84%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%B3%95.html


另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享标题:system函数被废除的替代方法-创新互联
当前网址:https://www.cdcxhl.com/article0/ppsio.html

成都网站建设公司_创新互联,为您提供网站制作动态网站App设计响应式网站营销型网站建设面包屑导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设