1
votes

I'm trying to open links from UIWebView in safari, but have been unsuccessful so far. I am fairly certain I'm doing something wrong with the delegates. Can you guys take a look?

Here is what I have in my viewcontroller.m

(BOOL)webView:(UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType; {

NSURL *requestURL =[[request URL]retain];
if(([[requestURL scheme]isEqualToString:@"http"])&&(navigationType == 
UIWebViewNavigationTypeLinkClicked)){
return ![[UIApplication sharedApplication]openURL:[requestURL

autorelease]]; } [requestURL release]; return YES; }

sorry about the formatting. Anyway my first question is should the webView above be the same as the webView that I declared in my .h file?

My next question is about delegating the webview. This is my viewcontroller.h

http://jsfiddle.net/qJ8am/ (I know its not javascript, but it looks better here than in a blockquote)

and here is what I put in my .m viewdidload function (this was a guess i didn't know where to put it, or even if it should be self)

[webView setDelegate:self];

When running this project the code might as well not even be there, the links all still open in the app, not in safari. Can you guys help me with what I am doing wrong or give me some pointers on how to set up NSLog or something so I can see what is going wrong? Thanks for your help

1

1 Answers

1
votes

refer below code: this code is some part of Apple Sample code http://developer.apple.com/library/ios/#samplecode/UICatalog/Introduction/Intro.html

#import <UIKit/UIKit.h>

@interface WebViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate>
{
    UIWebView *myWebView;
}

@property (nonatomic, retain) UIWebView *myWebView;

@end

#import "WebViewController.h"
#import "Constants.h"

@implementation WebViewController

@synthesize myWebView;

- (void)dealloc
{
    myWebView.delegate = nil;
    [myWebView release];

    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"WebTitle", @"");

    CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
    webFrame.origin.y += kTopMargin + 5.0;  // leave from the URL input field and its label
    webFrame.size.height -= 40.0;
    self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
    self.myWebView.backgroundColor = [UIColor whiteColor];
    self.myWebView.scalesPageToFit = YES;
    self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    self.myWebView.delegate = self;
    [self.view addSubview:self.myWebView];

    CGRect textFieldFrame = CGRectMake(kLeftMargin, kTweenMargin,
                                       self.view.bounds.size.width - (kLeftMargin * 2.0), kTextFieldHeight);
    UITextField *urlField = [[UITextField alloc] initWithFrame:textFieldFrame];
    urlField.borderStyle = UITextBorderStyleBezel;
    urlField.textColor = [UIColor blackColor];
    urlField.delegate = self;
    urlField.placeholder = @"<enter a URL>";
    urlField.text = @"http://www.apple.com";
    urlField.backgroundColor = [UIColor whiteColor];
    urlField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    urlField.returnKeyType = UIReturnKeyGo;
    urlField.keyboardType = UIKeyboardTypeURL;  // this makes the keyboard more friendly for typing URLs
    urlField.autocapitalizationType = UITextAutocapitalizationTypeNone; // don't capitalize
    urlField.autocorrectionType = UITextAutocorrectionTypeNo;   // we don't like autocompletion while typing
    urlField.clearButtonMode = UITextFieldViewModeAlways;
    [urlField setAccessibilityLabel:NSLocalizedString(@"URLTextField", @"")];
    [self.view addSubview:urlField];
    [urlField release];

    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]]];
}

// called after the view controller's view is released and set to nil.
// For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.
// So release any properties that are loaded in viewDidLoad or can be recreated lazily.
//
- (void)viewDidUnload
{
    [super viewDidUnload];

    // release and set to nil
    self.myWebView = nil;
}


#pragma mark -
#pragma mark UIViewController delegate methods

- (void)viewWillAppear:(BOOL)animated
{
    self.myWebView.delegate = self; // setup the delegate as the web view is shown
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.myWebView stopLoading];   // in case the web view is still loading its content
    self.myWebView.delegate = nil;  // disconnect the delegate as the webview is hidden
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // we support rotation in this view controller
    return YES;
}

// this helps dismiss the keyboard when the "Done" button is clicked
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[textField text]]]];

    return YES;
}


#pragma mark -
#pragma mark UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    // starting the load, show the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // finished loading, hide the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    // load error, hide the activity indicator in the status bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    // report the error inside the webview
    NSString* errorString = [NSString stringWithFormat:
                             @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font></center></html>",
                             error.localizedDescription];
    [self.myWebView loadHTMLString:errorString baseURL:nil];
}

@end