Range or index out of bounds (App is crashing)

2.6k views Asked by At

I have an app with ads banner (inner-active) in every view controller (4 views) and i keep getting this error after 2-3-4 minutes that my app is running:

'NSRangeException', reason: '*** -[__NSCFString substringToIndex:]: Range or index out of bounds'

My app is crashing & i can't find the solution for that, but i know for sure that is something with my AD banner issue cau's when i comment the ad banner code everything works great. i can assume that it happens when new request is loading on the banner.
This is the code i'm using for the AD banner:

.h file:

@property (nonatomic, retain) UIView *adBanner;

.m file:
Did synthesize & import for what i need and after that:

- (void)viewWillAppear:(BOOL)animated
{    
    CGRect frame = CGRectMake(0, 430, 320, 50);
    self.adBanner = [[UIView alloc] initWithFrame:frame];   
    [self.view addSubview:self.adBanner];

    // Display ad
    if (![InneractiveAd DisplayAd:@"iOS_Test" withType:IaAdType_Banner withRoot:adBanner withReload:60 withParams:optionalParams])
    {
        adBanner.hidden = YES;
    }
}

This is my AppDelegate (don't know why but maybe it something with that too?):

.h file:

#import <UIKit/UIKit.h>

@class RootViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) RootViewController *rootViewController;

@end

.m file:

#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize rootViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:NULL];
    [self.window addSubview:self.rootViewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

I realy don't understand what it could be and how it related to NSCFString :\
Thanks.

1

There are 1 answers

0
Raymond On

Not sure if you got an answer to this but I was getting it with this line of code when there was no internet connection.

NSString *value = [serverOutput substringWithRange:NSMakeRange(2, [serverOutput length] - 2 * 2)];

I had to protect it with a check to the internet connection and an if then statement