- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-07-11T16:04:13+09:00","default:wikiwriter","wikiwriter")
&tag(Cocoa/WebViewの組み込み);
*目次 [#g8cf9e5e]
#contents
*関連ページ [#n7005997]
*参考情報 [#l5899f0b]
*WebViewを設置した単純なネイティブアプリを作成する [#vbcce27a]
**WebViewを設置した単純なネイティブアプリを作成する [#vbcce27a]
**Swift + Storyboardの場合 [#e6933ce6]
***URLを表示する [#oe555c82]
-Storyboardを開きView ControllerにWebViewを設置
-WebViewをViewController.swiftにControlドラッグし、アウトレットを作成する。
-ViewControllerのviewDidLoad()で以下のようにすればURLを表示できる。
#pre{{
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
}}
***ツールバーを設置する [#ie733860]
-StoryboardでWindow Controllerを選択し、その中にあるWindowの内部にToolBarを設置。
-デフォルトのボタンを削除し、Image Toolbar Itemを追加する。
-戻る・進む・リロードなどはAttributes InspectorのImage Nameを指定すればデフォルトの画像が使用できる。
***ツールバーのボタンを押したときの処理 [#t5ee4e1c]
-例えばツールバーに戻る、進むボタンを設置する場合。
-Window Controllerのクラスとして以下を指定する(新規=>Newで作成したクラス)。
#pre{{
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()
}
}
}}
-ツールバーのボタンを右クリックし、sent > Actionをドラッグして、Window Controllerにドロップ上記のbackClickedと接続する。
-ちなみに戻る処理はViewController.swift内部の以下の処理。
#pre{{
func goBack() {
myWebView.goBack()
}
}}
***リンククリック時に外部ブラウザで開く [#t0581484]
-ViewControllerにWebPolicyDelegateを実装。policyDelegateに自身を設定しイベントハンドラを生成する。
-decidePolicyForNavigationActionは通常のリンクをクリックしたときの動作。
-decidePolicyForNewWindowActionはtarget="_blank"のリンクをクリックしたときなど外部ウィンドウを開くときの動作。
#pre{{
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 + xibの場合 [#e25dd591]
[[objective c - How to load URL on launch in a WebView (OSX project)? - Stack Overflow:http://stackoverflow.com/questions/15921974/how-to-load-url-on-launch-in-a-webview-osx-project]]に解説がある。
-xibのWindow内部のviewにWebViewをドロップ。
-xibとAppDelegateのソースコードを同時に表示して(アシスタントエディタを使う)、WebViewをControl-ドラッグする。するとAppDelegateにWebViewのアウトレットが自動で作成される。
-ヘッダーは以下のようになる。
#pre{{
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (weak) IBOutlet WebView *myWebView;
@end
}}
-ソースは以下のようになる。
#pre{{
#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
}}
-プロジェクト設定でWebKit.frameworkを追加する。
-またATSのせいでHTTP接続ができない場合、Info.plistを修正する。「App Transport Security Settings > Allow Arbitary Loads」をYESにする。