如果url有效,如何检查iphone应用程序然后加载WebVIew else show alert
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果url有效,如何检查iphone应用程序然后加载WebVIew else show alert相关的知识,希望对你有一定的参考价值。
我有应用程序我想要当用户在文本字段中输入url并按回车键时应该检查url是否有效然后在web视图中加载url否则show alert输入有效的url
在我的代码中,它始终显示有效的URL。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField setUserInteractionEnabled:YES];
[textField resignFirstResponder];
test=textField.text;
NSLog(@"Test is working and test is %@",test);
[self urlIsValiad:test];
if ([test isEqualToString:@"Valid"]) {
NSURL *url = [NSURL URLWithString:test];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enter Valid URL" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
return YES;
}
- (BOOL) urlIsValiad: (NSString *) url
{
NSString *regex =
@"((?:http|https)://)?(?:www\.)?[\w\d\-_]+\.\w{2,3}(\.\w{2})?(/(?<=/)(?:[\w\d\-./_]+)?)?";
/// OR use this
///NSString *regex = "(http|ftp|https)://[w-_]+(.[w-_]+)+([w-.,@?^=%&:/~+#]* [w-@?^=%&/~+#])?";
NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([regextest evaluateWithObject: url] == YES) {
NSLog(@"URL is valid!");
test=@"Valid";
} else {
NSLog(@"URL is not valid!");
test=@"Not Valid";
}
return [regextest evaluateWithObject:url];
}
答案
// .h File
-(BOOL)validateUrl;
.m File
-(BOOL)validateUrl{
NSString *urlRegEx = @"((?:http|https)://)?(www\.)[\w\d\-_]+\.\w{2,3}(\.\w{2})?(/(?<=/)(?:[\w\d\-./_]+)?)?";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
if ([urlTest evaluateWithObject: self] == YES) {
NSLog(@"URL is valid!");
} else {
NSLog(@"URL is not valid!");
}
return [urlTest evaluateWithObject:self];
}
// Main View Controller .m File
if (![txtWebsite.text validateUrl])
{
[KWebsiteURLTypeValidation showAsAlert:self];
return;
}
另一答案
您可以使用此方法:
- (BOOL) validateUrl: (NSString *) url {
NSString *theURL =
@"(http|https)://((\w)*|([0-9]*)|([-|_])*)+([\.|/]((\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", theURL];
return [urlTest evaluateWithObject:url];
}
来自Check URL validity。希望这可以帮助。
另一答案
我修改了你的代码,所以请用你的代码替换这个方法,它将超级精细
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField setUserInteractionEnabled:YES];
[textField resignFirstResponder];
test=textField.text;
NSLog(@"Test is working and test is %@",test);
if ([self urlIsValiad:test]) {
NSURL *url = [NSURL URLWithString:test];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enter Valid URL" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
return YES;
}
以上是关于如果url有效,如何检查iphone应用程序然后加载WebVIew else show alert的主要内容,如果未能解决你的问题,请参考以下文章