0

我想在 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 方法,我做错了什么吗?

4

1 回答 1

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;
}

这应该可以,我没试过,如果有必要我明天回来试试。

于 2012-11-20T17:20:18.960 回答