上一篇我寫了在xcode里面添加run script
來執(zhí)行腳本勿璃,可是打包之后發(fā)現(xiàn),只有用xcode
運(yùn)行的時(shí)候才會(huì)執(zhí)行這個(gè)shell
命令晚凿,查了很多資料,最后我覺得應(yīng)該是我理解錯(cuò)了瘦馍,這里的run script
很可能只是為了方便自動(dòng)化打包之類的場景適用歼秽,而并不是真正的運(yùn)行時(shí)執(zhí)行腳本。那該怎么實(shí)現(xiàn)呢情组?
參閱博客一
參閱博客二
參閱博客三
- (NSString *)cmd:(NSString *)cmd
{
// 初始化并設(shè)置shell路徑
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];
// -c 用來執(zhí)行string-commands(命令字符串)燥筷,也就說不管后面的字符串里是什么都會(huì)被當(dāng)做shellcode來執(zhí)行
NSArray *arguments = [NSArray arrayWithObjects: @"-c", cmd, nil];
[task setArguments: arguments];
// 新建輸出管道作為Task的輸出
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
// 開始task
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
// 獲取運(yùn)行結(jié)果
NSData *data = [file readDataToEndOfFile];
return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}
但是有一個(gè)問題,因?yàn)槟_本是:
#!/bin/bash
rm -rf ~/Downloads/keystore.xml
#chmod -x write.sh
/usr/local/bin/adb pull /data/data/com.whatsapp/shared_prefs/keystore.xml ~/Downloads/keystore.xml
cat ~/Downloads/keystore.xml
adb需要管理員權(quán)限院崇,所以一直提示沒有權(quán)限執(zhí)行腳本肆氓。
NSString *script = [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];
查了一下資料,可以用with administrator privileges
,這時(shí)候又報(bào)"用戶名密碼不正確"亚脆,不知道為啥,最終拋棄了NSTask
,找到了這個(gè)庫,它自動(dòng)幫我們處理權(quán)限問題盲泛,最終:
- (void)runSTPrivilegedTask {
NSString *cmd = @"/bin/sh launcher.sh";
STPrivilegedTask *privilegedTask = [[STPrivilegedTask alloc] init];
NSMutableArray *components = [[cmd componentsSeparatedByString:@" "] mutableCopy];
NSString *launchPath = components[0];
[components removeObjectAtIndex:0];
[privilegedTask setLaunchPath:launchPath];
[privilegedTask setArguments:components];
[privilegedTask setCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
// Set it off
OSStatus err = [privilegedTask launch];
if (err != errAuthorizationSuccess) {
if (err == errAuthorizationCanceled) {
NSLog(@"User cancelled");
return;
} else {
NSLog(@"Something went wrong: %d", (int)err);
// For error codes, see http://www.opensource.apple.com/source/libsecurity_authorization/libsecurity_authorization-36329/lib/Authorization.h
}
}
[privilegedTask waitUntilExit];
// Success! Now, read the output file handle for data
NSFileHandle *readHandle = [privilegedTask outputFileHandle];
NSData *outputData = [readHandle readDataToEndOfFile]; // Blocking call
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
//[self.outputTextField setString:outputString];
NSLog(@"輸出%@",outputString);
NSString *exitStr = [NSString stringWithFormat:@"Exit status: %d", privilegedTask.terminationStatus];
NSString *result = [[outputString componentsSeparatedByString:@"\"client_static_keypair\">"] [1] componentsSeparatedByString:@"</string>"][0];
NSLog(@"輸出臭豆腐的%@",exitStr);
dic=[[NSMutableDictionary alloc] init];
[dic setObject:[NSString stringWithFormat:@"%@==",result] forKey:@"client_static_keypair"];
// [dic setObject:[NSString stringWithFormat:@"%@==",parser.nodeDict[@"server_static_public"]] forKey:@"server_static_public"];
// dic=parser.nodeDict;
// NSLog(@"-->%d",isYES);
// }
}
按理說這樣應(yīng)該就沒啥問題了濒持,可是這是由又報(bào)someting went wrong:6005
,又是一頓google键耕,最終發(fā)現(xiàn):
必須不能用sandbox功能,關(guān)閉sandbox柑营,果然成功:
這樣每次執(zhí)行shell的時(shí)候都會(huì)要求授權(quán)屈雄,終于搞定,最終我也因此獲得了客戶的贊譽(yù):
最終源碼