在safari中打开UIWebView链接? UIWebView委托放置

时间:2011-10-06 02:47:58

标签: iphone xcode delegates uiwebview webview

我正试图在Safari中打开来自UIWebView的链接,但到目前为止还没有成功。我相当肯定我对代表做错了。你们可以看看吗?

以下是我在viewcontroller.m中的内容。

  

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

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

自动释放]];       }       [requestURL发布];       返回YES; }

抱歉格式化。无论如何,我的第一个问题是上面的webView是否应该与我在.h文件中声明的webView相同?

我的下一个问题是委托webview。这是我的viewcontroller.h

http://jsfiddle.net/qJ8am/ (我知道它不是javascript,但它在这里看起来比在blockquote中更好)

这是我放入.m viewdidload函数的内容(这是猜测我不知道放在哪里,或者即使它应该是自己的)

  

[webView setDelegate:self];

在运行此项目时,代码可能甚至不在那里,链接仍然在应用程序中打开,而不是在safari中。你们可以帮我解决我做错了什么,或者给我一些关于如何设置NSLog的指示,以便我可以看到出了什么问题?谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

参考下面的代码:此代码是Apple示例代码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