現(xiàn)在QQ不是可以同時登錄多個賬號嗎?怎么才能實現(xiàn)QQ這種效果,開啟多個哪?下面我們就來探討一下。
1、在Main.storyboard中的Main Menu里拖出一條線到AppDelegate.m里創(chuàng)建一個方法

2、然后實現(xiàn)點擊方法:
- (IBAction)newApplication:(id)sender {
//獲得本程序的.exec文件
NSString *executablePath = [[NSBundle mainBundle] executablePath];
//創(chuàng)建任務
NSTask *task = [[NSTask alloc] init];
//啟動路徑
task.launchPath = executablePath;
//啟動
[task launch];
}
3、這個NSMenuItem的快捷鍵是command + N ,所以我們按下這個快捷鍵就能看到此App又開啟了一個

4、然而正當我興高采烈準備交差的時候,發(fā)現(xiàn)程序的沙盒權限打開之后,怎么也實現(xiàn)不了多開了。
比較了一下打開沙盒之前和之后的task,發(fā)現(xiàn)currentDirectoryPath值不一樣了,可是怎么設置也沒用。
最后折騰了半天,直接用/usr/bin/open的命令去實現(xiàn):

5、所以你的程序是打開沙盒權限的,請使用以下方法實現(xiàn)多開app的效果:
- (IBAction)newApplication:(id)sender {
//獲得本程序的路徑
NSString *applicationPath = [[NSBundle mainBundle] bundlePath];
//創(chuàng)建任務
NSTask *task = [[NSTask alloc] init];
//啟動路徑
task.launchPath = @"/usr/bin/open";
//添加參數(shù)
task.arguments = @[@"-n", applicationPath];
//啟動
[task launch];
}