我想打开uiwebview到safari浏览器的链接,如果我在viewController中实现了shouldStartLoadWithRequest方法,但是当我在同一个类中实现shouldStartLoadWithRequest并将UIWebView的委托设置为self时,我的代码正常工作,但它无法正常运行介于和显示汇编级代码,错误 EXC_BAD_ACCESS(代码= 2,地址= 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方法,我做错了什么?
答案 0 :(得分:0)
您的代码不清楚,可能需要更多信息,但从我看到的情况来看,ShowView类从未实例化,因此它甚至不应该显示。
你应该做这样的事情我猜:
// mnetViewController.m
#import "mnetViewController.h"
#import "ShowView.h"
@interface mnetViewController ()
@end
@implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
ShowView* theShowView = [[ShowView alloc] initWithFrame:CGRectMake(insert the frame you want your webview to have)];
theShowView.autoresizesSubviews = YES;
[self.view addSubview:theShowView];
[theShowView release];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
现在对于ShowView类,尝试这样的事情:
// ShowView.h
#import <UIKit/UIKit.h>
@interface ShowView : UIView <UIWebViewDelegate> {
}
@end
// ShowView.m
#import "ShowView.h"
@implementation ShowView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
aWebView.scalesPageToFit = YES;
[aWebView setDelegate:self];
[self addSubview:aWebView];
NSString *urlAddress = @"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
[aWebView release];
}
return self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"In shouldStartLoadWithRequest method for URL : %@",[request [URL absolutString]]);
if ([[[request URL] absoluteString] isEqual:@"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
这应该有用,我没有尝试过,明天我会回来尝试一下。