2

私のアプリでは、Web ブラウザーで複数の URL を開きたいと考えています。

私はこれを次のようにします:

int options = NSWorkspaceLaunchWithoutActivation | NSWorkspaceLaunchWithErrorPresentation;

[[NSWorkspace sharedWorkspace] openURLs: urls
                withAppBundleIdentifier: @"com.apple.safari"
                                options: options
         additionalEventParamDescriptor: nil
                      launchIdentifiers: nil];

Safari は一度に 6 つの URL しか開くことができず、 を使用しているときにNSWorkspaceLaunchWithErrorPresentation次のエラー メッセージが表示されます。

アプリケーション「Safari」が応答しないため、開くことができません。

エラーメッセージ

バンドル識別子を設定するcom.google.Chromeとさらに悪化し、4 つのタブしか開かれません。Firefox ( org.mozilla.firefox) も 6 つのタブを開きます。

4

1 に答える 1

2

説明した制限を回避する簡単な方法は、待機またはスリープ機能を使用することです。決めた数の URL を開くことができるはずです。

-(void)openURLs {

for (int i = 0; i <= 18; i++) { // open 18 URLS for this example

        NSString *url = @"http://google.com";
        [self openURL:url];

        [NSThread sleepForTimeInterval:0.2f]; // wait .02 second
    }
}

- (void)openURL:(NSString *)url {

int options = NSWorkspaceLaunchWithoutActivation | NSWorkspaceLaunchWithErrorPresentation;

    NSArray *urls = [NSArray arrayWithObject:[NSURL URLWithString:url]];

    [[NSWorkspace sharedWorkspace] openURLs: urls
                    withAppBundleIdentifier: @"com.apple.safari"
                                    options: options
             additionalEventParamDescriptor: nil
                          launchIdentifiers: nil];
}

注: URL をロードする方法 (バックグラウンドなど) に応じて、ディスパッチ キューを使用して別のスレッドを使用してそれらをロードできます。

于 2015-03-25T16:52:25.293 に答える