我想在 safari 浏览器中打开 uiwebview 的链接,如果我在 viewController 中实现 shouldStartLoadWithRequest 方法,我的代码可以正常工作,但是当我在同一个类中实现 shouldStartLoadWithRequest 并将 UIWebView 的委托设置为 self 时,它不起作用它会在两者之间停止并显示带有错误EXC_BAD_ACCESS(code=2, address=0x9)的汇编级代码 我的文件如下
//ShowView.h 文件内容
#import <UIKit/UIKit.h>
@interface ShowView : UIView <UIWebViewDelegate> {
}
- (void) showViewFunction;
@property (nonatomic, assign) UIViewController *mainViewContObj;
@end
//ShowView.m 文件的内容是:
#import "ShowView.h"
@implementation ShowView
- (void) showViewFunction {
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[aWebView setDelegate:self];
NSString *urlAddress = @"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
aWebView.delegate = self;
[aWebView loadRequest:requestObj];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[[[self mainViewContObj] view] addSubview:aWebView];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"In shouldStartLoadWithRequest method");
if ([[[request URL] absoluteString] isEqual:@"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
@end
// ViewController.h 的内容
#import "ViewController.h"
#import "ShowView.h"
@interface mnetViewController ()
@end
@implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
有时会显示 html 页面,但是当我单击链接时,它会在同一个 UIWebview 窗口中打开,甚至没有进入 shouldStartLoadWithRequest 方法,我做错了什么吗?