做越獄應用和插件開發(fā),經(jīng)常會調用 system 去執(zhí)行系統(tǒng)命令,早在 Xcode 7,使用 system 函數(shù)提示警告:
為新鄉(xiāng)等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及新鄉(xiāng)網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為做網(wǎng)站、網(wǎng)站建設、新鄉(xiāng)網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!'system' is deprecated: first deprecated in iOS 8.0 - Use posix_spawn APIs installd
只是警告,還是可以正常編譯和使用,但是升級到 Xcode 9,system 函數(shù)就從 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 函數(shù)不可,那就使用第三種方法,找到 system 函數(shù)地址直接調用,方法參見: 動態(tài)調用函數(shù),具體代碼如下:
typedef int (*my_system) (const char *str); int call_system(const char *str){ //動態(tài)庫路徑 char *dylib_path = "/usr/lib/libSystem.dylib"; //打開動態(tài)庫 void *handle = dlopen(dylib_path, RTLD_GLOBAL | RTLD_NOW); if (handle == NULL) { //打開動態(tài)庫出錯 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 函數(shù)就相當于 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
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享標題:system函數(shù)被廢除的替代方法-創(chuàng)新互聯(lián)
當前網(wǎng)址:http://aaarwkj.com/article0/ppsio.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、動態(tài)網(wǎng)站、App設計、響應式網(wǎng)站、營銷型網(wǎng)站建設、面包屑導航
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)