0

我目前正在做一个学校项目,我的 iphone 应用程序被拒绝了两次,我知道为什么。我的问题的解决方案虽然很困难。在这里,我有来自我的项目的 .m 文件,并且我有一个基于 Web 的应用程序。现在,如果地址栏中只有 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
4

2 回答 2

1

更新你的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,您还应该添加其他错误检查。

于 2013-02-21T00:58:12.910 回答
0

我认为你可以利用

  • (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

您可以检查 [请求 URL] 及其方案。

尝试在此方法中将 URL 重定向到不同的 URL,并返回相应的 BOOL 值 (YES/NO)。

于 2013-02-21T01:15:53.237 回答