0
votes

I want to open links of uiwebview in to the safari browser my code is working perfectly if I implement shouldStartLoadWithRequest method in viewController but when I implement shouldStartLoadWithRequest in same class and set UIWebView's delegate to self it doesn't work it get halt in between and shows assembly level code with error EXC_BAD_ACCESS(code=2, address=0x9) my files are as follows

//content of ShowView.h file

#import <UIKit/UIKit.h>
@interface ShowView : UIView <UIWebViewDelegate> {

}

- (void) showViewFunction;

@property (nonatomic, assign) UIViewController *mainViewContObj;

@end

//content of ShowView.m file is :

#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

// Content of 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

Some times html page shows but when I click on link it opens in same UIWebview window, and not even going into the shouldStartLoadWithRequest method, am I doing anything wrong?

1

1 Answers

0
votes

Your code isn't clear, might need more info's, but from what i see, the ShowView class is never instanciated, so it shouldn't even show.

you should make something like this i guess :

//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];

    }

now for the ShowView class, try something like this :

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

This should work, i didn't try it, i'll comeback tomorrow to try it if necessary.