2011-10-06 58 views
1

我试图在Safari中打开来自UIWebView的链接,但到目前为止还没有成功。我相当肯定我在与代表做错了事。你们可以看看吗?在safari中打开UIWebView链接? 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 

autorelease]]; } [requestURL release]; 返回YES; }

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

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

http://jsfiddle.net/qJ8am/ (我知道它不是JavaScript的,但它看起来更好地在这里比在块引用)

这里是我把我的.m viewDidLoad中的功能(这是一个猜测我不知道该把它放在哪里,或者即使它应该是自己的)

[webView setDelegate:self];

当运行这个项目的代码可能不会在那里,链接都仍然在应用程序中打开,而不是在safari中。你们可以帮我解决我做错了什么,或者给我一些关于如何设置NSLog或者其他东西的指示,这样我就可以看到出了什么问题?感谢您的帮助

回答

1

参考下面的代码:此代码是苹果的示例代码某些部分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 
相关问题