2011-12-02 69 views
0

我遵循了許多教程,並且我的Web視圖中的鏈接仍未在Safari中打開。 我是一個noob,這是我的第一個iPhone應用程序,所以我希望我只是缺少一些簡單的東西。Iphone WebView鏈接無法在Safari中打開

// 
// ViewController.m 
// FirstIphoneApp 
// 
// Created by admin on 12/2/11. 
// Copyright (c) 2011 __MyCompanyName__. All rights reserved. 
// 

#import "ViewController.h" 

@implementation ViewController 
@synthesize webView; 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)navigationType 
{ 
    if (navigationType == UIWebViewNavigationTypeLinkClicked) 
    { 
     [[UIApplication sharedApplication] openURL:[inRequest URL]]; 
     return NO; 
    } 
    return YES; 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://somewebsite.com/iphone.html"]]]; 


    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } else { 
     return YES; 
    } 
} 

@end 

這裏是我的頭文件

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UIWebViewDelegate,UIAlertViewDelegate>{ 
    IBOutlet UIWebView *webView; 
} 


@property(nonatomic, retain) IBOutlet UIWebView *webView; 

@end 

回答

0

是否shouldStartLoadWithRequest方法火?這可能是因爲你沒有連接你的nib文件中的委託。通常,這將連接到您的視圖控制器,或任何文件shouldStartLoadWithRequest方法中找到。

+0

啊,是我設置一個斷點,它似乎並沒有被擊中。我沒有看到一個nib文件,是XCode 4.2.1中的「storyboard」文件嗎?並且你能提供關於連接故事板中的委託的任何指示嗎? – TreK

+0

我能找到代表。我選擇了View而不是View Controller作爲Delegate。有用!謝謝 – TreK