1

I have a standalone UIViewController with a UIWebView. I need to check if a certain URL has been hit, and if so, present a new UIViewController inside of a UINavigationController.

Here is my code:

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

NSURL *url = request.URL;
NSString *urlString = url.absoluteString;

//Check if special link
if ( [ urlString isEqualToString: @"http://www.mysite.com/store" ] ) {
    //Here present the new view controller
    StoreViewController *controller = [[StoreViewController alloc] init];
    [self presentViewController:controller animated:YES completion: nil];

    return NO;
}

    return YES;

}

However, when I run the app, I get the following error:

WebKit threw an uncaught exception in the method webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate:

<'NSInvalidArgumentException'> * -[__NSArrayM insertObject:atIndex:]: object cannot be nil

If I try and open the UINavigationController itself it works fine...but then how do I pass data to the StoreViewController itself?

Jonathan
  • 2,623
  • 3
  • 23
  • 38
AdamTheRaysFan
  • 175
  • 2
  • 9
  • The answer(s) here may help: http://stackoverflow.com/questions/7883899/ios-5-uiwebview-delegate-webkit-discarded-an-uncaught-exception-in-the-webview – Jonathan May 05 '14 at 01:28

2 Answers2

0

Let's try these solutions:

  1. Destroy webview's delegate in your view controller:

    -(void)dealloc{ self.yourWebView.delegate = nil}
    
  2. Stop request from webView:

    -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    
    StoreViewController *controller = [[StoreViewController alloc] init];
    [self presentViewController:controller animated:YES completion: nil];
    //stop loading here
    [webview stopLoading];
    return NO;
    }
    
  3. Double check in new view controller, do you have any array which it's element is assigned to nil

Hope this helps you

nmh
  • 2,497
  • 1
  • 15
  • 27
0

Lets go through the problems one by one:

  • WebKit Exception: You can resolve this by stopping the webview loading. Just call [webView stopLoading];
  • Pass data: If you declare your required data in the StoreViewController.h you can easily set them when you are initializing the view controller.

    StoreViewController *controller = [[StoreViewController alloc] init]; 
    controller.myStringData = @"Required Data"; 
    controller.myDataDictionary = someDictionary;
    

    You will be able to get the data in the viewDidLoad method of StoreViewController

Hope this helps.

Max
  • 3,371
  • 2
  • 29
  • 30