0

总而言之,我的应用程序中有一个浏览器,我希望它只启动 5 个网站,别无其他。有没有一种方法可以让我的浏览器通过设置自定义字符串来启动 5 个网站,比如 if {user types this string in} go to that then else {do not load}

这是我的 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.
}
4

1 回答 1

1

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

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
    }
}
于 2013-05-06T00:07:47.453 回答