Tag: Cocoa/WebViewの組み込み
override func viewDidLoad() { super.viewDidLoad() let url = NSURL(string:"http://www.softantenna.com/")! let request = NSURLRequest(URL: url) myWebView.mainFrame.loadRequest(request) myWebView.policyDelegate = self
class MyWindowController: NSWindowController { override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. } var viewController: ViewController { get { return self.window!.contentViewController! as! ViewController } } @IBAction func backClicked(sender: NSToolbarItem) { print("abc"); viewController.goBack() } }
func goBack() { myWebView.goBack() }
class ViewController: NSViewController,WebPolicyDelegate { @IBOutlet weak var myWebView: WebView! override func viewDidLoad() { super.viewDidLoad() let url = NSURL(string:"http://www.softantenna.com/")! let request = NSURLRequest(URL: url) myWebView.mainFrame.loadRequest(request) myWebView.policyDelegate = self } func openLinkWithSafari(actionInformation: [NSObject : AnyObject], request: NSURLRequest, decisionListener listener: WebPolicyDecisionListener) { let url: String = request.URL!.absoluteString if !url.hasPrefix("http://www.softantenna.com/") && NSWorkspace.sharedWorkspace().openURL((actionInformation[WebActionOriginalURLKey] as! NSURL)) { listener.ignore() return } } func webView(webView: WebView, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject], request: NSURLRequest, frame: WebFrame, decisionListener listener: WebPolicyDecisionListener) { if WebNavigationType.LinkClicked.rawValue == Int((actionInformation[WebActionNavigationTypeKey] as! NSNumber)) { self.openLinkWithSafari(actionInformation, request: request, decisionListener: listener) } listener.use() } func webView(webView: WebView, decidePolicyForNewWindowAction actionInformation: [NSObject : AnyObject], request: NSURLRequest, newFrameName frameName: String, decisionListener listener: WebPolicyDecisionListener) { if WebNavigationType.LinkClicked.rawValue == Int((actionInformation[WebActionNavigationTypeKey] as! NSNumber)) { self.openLinkWithSafari(actionInformation, request: request, decisionListener: listener) } listener.use() } }
objective c - How to load URL on launch in a WebView (OSX project)? - Stack Overflowに解説がある。
#import <Cocoa/Cocoa.h> #import <WebKit/WebKit.h> @interface AppDelegate : NSObject <NSApplicationDelegate> @property (weak) IBOutlet WebView *myWebView; @end
#import "AppDelegate.h" @interface AppDelegate () @property (weak) IBOutlet NSWindow *window; @end @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application [[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.softantenna.com/"]]]; } - (void)applicationWillTerminate:(NSNotification *)aNotification { // Insert code here to tear down your application } @end