我对网络视图有问题。
我为Osx制作了一个简单的网络浏览器,在里面我必须隐藏nvigation栏,菜单和右键单击,用户只能进入一个特定的url。
所有这些都没关系,但我需要允许_blank目标..我的意思是..我有一些链接的目标_blank,所以在新窗口中打开,但它不起作用,我不知道允许这样做。
这是我的DataOAppDelegate.h代码:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface DataOAppDelegate : NSObject <NSApplicationDelegate,NSWindowDelegate>
{WebView *WebView;
//other instance variable
}
@property (assign) IBOutlet NSWindow *window;
@property (retain, nonatomic) IBOutlet WebView *myWebView;
@end
和 DataOAppDelegate.m 的代码
#import "DataOAppDelegate.h"
//#import <WebKit/WebKit.h>
@implementation DataOAppDelegate
@synthesize window;
@synthesize myWebView;
//your function etc
-(void)awakeFromNib{
NSString *urlText = @"http://website.com";
[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
[myWebView setDrawsBackground:NO];
[window setDelegate:self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[myWebView setUIDelegate:self];
NSString *urlText = @"http://website.com";
[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
}
- (WebView *)myWebView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
NSLog(@"sss%@",sender);
NSUInteger windowStyleMask = NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask |
NSTitledWindowMask;
NSWindow* webWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask:windowStyleMask backing:NSBackingStoreBuffered defer:NO];
WebView* newWebView = [[WebView alloc] initWithFrame:[webWindow contentRectForFrameRect:webWindow.frame]];
[newWebView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[webWindow setContentView:newWebView];
[webWindow center];
[webWindow makeKeyAndOrderFront:self];
[[newWebView mainFrame] loadRequest:request];
return newWebView;
}
- (void)launchSoftWithBundleID:(NSString *)softPath
{
NSBundle *softBundle = [NSBundle bundleWithPath:softPath];
NSString *bundleID = [softBundle bundleIdentifier];
//
NSTask *softTask = [[NSTask alloc] init];
[softTask setLaunchPath:softPath];
[softTask launch];
//
NSArray *array = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleID];
if ([array count] > 0)
{
NSRunningApplication *runningApp = [array objectAtIndex:0];
[runningApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}
}
WebViews具有委托方法:decidePolicyForNavigationAction
和decidePolicyForNewWindowAction
(文档)。
- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id)listener {
if ([sender isEqual:self.YourVebView]) {
[listener use];
}
else {
[[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]];
[listener ignore];
}
}
- (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id<WebPolicyDecisionListener>)listener {
[[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]];
[listener ignore];
}
注意:不要忘记为 Web 视图设置策略委托。