gpt4 book ai didi

ios - 检测URL是否不同于我在Objective-C中的网站

转载 作者:行者123 更新时间:2023-12-01 19:55:52 28 4
gpt4 key购买 nike

我有一个Objective-c应用程序,可在加载该应用程序时打开我的网站。

我网站上的大多数链接都指向不同的网站/ URL。

我正在尝试更新我的Objective-C代码,如果该URL与我的网站不同,则在Safari浏览器中打开该URL,而不是在我的APP中打开。

这有可能吗?

这是我的代码

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

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

@end

ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// Load the url into the webview
NSURL *url = [NSURL URLWithString:@"http://mywebsite.com/"];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

您要实现UIWebViewDelegate,尤其是:

- (void)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)type {
if (![request.url.host isEqualToString:@"mywebsite.com"]) {
[UIApplication.sharedApplication openURL:request.absoluteURL];
return NO;
}
return YES;
}

并迅速
func webView(webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType navType: UIWebViewNavigationType) -> Bool {
if request.url?.host != "mywebsite.com" {
UIApplication.shared.openURL(request.absoluteURL)
return false
}
return true
}

关于ios - 检测URL是否不同于我在Objective-C中的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42699048/

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