gpt4 book ai didi

IOS UIWebView : opening links in safari

转载 作者:可可西里 更新时间:2023-11-01 06:19:46 24 4
gpt4 key购买 nike

我创建了自定义类,文件是 showBlock.h 和 showBlock.m,用于以编程方式加载 UIWebView,showBlock.m 的实现是

#import "showBlock.h"

@implementation showBlock;

@synthesize mainViewContObj;

- (void) showView {
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[aWebView setDelegate:[self mainViewContObj]];
NSString *urlAddress = @"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[aWebView loadRequest:requestObj];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[[[self mainViewContObj] view] addSubview:aWebView];

}
@end

它工作正常,正在加载包含 html 内容的 index.php 文件,但我想在 safari 浏览器中打开此 html 文件的链接,我需要为此做些什么?

最佳答案

需要在ShowBlock.m中添加下面的delegate方法实现

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
// This practically disables web navigation from the webView.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return FALSE;
}
return TRUE;
}

关于IOS UIWebView : opening links in safari,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com