2011-10-06 114 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 
相關問題