2011-06-05 63 views
0

我有一个webview显示在滚动视图和翻转视图,并需要找到一种方式来打开链接,当在safari(不在应用程序中)点击Safari浏览器!如何在Safari中的UIWebview中打开链接

我会appriciate任何帮助,因为我坚持了几个小时在这!

ArticleScrollVC.h

// ArticleScrollVC.h 




#import <UIKit/UIKit.h> 
#import "PagedScrollview.h" 
#import "OMPageControl.h" 

#define VIEW_FRONT_TAG 98765 
#define VIEW_DESCR_TAG 98764 

@interface ArticleScrollVC : UIViewController <PagedScrollDelegate>{ 

    NSArray *articles; 
    NSMutableArray *pages; 
    IBOutlet PagedScrollView *scrollView; 
    IBOutlet OMPageControl *pageControl; 
    IBOutlet UIImageView *pageControlBG; 
    IBOutlet UIImageView *imageBG; 
    int currentPage; 
} 

@property (nonatomic,retain) NSArray *articles; 
@property int currentPage; 

- (void)resize; 
- (void)changeViewMode; 

+ (BOOL) showDescription; 

- (IBAction) pageControlDidChange; 

@end 

// ArticleScrollVC.m

// ArticleScrollVC.m 


#import "ArticleScrollVC.h" 
#import "AsyncImageView.h" 
#import "TabListViewController.h" 
#import "ArticlePageVC.h" 


#define PREFIX_TAG 666666 

@implementation ArticleScrollVC 

static BOOL showDescription = NO; 

@synthesize articles; 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [scrollView setScrollDelegate:self]; 
    [scrollView setCurrentPage:currentPage]; 
    pageControl.numberOfPages = 0; 
    [pageControl setImageNormal:[UIImage imageNamed:@"pageControlNormal.png"]]; 
    [pageControl setImageCurrent:[UIImage imageNamed:@"pageControlCurrent.png"]]; 
} 

- (int) numberOfPages{ 
    pageControl.numberOfPages = [articles count]; 
    if (pageControl.currentPage==0) { 
     [pageControl setCurrentPage:1]; 
     [pageControl setCurrentPage:0]; 
    } 
    //pages = [[NSMutableArray alloc] initWithCapacity:10]; 
    return [articles count]; 
} 



- (UIView*) viewForPage:(int)page{ 
    UIDevice *dev = [UIDevice currentDevice]; 
    NSString *deviceModel = dev.model; 
    ArticlePageVC *pageVC; 

    if([deviceModel isEqual:@"iPad"] || [deviceModel isEqual:@"iPad Simulator"]){ 
     pageVC = [[[ArticlePageVC alloc] initWithNibName:@"ArticlePageIpad" bundle:nil] autorelease]; 
    }else { 
     pageVC = [[[ArticlePageVC alloc] initWithNibName:@"ArticlePageVC" bundle:nil] autorelease]; 
    } 

    pageVC.article = (ListItem *)[articles objectAtIndex:page]; 
    pageVC.view.frame = scrollView.frame; 
    CGRect frame = scrollView.frame; 
    [pageVC resize:frame]; 
    pageVC.view.tag = PREFIX_TAG+page; 
    return pageVC.view; 
} 

- (IBAction) pageControlDidChange{ 
    [scrollView setCurrentPage:[pageControl currentPage]]; 
} 

- (void) didChoosePage:(int)page{ 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    UIView *pageVC = [self.view viewWithTag:PREFIX_TAG+page]; 
    UIViewAnimationTransition transition = !showDescription?UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight; 
    [UIView setAnimationTransition:transition 
          forView:pageVC 
          cache:YES]; 
    [self changeViewMode]; 
    [UIView commitAnimations]; 
} 

- (void) changeViewMode{ 
    showDescription = !showDescription; 
    for (int page=0; page<[articles count]; page++) { 
     UIView *pageVC = [self.view viewWithTag:PREFIX_TAG+page]; 
     [[pageVC viewWithTag:VIEW_DESCR_TAG] setHidden:!showDescription]; 
     [[pageVC viewWithTag:VIEW_FRONT_TAG] setHidden:showDescription]; 
    } 
    [scrollView setScrollEnabled:!showDescription]; 
    [pageControl setHidden:showDescription]; 
} 
- (void) scrollViewChangedPage:(NSNumber*)page{ 
    pageControl.currentPage = [page intValue]; 
} 

- (void) resize{ 
    scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
    pageControl.frame = CGRectMake(20, self.view.frame.size.height-36, self.view.frame.size.width-40, 36); 
    pageControlBG.frame = pageControl.frame; 
    [imageBG setFrame:scrollView.frame]; 

} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
} 

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

+ (BOOL) showDescription{ 
    return showDescription; 
} 

- (void)dealloc { 
    [pages release]; 
    [super dealloc]; 
} 


@end 

// ArticlePageVC.h

// ArticlePageVC.h 


#import <UIKit/UIKit.h> 
#import "TabListViewController.h" 
#import "AsyncImageView.h" 

#import "TabHTMLViewController.h" 

@interface ArticlePageVC : UIViewController { 

    IBOutlet AsyncImageView *asyncImage; 
    IBOutlet UILabel *label; 
    IBOutlet UIImageView *labelBG; 

    IBOutlet UIWebView *body; 
    IBOutlet UILabel *detailLabel; 
    IBOutlet UIImageView *detailLabelBG; 

    IBOutlet UIImageView *pageControlBG; 

    ListItem *article; 
} 

@property (nonatomic,retain) ListItem *article; 
- (void)resize; 


@end 

// ArticlePageVC.m

// ArticlePageVC.m 


#import "ArticlePageVC.h" 
#import "ArticleScrollVC.h" 

@implementation ArticlePageVC 

@synthesize article; 

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
/* 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization. 
    } 
    return self; 
} 
*/ 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [asyncImage changeContentMode:UIViewContentModeScaleAspectFit]; 
    [asyncImage loadImageFromURL:[NSURL URLWithString:article.thumbnailURL]]; 
    label.text = article.title; 
    detailLabel.text = article.title; 
    [body loadHTMLString:[article content] baseURL:nil]; 
    [pageControlBG setImage:[UIImage imageNamed:@"page_control_bg.png"]]; 

    body.scalesPageToFit = YES; 

} 

- (void) resize:(CGRect)frame{ 

    UIDevice *dev = [UIDevice currentDevice]; 
    NSString *deviceModel = dev.model; 
// ArticlePageVC *pageVC; 

    if([deviceModel isEqual:@"iPad"] || [deviceModel isEqual:@"iPad Simulator"]){ 
     self.view.frame = frame; 
     [body setFrame:CGRectMake(20, 130, frame.size.width-40, frame.size.height-130)]; 

     [label setFrame:CGRectMake(40, frame.size.height-340, frame.size.width-100, 320)]; 
     [asyncImage resize:CGRectMake(frame.origin.x+10, 0, asyncImage.frame.size.width-40, frame.size.height-286-2)]; 
     [labelBG setFrame:CGRectMake(20, frame.size.height-320, frame.size.width-40, 284)]; 
     [pageControlBG setFrame:CGRectMake(20, frame.size.height-36, frame.size.width-40, 36)]; 
    }else { 
     self.view.frame = frame; 
     [body setFrame:CGRectMake(20, 120, frame.size.width-40, frame.size.height-120)]; 

     [label setFrame:CGRectMake(40, frame.size.height-156, frame.size.width-100, 120)]; 
     [asyncImage resize:CGRectMake(frame.origin.x+10, frame.origin.y+1, asyncImage.frame.size.width-40, frame.size.height-136-2)]; 
     [labelBG setFrame:CGRectMake(20, frame.size.height-156, frame.size.width-40, 120)]; 
     [pageControlBG setFrame:CGRectMake(20, frame.size.height-36, frame.size.width-40, 36)]; 
    } 



    [[self.view viewWithTag:VIEW_DESCR_TAG] setHidden:![ArticleScrollVC showDescription]]; 
    [[self.view viewWithTag:VIEW_FRONT_TAG] setHidden:[ArticleScrollVC showDescription]]; 
} 



- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
} 

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


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

回答

1

在您的视图控制器中采用UIWebViewDelegate协议,然后实现-webView:shouldStartLoadWithRequest:navigationType:这样它会向应用程序发送一个-openURL:消息并返回NO,以防止Web视图打开它。

+0

嗨,thx但仍然没有好的结果。我已经加入 // ArticlePageVC.h #进口 #进口 “TabListViewController.h” #进口 “AsyncImageView.h” #进口 “TabHTMLViewController.h” @interface ArticlePageVC:的UIViewController { 到我ArticlePageVC.h文件,然后 - (BOOL)web视图:(的UIWebView *)体shouldStartLoadWithRequest:(的NSURLRequest *)请求navigationType:(UIWebViewNavigationType)navigationType { \t返回NO; \t } 在我的ArticlePageVC.m文件中。但链接中的网址仍然在webview中打开 – Andreas 2011-06-05 16:17:57

+0

您是否将Web视图的代理设置为您的类?您的委托方法是否被调用? – Caleb 2011-06-05 20:27:44

+0

我可能误解了你的问题。您是否想单击Safari中的链接并在您的应用中打开URL,或者单击应用中的链接并在Safari中打开URL?我上面的回答是针对后者的; @vicvicvic告诉你如何做前者。 – Caleb 2011-06-06 08:34:19

0

要处理来自其他应用程序(例如Safari)的开放URL,您需要在应用程序代理中使用register a URL scheme并实现application:openURL:sourceApplication:annotation:。例如,只要方案(andreas)是唯一的,您就可以拥有andreas://the.url.doesnt/matter/because/your/app#parses-it

点击与您计划的链接将导致iOS委托您的应用程序如何解释此URL(使用上述消息)。我的第一个链接有例子。

要定制当用户在应用程序中的一个UIWebView链接点击会发生什么,使用UIWebViewDelegate协议并实施webView:shouldStartLoadWithRequest:navigationType:

您可以返回NO从做“默认”的东西停止Web视图(即加载请求),而是做...任何你想要的,例如加载别的东西。

相关问题