gpt4 book ai didi

ios - 尝试仅允许某些字符串进入 URL 栏

转载 作者:行者123 更新时间:2023-11-29 03:56:51 31 4
gpt4 key购买 nike

总而言之,我的应用程序中有一个浏览器,我希望它只启动 5 个网站,仅此而已。有没有办法让我的浏览器通过设置自定义字符串来启动 5 个网站,例如 if {用户键入此字符串} 转到该网站,然后 else {不加载}

这是我的 URL 部分的代码

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

// go to google.com
NSURL *urlRequest = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:urlRequest];

// set text bar
urlbar.text = [urlRequest absoluteString];

// load request
[webView loadRequest:request];

// show loading icon
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

抱歉,如果它不是太详细,或者如果我遗漏了一些东西,我会说我是这方面的初学者。

这是我的附加代码

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil: (UINavigationController *)navigationController: (Controller *)transmission
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.controller = navigationController;
self.libtransmission = transmission;
}
return self;
}

- (IBAction)go:(id)sender
{
// create url request
NSURL *urlRequest = [NSURL URLWithString:urlbar.text];
NSURLRequest *request = [NSURLRequest requestWithURL:urlRequest];

// load request
[webView loadRequest:request];

// make network icon visible
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// update urlbar
urlbar.text = webView.request.URL.absoluteString;

// make the loading icon disappear
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

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

最佳答案

将可以访问的站点放入一个数组中,然后遍历该数组,看看输入的字符串是否在该数组中,就像这样

NSArray *sites = @[@"http://test.com", @"http://test1.com", @"http://test2.com", @"http://test3.com", @"http://test4.com"];
NSString *urlToOpen = URLTextField.text;
for (NSString *str in sites){
if ([urlToOpen isEqualToString:str]){
NSURL *urlRequest = [NSURL URLWithString:urlToOpen];
NSURLRequest *request = [NSURLRequest requestWithURL:urlRequest];
urlbar.text = [urlRequest absoluteString];
[webView loadRequest:request];

}
else{
//url user entered is not one of the 5 urls
}
}

关于ios - 尝试仅允许某些字符串进入 URL 栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16390297/

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