In App Purchase Crashes App - objective-c

I have an iOS App that I just submitted an update for. The development version of the app (simulator and my 4S) never crashed and it works just fine. The app got approved today as well as the IAP, but I realized this morning that the iAP was not "cleared for purchase". I just cleared it for purchase, but the app still crashes. The error the iPhone gives (running the app store version) gives the error:
<Error>: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
Here is the code where that happens:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:
(SKProductsResponse *)response
{
NSArray *myProduct = response.products;
//this line is where the error occurs
noAdProduct = [myProduct objectAtIndex:0];
if (![[[NSUserDefaults standardUserDefaults] objectForKey:#"the id here"] boolValue]) {
adCell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", noAdProduct.price];
adCell.userInteractionEnabled = YES;
adCell.accessoryType = UITableViewCellAccessoryNone;
adCell.accessoryView = nil;
[self.tableView reloadData];
}
}
This happens while requesting a list of available products, which obviously returns the product in the simulator but not in the app. I just added a safeguard against this but I will need to submit it to the App Store again.
Does anyone know why the iAP is not showing up for the App Store version? Do I need to wait for the "cleared for sale" option to go to Apple's servers? Thanks!

Obviously myProduct array is empty. Why this happens is another question, but you should never call objectAtIndex without making sure the requested index exists.
if ([myProduct count] > 0)
noAdProduct = [myProduct objectAtIndex:0];
else
// Deal with the situation when the array is empty.

Related

Crash when setting badge on tab bar in objective C

This line of code crashes. How to check if tabor item is not nil then setting badge?
[self.tabController.tabBar.items objectAtIndex:0].badgeValue = [NSString stringWithFormat:#"%i", num];
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_CTNativeGlyphStorage
tabBar]: unrecognized selector sent to instance 0x15029cfd0'
I checked it by this line of code, but it didn't solve the problem.
if (self.tabController.tabBar.items != nil) { }
If you are setting the badge value before the tab controller is even presented or fully initiated, then there wont be anything at index 0 as yet. There is no need for the tab controller to initiate everything until time comes for presentation, for memory usage purposes. You can keep a reference to each instance of UITabBarItem and set the badge value on them. Or you are going to have to wait until viewDidAppear is called, ensuring all UITabBarItems are fully instantiated...
I cant assume anything else here without seeing the rest of your code.

NSUserDefaults crashes when adding empty array

I just moved to iOS 8, installed Xcode 6 etc.
This code worked nicely on iOS 7 and 6. It even seems to work on an iPhone 4S with iOS 8 installed. But on iPhones 5 and 5S it crashes. (8.0.0 and 8.0.2 alike)
It does not matter whether the app is build against SDK 7 or 8. It crashes in both cases at the same stage.
The .h file contains:
#property (nonatomic, strong) NSMutableArray* filterBrandsExclude;
from the .m file:
// Following lines just to set the conditions for your understanding:
_filterBrandsExclude = [[NSMutableArray alloc] initWithCapacity:0]; // just creates an empty array.
// As a matter of fact, this array may well be empty and it worked nicely on older iOS versions.
// this line crashes:
[userDefaults setObject:_filterBrandsExclude forKey:#"filterBrandsExclude"];
Error Message:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSKeyValueSlowMutableArray getObjects:range:]: value for key filterBrandsExclude of object 0x17004ae00 is nil'
*** First throw call stack:
(0x1885c6084 0x198bac0e4 0x1885c5fc4 0x18946097c 0x189460a68 0x189460ab0 0x1884c24d0 0x18854be90 0x1884c21c0 0x1884c17e8 0x188627c80 0x188552d9c 0x188551e9c 0x188600290 0x1885ff840 0x1886030e4 0x1893bb750 0x100053e5c 0x1000522a4 0x18cfd65d4 0x18cfdead0 0x18cfdea58 0x18cfd238c 0x1907d1640 0x18857e360 0x18857d468 0x18857ba8c 0x1884a9664 0x1915eb5a4 0x18cdae984 0x1000515f4 0x19921aa08)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any idea is appreciated.
you can manually check
if(self.filterBrandsExclude != nil && self.filterBrandsExclude.count > 0)
[userDefaults setObject:_filterBrandsExclude forKey:#"filterBrandsExclude"];
else
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"filterBrandsExclude"];
Two things.
In your error it says that the value for the key filterBrandsInclude is nil, but in your question you reference the key filterBrandsExclude. Are you sure you've pinpointed the actual error at this point?
EDIT: Point #2 is invalid.

Objective-C errors after getting the IAP product response

This code is from the Phonegap Code: IAP Plugin. The error happens on the line of the code right after the "sent js". All the elements sent to the function are non-nil except for the last one 'nil'. I even logged them out to make sure they were sent. This code is right out of the plugin (https://github.com/usmart/InAppPurchaseManager-EXAMPLE) and has not been modified except for the logging. In the debugger i saw that none of the objects were nil, so i don't understand why the error is happening.
Here is the error:
[__NSArrayI JSONRepresentation]: unrecognized selector sent to
instance 0xdc542d0
2013-02-13 23:26:17.209 GoblinSlots[4519:707] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSArrayI JSONRepresentation]: unrecognized selector sent to
instance 0xdc542d0'
here is the code:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse: (SKProductsResponse *)response
{
NSLog(#"got iap product response");
for (SKProduct *product in response.products) {
NSLog(#"sending js for %#", product.productIdentifier);
NSLog(#" title %#", product.localizedTitle );
NSLog(#" desc%# - %#", product.localizedDescription, product.localizedPrice );
NSArray *callbackArgs = [NSArray arrayWithObjects:
NILABLE(product.productIdentifier),
NILABLE(product.localizedTitle),
NILABLE(product.localizedDescription),
NILABLE(product.localizedPrice),
nil ];
NSLog(#"sent js");
NSString *js = [NSString stringWithFormat:#"%#.apply(plugins.inAppPurchaseManager, %#)", successCallback, [callbackArgs JSONSerialize]];
NSLog(#"js: %#", js);
[command writeJavascript: js];
}
All the stuff to do JSON serialization seems to be already included with the Cordova plugins.
There's no need to download and install yet another JSON library.(a)
It appears that PhoneGap is in the process of switching from SBJson to JSONKit.(b)
PhoneGap is also in the process of changing all the JSON methods to use a "cdvjk_" prefix. (c)
As far as I can tell, something didn't quite go right during those changes.
What I did was edit the file Plugins/InAppPurchaseManager.m , where I made these changes:
Add the line
#import <Cordova/CDVJSON.h>
Replace the line
return [self respondsToSelector:#selector(cdvjk_JSONString)] ? [self cdvjk_JSONString] : [self cdvjk_JSONRepresentation];
with
return [self JSONString];
. (What's the right way to push this or a better bugfix back to the nice PhoneGap people?)
JSONRepresentation is a category that SBJson adds so you have to include SBJson.h in the class that uses it.

*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array. When pushing to a navigation controller

I am writing an iPad app. I load a view in with "initWithNibName" and then trying to push the view onto the Navigation controller.
[self.navigationController pushViewController:tvc animated:YES];
This causes the app to crash giving the following stackTrace
2011-09-01 12:46:06.040 PuckDragDemo[4932:f803] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
* First throw call stack:
(0x1191062 0x1322d0a 0x117ddc8 0x2979 0xae95 0x449ebe 0x44a1b1 0x45bccd 0x45bebf 0x45c18b 0x45cc2f 0x45d1d6 0x45cdf4 0x13975 0x1192ed9 0x386eb2 0x386e4a 0x42c3e6 0x42c8af 0x42bb6e 0x3ac2a0 0x3ac4c6 0x392c74 0x386399 0x1d87fa9 0x11651d5 0x10ca042 0x10c892a 0x10c7dd4 0x10c7ceb 0x1d86879 0x1d8693e 0x38438b 0x1c49 0x1bc5)
terminate called throwing an exception(gdb) bt
I have no idea why I am getting this error when I don't have an array. my only guess is that there is an array in the background somewhere which is getting called?
P.S the code requested
if (self.started == YES) return;
if (self.bEditMode == YES) {
[self go];
} else {
GetReadyModalViewController *modalVC = [[GetReadyModalViewController alloc] initWithNibName:#"GetReadyModelViewController" bundle:nil];
modalVC.delegate = self;
[self presentModalViewController:modalVC animated:NO];
modalVC.taskTitle.text = [self.taskPreferences objectForKey:#"title"];
modalVC.taskDescription.text = [self.taskPreferences objectForKey:#"description"];
}
}
It is irrelevant to the pushing on the navigationController, it is a problem inside of the tic viewController. Please post the code from the viewDidLoad/viewWillAppear, as your are instantiating an array and trying to get data from it [myMutableArray objectAtIndex:**0**]
Fixed it. I don't know exactly how it worked. There is a array in my code but not directly connected to the navigation controller. It appears that a index in the array was being called that didn't exist but for some reason it wasn't calling an error. Don't ask me why but when the navigation controller pushed the view it picked up on a previous undetected error.

NSCF Array Out of Bounds?

I have a fairly simple application using Core Data, and a couple Array Controllers (in IB, no code files for them, other than the xcdatamodel file). When I run my application, I get the following error in the log (the app still runs, but no window appears until I go to File > New Document):
2011-02-08 18:45:10.434 MyApp[35178:a0f] *** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)
I have no references in my myDocument.h or .m files to an array. The error is not critical (the app still runs), but it disables some of my code to load the last used document or (if that doesn't exist) load a fresh document. The app simply loads up with no visible windows. I would greatly appreciate some help on this and will award the answer quickly :)
EDIT: Here is the applicationShouldOpenUntitledFile code:
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
NSArray* urls = [[NSDocumentController sharedDocumentController] recentDocumentURLs];
if ([urls count] > 0){
NSURL *lastURL= [urls objectAtIndex: 0];
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:lastURL display:YES error:nil];
return NO;
}
return YES;
}
Sorry to post here, I don't have the right to comment everywhere for the moment.
#Zakman411: Justin is saying you should enable the "Stop on Objective-C Exceptions" item in the Run menu, then launch the debugger. gdb should show you where the exception is thrown exactly.