Open the Safari app with URL in background without

2019-04-16 13:33发布

问题:

I want to open a URL from my current Cocoa app in Safari, but the thing is that I don't want the Safari UI to be visible to user. i.e. Safari should be executed in background without showing what is happening in it. Is it possible ?

If yes please tell the method to do it.

回答1:

You may open the URL in a web view by using WebView class and don't make the WebView visible to user if safari is not a compulsory requirement.

OR

You may use Scripting Bridge framework to minimize the application safari or use other methods in header file Safari.h to run it in background after opening the URL. You may go through this link to know more about Scripting Bridge. For your task follow the steps below:

Generate a header file for safari, using the terminal command below:

   sdef /Applications/Safari.app | sdp -fh --basename Safari

Now, check your home folder or the current directory from which terminal cmd is executed to see a file Safari.h Drag this file and add it to your project. Add ScriptingBridge.Framework to your project.

Import headers:

#import <ScriptingBridge/ScriptingBridge.h>

#import "Safari.h"

Now use the below code to hide safari after opening the URL:

SafariApplication* sfApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"];

SafariWindow* win =  [[sfApp windows]objectAtIndex:0];
[win setVisible:FALSE];