現(xiàn)在QQ不是可以同時(shí)登錄多個(gè)賬號(hào)嗎?怎么才能實(shí)現(xiàn)QQ這種效果,開啟多個(gè)哪?下面我們就來探討一下。
1、在Main.storyboard中的Main Menu里拖出一條線到AppDelegate.m里創(chuàng)建一個(gè)方法
2、然后實(shí)現(xiàn)點(diǎn)擊方法:
- (IBAction)newApplication:(id)sender {
//獲得本程序的.exec文件
NSString *executablePath = [[NSBundle mainBundle] executablePath];
//創(chuàng)建任務(wù)
NSTask *task = [[NSTask alloc] init];
//啟動(dòng)路徑
task.launchPath = executablePath;
//啟動(dòng)
[task launch];
}
3、這個(gè)NSMenuItem的快捷鍵是command + N ,所以我們按下這個(gè)快捷鍵就能看到此App又開啟了一個(gè)
4、然而正當(dāng)我興高采烈準(zhǔn)備交差的時(shí)候,發(fā)現(xiàn)程序的沙盒權(quán)限打開之后,怎么也實(shí)現(xiàn)不了多開了。
比較了一下打開沙盒之前和之后的task,發(fā)現(xiàn)currentDirectoryPath值不一樣了,可是怎么設(shè)置也沒用。
最后折騰了半天,直接用/usr/bin/open的命令去實(shí)現(xiàn):
5、所以你的程序是打開沙盒權(quán)限的,請(qǐng)使用以下方法實(shí)現(xiàn)多開app的效果:
- (IBAction)newApplication:(id)sender {
//獲得本程序的路徑
NSString *applicationPath = [[NSBundle mainBundle] bundlePath];
//創(chuàng)建任務(wù)
NSTask *task = [[NSTask alloc] init];
//啟動(dòng)路徑
task.launchPath = @"/usr/bin/open";
//添加參數(shù)
task.arguments = @[@"-n", applicationPath];
//啟動(dòng)
[task launch];
}