NSURLErrorDomain错误-999 IOS7

时间:2014-02-10 14:37:02

标签: ios ios7 uiwebview

我正在尝试在ios应用中打开网页时尝试此问题。在ios7之前它完美无缺,但现在我们正在升级我们的应用程序,这是我们唯一的严重问题。

关于这个问题,我发现它是在两次通话时产生的。我认为这就是这里发生的事情,因为如果我在didFailLoadWithError中写一个NSLog文本,它会显示两次。

我尝试忽略错误但加载圈显示为以太网并且网页无法加载。

我在这里粘贴我的代码,也许你可以帮助我们或给我们一个线索。

            #import "WebPoiViewController.h"
            #import "FavoriteManager.h"

            @interface WebPoiViewController ()

            @end

            @implementation WebPoiViewController
            @synthesize webView,urlStr, activityIndicator,title2,poi=_poi,position=_position;


            - (void)viewDidLoad { //We have a NIB file in play here, so I dropped the loadView here.  Just make sure that your loadView is not getting called twice!
                [super viewDidLoad];

                if (self.poi!=nil){
                    self.position = [[FavoriteManager sharedInstance] isPoiInFavoriteList:self.poi];

                    if (self.position==-1){

                        UIButton *button1 = [[UIButton alloc] init];
                        button1.frame=CGRectMake(0,0,30,30);
                        [button1 setBackgroundImage:[UIImage imageNamed: @"Favorites_icon_inactive.png"] forState:UIControlStateNormal];
                        [button1 addTarget:self action:@selector(makeFavorite:) forControlEvents:UIControlEventTouchUpInside];

                        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];


                        //self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Favorites_icon_inactive.png"]  style: UIBarButtonItemStylePlain target:self action:@selector(makeFavorite:)];
                    } else {

                        UIButton *button1 = [[UIButton alloc] init];
                        button1.frame=CGRectMake(0,0,30,30);
                        [button1 setBackgroundImage:[UIImage imageNamed: @"Favorites_icon.png"] forState:UIControlStateNormal];
                        [button1 addTarget:self action:@selector(deleteFavorite:) forControlEvents:UIControlEventTouchUpInside];

                        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
                        //self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Favorites_icon.png"]  style: UIBarButtonItemStylePlain target:self action:@selector(deleteFavorite:)];
                    }
                }

                if (nil!=title2&& ![title2 isEqualToString:@""]){
                    self.title=self.title2;
                }
                [self loadView];
            }

            - (IBAction)makeFavorite:(id)sender {
                self.position=[[FavoriteManager sharedInstance] addFavotitePOI:self.poi];
                UIButton *button1 = [[UIButton alloc] init];
                button1.frame=CGRectMake(0,0,30,30);
                [button1 setBackgroundImage:[UIImage imageNamed: @"Favorites_icon.png"] forState:UIControlStateNormal];
                [button1 addTarget:self action:@selector(deleteFavorite:) forControlEvents:UIControlEventTouchUpInside];

                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];

            }

            - (IBAction)deleteFavorite:(id)sender {
                [[FavoriteManager sharedInstance] deleteFavotitePOIat:self.position];
                self.position=-1;
                UIButton *button1 = [[UIButton alloc] init];
                button1.frame=CGRectMake(0,0,30,30);
                [button1 setBackgroundImage:[UIImage imageNamed: @"Favorites_icon_inactive.png"] forState:UIControlStateNormal];
                [button1 addTarget:self action:@selector(makeFavorite:) forControlEvents:UIControlEventTouchUpInside];

                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];

            }

            - (void)loadView {
                UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
                self.view = contentView;    

                CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
                webFrame.origin.y = 0.0f;
                webView = [[UIWebView alloc] initWithFrame:webFrame];
                webView.backgroundColor = [UIColor blueColor];
                webView.scalesPageToFit = YES;
                webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
                webView.delegate = self;

                if([webView respondsToSelector:@selector(scrollView)]){
                    webView.scrollView.bounces = NO; 
                }

                [self.view addSubview: webView];
                if (nil!=self.urlStr && ![urlStr isEqualToString:@""]){
                    NSLog(@"NO ES NIL");
                    //[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];

                    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
                    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
                    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
                    //activityIndicator.center = self.view.center;
                    activityIndicator.center = CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2);
                    [self.view addSubview: activityIndicator];
                } else{
                    [webView loadHTMLString:@"<html><center><br /><br /><font size=+5>No info<br /><br /></font></center></html>" baseURL:nil];

                }
            }


            #pragma mark WEBVIEW Methods

            - (void)webViewDidStartLoad:(UIWebView *)myWebView
            {
                // starting the load, show the activity indicator in the status bar
                [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
                [activityIndicator startAnimating];
            }

            - (void)webViewDidFinishLoad:(UIWebView *)myWebView
            {
                // finished loading, hide the activity indicator in the status bar
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                [activityIndicator stopAnimating];
            }

            - (void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error
            {
                if([error code] == NSURLErrorCancelled) return; // Ignore this error
                NSLog(@"FAIL");
                // load error, hide the activity indicator in the status bar
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

                // report the error inside the webview
                NSString* errorString = [NSString stringWithFormat:
                                         @"<html><center><br /><br /><font size=+5 color='red'>Error<br /><br />Your request %@</font></center></html>",
                                         error.localizedDescription];
                [webView loadHTMLString:errorString baseURL:nil];
            }

            - (void)didReceiveMemoryWarning{
                [super didReceiveMemoryWarning];
            }

            - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
            {
                return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
            }

            - (BOOL)shouldAutorotate {
                return [self.navigationController shouldAutorotate];
            }

            - (NSUInteger)supportedInterfaceOrientations {
                return [self.navigationController supportedInterfaceOrientations];
            }


            @end

1 个答案:

答案 0 :(得分:6)

我自己解决了。我想分享答案,因为它可能会帮助其他人解决同样的问题。

我刚刚在didFailWithError中添加了这段代码:

    - (void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error
    {
        if([error code] != NSURLErrorCancelled)  // only goes in if it is not -999
        {
        // load error, hide the activity indicator in the status bar
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        // report the error inside the webview
        NSString* errorString = [NSString stringWithFormat:
                                 @"<html><center><br /><br /><font size=+5 color='red'>Error<br /><br />Your request %@</font></center></html>",
                                 error.localizedDescription];
        [webView loadHTMLString:errorString baseURL:nil];
        }
        else{
             [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] ]];
        }
    }

现在,如果失败,它会尝试重复加载直到加载。可以通过使用计数器来改进。试试5次或10次,而不是像我的那样。