从 UIWebView 打开链接到 Safari (iphone) [英] open link from UIWebView into Safari (iphone)
问题描述
好的,与其尝试用文字解释我的问题,不如观看我录制的这个小视频http://www.youtube.com/watch?v=fwb55jnZI6w
ok rather than try to explain my problem in text, watch this little video i recorded http://www.youtube.com/watch?v=fwb55jnZI6w
这里是详细视图控制器的代码(webview所在的页面)
and here is the code for the detail view controller (the page where the webview is)
detailViewController.h
detailViewController.h
#import <UIKit/UIKit.h>
#import "MWFeedItem.h"
@interface DetailViewController : UIViewController <UIWebViewDelegate> {
MWFeedItem *item;
NSString *summaryString;
IBOutlet UILabel *titleLabel;
IBOutlet UIWebView *contentLabel;
//IBOutlet UILabel *dateLabel;
IBOutlet UIScrollView *textScroller;
}
@property (nonatomic, retain) MWFeedItem *item;
@property (nonatomic, retain) NSString *summaryString;
@property (nonatomic, retain) IBOutlet UILabel *titleLabel;
@property (nonatomic, retain) IBOutlet UIWebView *contentLabel;
@end
detailViewController.m
detailViewController.m
#import "DetailViewController.h"
#import "NSString+XMLEntities.h"
typedef enum { SectionHeader, SectionDetail } Sections;
typedef enum { SectionHeaderTitle, SectionHeaderDate, SectionHeaderURL } HeaderRows;
typedef enum { SectionDetailSummary } DetailRows;
@implementation DetailViewController
@synthesize item, summaryString, titleLabel, contentLabel;
- (BOOL)webView:(UIWebView *)contentLabel shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *requestURL = [ [ request URL ] retain ];
// Check to see what protocol/scheme the requested URL is.
return [ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
return NO;
// Auto release
[ requestURL release ];
// If request url is something other than http or https it will open
// in UIWebView. You could also check for the other following
// protocols: tel, mailto and sms
}
- (void)viewDidLoad {
[super viewDidLoad];
/*if (item.date) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
self.dateString = [formatter stringFromDate:item.date];
[formatter release];
}*/
if (item.summary) {
self.summaryString = [[[item.summary stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingXMLEntities];
}
titleLabel.text = item.title ? item.title : @"[No Title]";
[titleLabel setBackgroundColor:[UIColor clearColor]];
//dateLabel.text = dateString ? dateString : @"[No Date]";
// Summary
NSString *HTMLData = summaryString;
[contentLabel loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://www.feed43.com/1515171705611023.xml"]]];
//Calculate the expected size based on the font and linebreak mode of your label
//CGSize maximumLabelSize = CGSizeMake(280,9999);
//CGSize expectedLabelSize = [summaryString sizeWithFont:contentLabel.font
//constrainedToSize:maximumLabelSize
//lineBreakMode:contentLabel.lineBreakMode];
//adjust the label the the new height.
//CGRect newFrame = contentLabel.frame;
//newFrame.size.height = expectedLabelSize.height;
//contentLabel.frame = newFrame;
[textScroller setCanCancelContentTouches:NO];
[textScroller setContentSize:CGSizeMake(320, 500)];
textScroller.indicatorStyle = UIScrollViewIndicatorStyleBlack;
textScroller.scrollEnabled = YES;
textScroller.clipsToBounds = YES;
//return titleLabel;
//return dateLabel;
//return contentLabel;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return NO;
}
- (void)dealloc {
[super dealloc];
}
@end
注意:即使上面写着contentLabel",它也是一个 UIWebView.它是旧版本遗留下来的
note: even though it says "contentLabel" it is a UIWebView. it is left over from old versions
谢谢!
推荐答案
Implement -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
然后你可以做这样的事情:
Implement -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
Then you can do something like this :
NSURL* url = [request URL];
if (UIWebViewNavigationTypeLinkClicked == navigationType)
{
[[UIApplication sharedApplication] openURL:url];
return NO;
}
这篇关于从 UIWebView 打开链接到 Safari (iphone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!