上一篇我写了在xcode里面添加run script
来执行脚本,可是打包之后发现,只有用xcode
运行的时候才会执行这个shell
命令,查了很多资料,最后我觉得应该是我理解错了,这里的run script
很可能只是为了方便自动化打包之类的场景适用,而并不是真正的运行时执行脚本。那该怎么实现呢?
参阅博客一
参阅博客二
参阅博客三
- (NSString *)cmd:(NSString *)cmd
{
// 初始化并设置shell路径
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];
// -c 用来执行string-commands(命令字符串),也就说不管后面的字符串里是什么都会被当做shellcode来执行
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];
// 获取运行结果
NSData *data = [file readDataToEndOfFile];
return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}
但是有一个问题,因为脚本是:
#!/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需要管理员权限,所以一直提示没有权限执行脚本。
NSString *script = [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];
查了一下资料,可以用with administrator privileges
,这时候又报"用户名密码不正确",不知道为啥,最终抛弃了NSTask
,找到了这个库,它自动帮我们处理权限问题,最终:
- (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);
// }
}
按理说这样应该就没啥问题了,可是这是由又报someting went wrong:6005
,又是一顿google,最终发现:
必须不能用sandbox功能,关闭sandbox,果然成功:
这样每次执行shell的时候都会要求授权,终于搞定,最终我也因此获得了客户的赞誉:
最终源码