我目前正在从事一个学校项目,我的 iPhone 应用程序已被拒绝两次,我知道原因。但解决我的问题很困难。这里我有我的项目中的 .m 文件和一个基于网络的应用程序。现在,如果地址栏中只有 http://,它会显示一条错误,指出找不到该 url 的主机,因为它只是 http://,而我的应用程序因此被拒绝。我想要它做的是本地址栏中只有 http://时,使其转到不同的网站。请帮忙!!另外我使用的是 iOS 6
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
-(IBAction)press {
label.hidden = 0;
addressBar.hidden = 0;
browserPlace.hidden = 1;
button.hidden = 1;
button2.hidden = 0;
}
-(IBAction)press2
{
label.hidden = 1;
addressBar.hidden = 1;
browserPlace.hidden = 0;
button.hidden = 0;
button2.hidden = 1;
}
- (void)viewDidLoad
{
label.hidden = 1;
addressBar.hidden = 1;
browserPlace.hidden = 0;
button.hidden = 0;
button2.hidden = 1;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
addressBar.text = [defaults objectForKey:@"history"];
[self searchBarSearchButtonClicked:addressBar];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[browserPlace loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[self parseUrl:addressBar.text]]]];
}
-(NSString*) parseUrl: (NSString*) url
{
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"])
return url;
else
return [NSString stringWithFormat:@"http://%@", url];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:addressBar.text forKey:@"history"];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
label2.hidden = 0;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
label2.hidden = 1;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"%@", [error description]);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
label2.hidden = 1;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[error description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:YES];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最佳答案
更新您的 parseUrl:
方法。检查 URL 是否只是“http://”:
- (NSString *)parseUrl:(NSString *)url {
if (url.length == 0 || [url isEqualToString:@"http://"]) {
return @"http://www.someplacecool.com"; // put your desired URL here
} else if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"]) {
return url;
} else {
return [NSString stringWithFormat:@"http://%@", url];
}
}
您还应该添加其他错误检查,以防用户输入任何其他无效 URL。
关于ios - 本地址栏中只有前缀时,使 iphone webview 转到不同的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14992415/