简体   繁体   English

如何在 Safari 中打开 UIWebview 中的链接

[英]How to open link in UIWebview in Safari

I have a webview displayed in a scrollview and flip view and need to find a way to open links when clicked in safari (not in the app)!我有一个 webview 显示在滚动视图和翻转视图中,需要找到在 safari(不在应用程序中)中单击时打开链接的方法!

I would appriciate any help as im stuck for hours in this!我会在此停留数小时后获得任何帮助!

ArticleScrollVC.h文章ScrollVC.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

//  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 // 文章PageVC.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 // 文章PageVC.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

Adopt the UIWebViewDelegate protocol in your view controller, and then implement -webView:shouldStartLoadWithRequest:navigationType: such that it sends the application an -openURL: message with the url and returns NO to prevent the web view from opening it. Adopt the UIWebViewDelegate protocol in your view controller, and then implement -webView:shouldStartLoadWithRequest:navigationType: such that it sends the application an -openURL: message with the url and returns NO to prevent the web view from opening it.

To handle opening URLs from other applications (such as Safari), you need to register a URL scheme and implement application:openURL:sourceApplication:annotation: in your application delegate.要处理来自其他应用程序(例如 Safari)的打开 URL,您需要注册一个 URL 方案并在您的应用程序委托中实现application:openURL:sourceApplication:annotation:。 You could, for example, have andreas://the.url.doesnt/matter/because/your/app#parses-it as long as the scheme ( andreas ) is unique.例如,只要方案 ( andreas ) 是唯一的,您就可以拥有andreas://the.url.doesnt/matter/because/your/app#parses-it

Tapping on a link with your scheme will then cause iOS to delegate to your application how to interpret this URL (using the above message).点击与您的方案的链接将导致 iOS 委托给您的应用程序如何解释此 URL(使用上述消息)。 My first link has examples.我的第一个链接有例子。

To customize what happens when users tap on links in a UIWebView in your application, use the UIWebViewDelegate protocol and implement webView:shouldStartLoadWithRequest:navigationType:要自定义当用户在应用程序中点击 UIWebView 中的链接时会发生什么,请使用UIWebViewDelegate协议并实现webView:shouldStartLoadWithRequest:navigationType:

You can return NO to stop the web view from doing "default" stuff (ie load the request), and instead do... whatever you want, for example load something else.您可以返回NO以阻止 web 视图执行“默认”操作(即加载请求),而是执行...任何您想要的操作,例如加载其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM