Game Center not showing in iOS 5 - objective-c

I used this tutorial to access GameCenter.
http://www.raywenderlich.com/3276/how-to-make-a-simple-multiplayer-game-with-game-center-tutorial-part-12
I am able to lo in the game center but then if I want to use this code:
SFAppDelegate * delegate = (SFAppDelegate *) [UIApplication sharedApplication].delegate;
[[GCHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:2 viewController:self delegate:self];
This code should work, but if I call i it only shows black screen with blue toolbar in above.
the "self" in here is UIViewController and this controller implements the GCHelper delegate methods.
the method find ... looks like this:
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController delegate:(id<GCHelperDelegate>)theDelegate {
if (!gameCenterAvailable) return;
matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController; // ?
delegate = theDelegate;
[presentingViewController dismissModalViewControllerAnimated:NO];
if (pendingInvite != nil)
{
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:pendingInvite] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}
else
{
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
request.playersToInvite = pendingPlayersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}
}
I don't know what am I doing wrong....
Thanks for any suggestions for correction.
:)

Related

CNContactPickerViewController gets error (CNPropertyNotFetchedException) - Objective-C

I am calling the Contact Picker View like so:
- (void)openContacts {
CNContactPickerViewController *picker = [[CNContactPickerViewController alloc] init];
picker.delegate = self;
picker.displayedPropertyKeys = #[CNContactGivenNameKey];
[self presentViewController:picker animated:YES completion:nil];
}
And I am handling the selections with this delegate method:
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {
for (CNContact *contact in contacts) {
NSLog(#"%#",contact);
NSString *phone;
NSString *mobilePhone;
for (CNLabeledValue *label in contact.phoneNumbers) {
if ([label.label isEqualToString:CNLabelPhoneNumberMobile] || [label.label isEqualToString:CNLabelPhoneNumberiPhone]) {
mobilePhone = [label.value stringValue];
} else if ([label.label isEqualToString:CNLabelPhoneNumberMain]) {
phone = [label.value stringValue];
}
}
}
}
The NSLog outputs the following for contact: <CNContact: 0x78e56e50: identifier=1861C9BD-143B-4E93-8475-F9079F5D6192, givenName=Kate, familyName=Bell, organizationName=Creative Consulting, phoneNumbers=(not fetched), emailAddresses=(not fetched), postalAddresses=(not fetched)>
This only occurs on simulator and iOS9 iPad. I don't get an error with my iOS10 iPhone.
You should first ask permission for this. Then you can show picker.
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error)
{
CNContactPickerViewController *picker = [[CNContactPickerViewController alloc] init];
picker.delegate = self;
picker.displayedPropertyKeys = #[CNContactGivenNameKey];
[self presentViewController:picker animated:YES completion:nil];
}];
In this case you'll show picker always, without dependance on user's choice. So you should check property availability like that:
if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized)
{
}

FBFriendPickerViewController (FBTaggableFriendPickerViewController) has no navigation bar

In iOS 8 FBFriendPickerViewController is shown without navigation bar (in iOS 7 everything was perfect).
I use Facebook-iOS-SDK v3.9.0, but I checked with latest SDK and there is same behaviour also (I run sample project called Scrumptious).
Code:
- (void)pickFacebookFriends{
FBFriendPickerViewController *friendPicker = [[FBFriendPickerViewController alloc] init];
ABAddressBookCreateWithOptions(NULL, NULL);
ABPersonSortOrdering sortOrdering = ABPersonGetSortOrdering();
ABPersonCompositeNameFormat nameFormat = ABPersonGetCompositeNameFormatForRecord(NULL);
friendPicker.sortOrdering = (sortOrdering == kABPersonSortByFirstName) ? FBFriendSortByFirstName : FBFriendSortByLastName;
friendPicker.displayOrdering = (nameFormat == kABPersonCompositeNameFormatFirstNameFirst) ? FBFriendDisplayByFirstName : FBFriendDisplayByLastName;
friendPicker.fieldsForRequest = [[NSSet alloc] initWithObjects:#"picture", nil];
[friendPicker loadData];
[friendPicker presentModallyFromViewController:self
animated:YES
handler:^(FBViewController *sender, BOOL donePressed) {
if (donePressed) {
if (friendPicker.selection.count > 0) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}}];
return;
[self presentViewController:friendPickerController animated:YES completion:nil];
}
This is screenshot:
Be sure that you nav.bar doesn't hide and do push.
[self.navigationController setNavigationBarHidden:NO animated:NO];
_taggableFriendPickerViewController = [FBTaggableFriendPickerViewController new];
[_taggableFriendPickerViewController loadData];
_taggableFriendPickerViewController.delegate = self;
[self.navigationController pushViewController:_taggableFriendPickerViewController animated:YES];
#pragma mark - FBViewControllerDelegate
///When you press on done button will call this method.
- (void)facebookViewControllerDoneWasPressed:(FBViewController *)sender
{
_arrayWithUsers = ((FBTaggableFriendPickerViewController *)sender).selection;
}
You need to conform to <FBViewControllerDelegate>, then:
FBFriendPickerViewController *friendPicker = [[FBFriendPickerViewController alloc] init];
friendPicker.delegate = self;
ABAddressBookCreateWithOptions(NULL, NULL);
ABPersonSortOrdering sortOrdering = ABPersonGetSortOrdering();
ABPersonCompositeNameFormat nameFormat = ABPersonGetCompositeNameFormatForRecord(NULL);
friendPicker.sortOrdering = (sortOrdering == kABPersonSortByFirstName) ? FBFriendSortByFirstName : FBFriendSortByLastName;
friendPicker.displayOrdering = (nameFormat == kABPersonCompositeNameFormatFirstNameFirst) ? FBFriendDisplayByFirstName : FBFriendDisplayByLastName;
friendPicker.fieldsForRequest = [[NSSet alloc] initWithObjects:#"picture", nil];
[friendPicker loadData];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:friendPicker];
[self presentViewController:nc animated:YES completion:nil];
- (void)facebookViewControllerDoneWasPressed:(id)sender{
NSLog(#"%s",__PRETTY_FUNCTION__);
FBFriendPickerViewController *friendPicker = sender;
NSLog(friendPicker.selection.description);
}
The result is:

How can I load a landscape xib using one ViewController file?

I have a universal app. I'm using separate xibs for the portrait and landscape views. I have the app detecting the orientation and changing the value of a BOOL to true when I'm in landscape. I want to know how to load my landscape xib when that BOOL is true. I've tried several different methods to achieve this, but nothing has worked. Any input on this matter would be most appreciated. I can update this post to include any code snippets necessary. Thanks in advance.
edit: I want to do all of this in one ViewController class, and only for the iPad... not the iPhone. I have all that part worked out. I just need to load the landscape xib.
edit: In my viewDidLoad I'm doing this:
if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight) {
landscape = YES;
}
Here's my main view controller .m:
#implementation PassportAmericaViewController
#synthesize browseViewButton, webView, mainView, lblMemberName, menuOpen, internetActive, hostActive, isUsingiPad, portrait, landscape;
- (void)viewDidLoad {
menuOpen = NO;
UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
isUsingiPad = YES;
}
if (isUsingiPad)
if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight) {
landscape = YES;
}
[self checkForKey];
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES];
}
-(void) checkForKey{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int regCheck = [defaults integerForKey:#"registration"];
if (isUsingiPad) {
if (regCheck == 0) {
RegistrationViewController *regView = [[RegistrationViewController alloc]
initWithNibName:#"RegistrationView-iPad" bundle:[NSBundle mainBundle]];
regView.isUsingiPad = YES;
[self.navigationController pushViewController:regView animated:YES];
}else if (regCheck == 1) {
#try {
NSString *mbrFirstName = [defaults objectForKey:#"firstName"];
NSString *mbrLastName = [defaults objectForKey:#"lastName"];
NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
[name appendString:#" "];
[name appendString:mbrLastName];
lblMemberName.text = name;
}
#catch (NSException *exception) {
}
}
}else{
if (regCheck == 0) {
RegistrationViewController *regView = [[RegistrationViewController alloc]
initWithNibName:#"RegistrationView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:regView animated:YES];
}else if (regCheck == 1) {
#try {
NSString *mbrFirstName = [defaults objectForKey:#"firstName"];
NSString *mbrLastName = [defaults objectForKey:#"lastName"];
NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
[name appendString:#" "];
[name appendString:mbrLastName];
lblMemberName.text = name;
}
#catch (NSException *exception) {
}
}
}
}
-(IBAction) openBrowseView{
if (isUsingiPad && landscape) {
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:#"BrowseView-iPadLandscape" bundle:[NSBundle mainBundle]];
browseView.isUsingiPad = YES;
[self.navigationController pushViewController:browseView animated:YES];
}else if (isUsingiPad){
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:#"BrowseView-iPad" bundle:[NSBundle mainBundle]];
browseView.isUsingiPad = YES;
[self.navigationController pushViewController:browseView animated:YES];
}else{
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:#"BrowseView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:browseView animated:YES];
}
}
-(IBAction) openViewMore{
if (isUsingiPad) {
ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
initWithNibName:#"ViewMoreView-iPad" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:viewMoreView animated:YES];
viewMoreView.isUsingiPad = YES;
}else{
ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
initWithNibName:#"ViewMoreView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:viewMoreView animated:YES];
}
}
-(IBAction) callTollFree:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:8002837183"]];
}
-(IBAction)clickToJoin:(id)sender {
if (isUsingiPad) {
webView = [[WebViewController alloc]
initWithNibName:#"WebView-iPad" bundle:[NSBundle mainBundle]];
webView.url=#"http://www.passport-america.com/Members/JoinRenew.aspx";
[self.navigationController pushViewController:webView animated:YES];
webView.isUsingiPad = YES;
}else {
webView = [[WebViewController alloc]
initWithNibName:#"WebView" bundle:[NSBundle mainBundle]];
webView.url=#"http://www.passport-america.com/Members/JoinRenew.aspx";
[self.navigationController pushViewController:webView animated:YES];
}
}
-(IBAction) iPadContactUs:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"mailto:info#passport-america.com"]];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([animationID isEqualToString:#"slideMenu"]){
UIView *sq = (__bridge UIView *) context;
[sq removeFromSuperview];
}
}
-(void) positionViews {
if (isUsingiPad) {
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
initWithNibName:#"PassportAmericaViewController-iPad" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView2 animated:YES];
homeView2.isUsingiPad = YES;
homeView2.portrait = YES;
homeView2.landscape = NO;
}else{
PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
initWithNibName:#"PassportAmericaViewController-iPadLandscape" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView2 animated:YES];
homeView2.isUsingiPad = YES;
homeView2.portrait = NO;
homeView2.landscape = YES;
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (isUsingiPad) {
return YES;
}else{
// Return YES for supported orientations
return NO;
}
}
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration {
if (isUsingiPad) {
[self positionViews];
}else{
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#end
Edit
It looks like you need to do your work here
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? #"landscapeNibName" : #"portraitNibName";
RegistrationViewController *regView = [[RegistrationViewController alloc] initWithNibName:nibName bundle:[NSBundle mainBundle]];
I'm not sure where you are getting stuck...
- (id)init;
{
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? #"landscapeNibName" : #"portraitNibName";
self = [super initWithNibName:nibName bundle:nil];
if (self) {
// any other init stuff
}
return self;
}
or if you prefer to name the nib when you instantiate
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? #"landscapeNibName" : #"portraitNibName";
MyViewController *viewController = [[MyViewController alloc] initWithNibName:nibName bundle:nil];

Object is deallocated don't know why?

Please help me I am not able to understand why this is happening there a object _getProductType is deallocate without any reason. please take a look, I am not able to figure out what is happening , if you can help me I will be very thank full to you, Thanks in advance
//
// ProductComponentViewController.m
// TurfNutritionTool
//
// Created by Aashish Joshi on 10/14/11.
// Copyright 2011 Abacus Consultancy Services. All rights reserved.
//
#import "ProductComponentViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation ProductComponentViewController
#synthesize productTableView = _productTableView;
#synthesize productTypeSelector = _productTypeSelector;
#synthesize turftypePopover = _turftypePopover;
#synthesize gotTurftype = _gotTurftype;
#synthesize resultProduct = _resultProduct;
#synthesize productTableCellStyle = _productTableCellStyle;
#synthesize dbObject = _dbObject;
#synthesize getProductType = _getProductType;
#define Granular #"G"
#define Liquid #"L"
#define Tankmix #"T"
#define AllProduct #"A"
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// D o any additional setup after loading the view from its nib.
UIColor *_background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"main_bg.png"]];
self.view.backgroundColor = _background;
[_background release];
// init the segment button value
[productTypeSelector setSelectedSegmentIndex:0];
_getProductType = [[NSString alloc] initWithString:AllProduct];
// set table delegate
_productTableView.delegate = self;
// load the product
[self loadProductComponentValues];
// Set the table view to be rounded
[[_productTableView layer] setCornerRadius:5.0];
}
- (void)viewDidUnload
{
[self setProductTableView:nil];
[self setProductTypeSelector:nil];
_productTableView = nil;
_productTypeSelector = nil;
_turftypePopover = nil;
_gotTurftype = nil;
_resultProduct = nil;
_productTableCellStyle = nil;
_getProductType = nil;
[_getProductType release];
[_productTableView release];
[_productTypeSelector release];
[_turftypePopover release];
[_gotTurftype release];
[_resultProduct release];
[_productTableCellStyle release];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)dealloc {
[_getProductType release];
[_productTableView release];
[_productTypeSelector release];
[_turftypePopover release];
[_gotTurftype release];
[_resultProduct release];
[_productTableCellStyle release];
[super dealloc];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// #warning Incomplete method implementation.
// Return the number of rows in the section.
// NSLog(#"%s", __FUNCTION__);
return [self.resultProduct count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"productContentTableCellStyle" owner:self options:nil];
cell = _productTableCellStyle;
self.productTableCellStyle = nil;
}
NSDictionary * _productRow = [_dbObject getProductdetail:[self.resultProduct objectAtIndex:indexPath.row]];
// Configure the cell...
UILabel* _label = (UILabel *)[cell viewWithTag:1];
NSString* _linkcode = [_productRow objectForKey:#"linkcode"];
_label.text = _linkcode;
_label = (UILabel *)[cell viewWithTag:2];
NSString* _description = [_productRow objectForKey:#"description"];
_label.text = _description;
_label = (UILabel *)[cell viewWithTag:3];
NSString* _productcode = [_productRow objectForKey:#"productcode"];
_label.text = _productcode;
_label = (UILabel *)[cell viewWithTag:4];
NSString* _weight = [_productRow objectForKey:#"weight"];
_label.text = _weight;
_label = (UILabel *)[cell viewWithTag:6];
NSNumber* _costperBag = [[NSNumber alloc] initWithFloat:[[_dbObject getUserProductCost:[self.resultProduct objectAtIndex:indexPath.row]] floatValue]];
_label.text = [#"$ " stringByAppendingString:[_costperBag stringValue]];
[_costperBag autorelease];
_getProductType = [_productRow objectForKey:#"producttype"];
if ([_getProductType isEqualToString:#"G"]) {
_label = (UILabel *)[cell viewWithTag:10];
NSString* _weightTag = [[NSString alloc] initWithString:#"Weight in Lbs"];
_label.text = _weightTag;
[_weightTag autorelease];
_label = (UILabel *)[cell viewWithTag:11];
NSString* _SGNTag = [[NSString alloc] initWithString:#"SGN"];
_label.text = _SGNTag;
[_SGNTag autorelease];
_label = (UILabel *)[cell viewWithTag:5];
NSString* _sgn = [_productRow objectForKey:#"sgn"];
_label.text = _sgn;
} else if([_getProductType isEqualToString:#"L"]) {
_label = (UILabel *)[cell viewWithTag:10];
NSString* _weightTag = [[NSString alloc] initWithString:#"Weight in Ozs"];
_label.text = _weightTag;
[_weightTag autorelease];
_label = (UILabel *)[cell viewWithTag:11];
NSString* _SGTag = [[NSString alloc] initWithString:#"SG"];
_label.text = _SGTag;
[_SGTag autorelease];
_label = (UILabel *)[cell viewWithTag:5];
NSString* _sgn = [_productRow objectForKey:#"sg"];
_label.text = _sgn;
} else if([_getProductType isEqualToString:#"T"]) {
_label = (UILabel *)[cell viewWithTag:10];
NSString* _weightTag = [[NSString alloc] initWithString:#"Weight in Ozs"];
_label.text = _weightTag;
[_weightTag autorelease];
_label = (UILabel *)[cell viewWithTag:11];
NSString* _SGTag = [[NSString alloc] initWithString:#"SG"];
_label.text = _SGTag;
[_SGTag autorelease];
_label = (UILabel *)[cell viewWithTag:5];
NSString* _sgn = [_productRow objectForKey:#"sg "];
_label.text = _sgn;
}
return cell;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIColor *_background = [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"toolbar_bkg.png"]] autorelease];
UIView* _customView = [[[UIView alloc]initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]autorelease];
_customView.backgroundColor = _background;
return _customView;
}
- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIColor *_background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"toolbar_bkg.png"]];
UIView* _customView = [[[UIView alloc]initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]autorelease];
_customView.backgroundColor = _background;
[_background release];
return _customView ;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
if (_delegate != nil) {
NSString *_selectedTurftype = [_getTurftype objectAtIndex:indexPath.row];
[_delegate getSelectedTurftype:_selectedTurftype];
}
*/
// NSDictionary * _productRow = [_dbObject getProductdetail:[self.resultProduct objectAtIndex:indexPath.row]];
// Animate the deselection
[self.productTableView deselectRowAtIndexPath:[self.productTableView indexPathForSelectedRow] animated:YES];
CGRect _popoverRect = CGRectMake(800.0f, 380.0f, 10.0f, 10.0f);
if ([_turftypePopover isPopoverVisible]) {
[_turftypePopover dismissPopoverAnimated:YES];
}
else
{
ProductDetailViewController* _productDetailViewControllerObj = [[ProductDetailViewController alloc] init];
_turftypePopover = [[UIPopoverController alloc]
initWithContentViewController:_productDetailViewControllerObj];
_productDetailViewControllerObj.dbObject = _dbObject;
[_productDetailViewControllerObj getSelectedProductId:[self.resultProduct objectAtIndex:indexPath.row] ];
[_productDetailViewControllerObj release];
_turftypePopover.popoverContentSize = CGSizeMake(360, 500);
[_turftypePopover presentPopoverFromRect:_popoverRect inView:self.view
permittedArrowDirections:0 animated:YES];
}
[self.productTableView deselectRowAtIndexPath:[self.productTableView indexPathForSelectedRow] animated:YES];
}
#pragma mark - ProductComponentViewController lifecycle methods
- (IBAction)laodTurftype:(id)sender {
[self initAllTheProduct];
if (_getProductType == (id)[NSNull null] && _getProductType == nil) {
_getProductType = [NSString stringWithString:AllProduct];
}
if ([_turftypePopover isPopoverVisible]) {
[_turftypePopover dismissPopoverAnimated:YES];
}
else
{
TurftypePopoverViewController* _turftypeControllerObj = [[TurftypePopoverViewController alloc] init];
_turftypeControllerObj.dbObject = _dbObject;
_turftypeControllerObj.delegate = self;
_turftypePopover = [[UIPopoverController alloc]
initWithContentViewController:_turftypeControllerObj];
[_turftypeControllerObj release];
_turftypePopover.popoverContentSize = CGSizeMake(150, 225);
[_turftypePopover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
- (IBAction)setProductType:(id)sender {
switch (_productTypeSelector.selectedSegmentIndex) {
case 0:
_getProductType = [NSString stringWithString:AllProduct];
break;
case 1:
_getProductType = [NSString stringWithString:Granular];
break;
case 2:
_getProductType = [NSString stringWithString:Liquid];
break;
case 3:
_getProductType = [NSString stringWithString:Tankmix];
break;
}
[self loadProductComponentValues];
// Check Point
[TestFlight passCheckpoint:#"SET_PRODUCT_TYPE"];
}
// This is Delgate Method to get selceted product
-(void) getSelectedTurftype:(NSString*) getTurftype {
self.gotTurftype = getTurftype;
NSLog(#"self.gotTurftype %#", self.gotTurftype);
[self loadProductComponentValues];
if ([_turftypePopover isPopoverVisible]) {
[_turftypePopover dismissPopoverAnimated:YES];
}
}
-(void) initAllTheProduct {
_getProductType = [NSString stringWithString:AllProduct];
self.gotTurftype = nil;
// init the segment button value
[productTypeSelector setSelectedSegmentIndex:0];
[self loadProductComponentValues];
// Check Point
[TestFlight passCheckpoint:#"SET_ALL_PRODUCT"];
}
- (IBAction)setAllProduct:(id)sender {
[self initAllTheProduct];
}
// This Method use for Load Value of ProductComponent
- (NSMutableArray*) loadProductComponentValues {
[self.resultProduct removeAllObjects];
if (!_dbObject) [self loadDBAccessDatabase];
self.resultProduct = [[NSMutableArray alloc] initWithArray:[self.dbObject getRelatedProductArray:self.gotTurftype andProductType:_getProductType]];
[_productTableView reloadData];
return self.resultProduct;
}
- (NSMutableArray *) loadProductComponentValuesIfEmpty {
// NSLog(#"%s", __FUNCTION__);
[self initAllTheProduct];
if (!_dbObject) [self loadDBAccessDatabase];
if (!self.resultProduct || ![self.resultProduct count]) self.resultProduct = [[NSMutableArray alloc] initWithArray:[self.dbObject getRelatedProductArray:self.gotTurftype andProductType:_getProductType]];
// Check Point
[TestFlight passCheckpoint:#"LOAD_DATABASE"];
return self.resultProduct;
}
- (DBAccess *) loadDBAccessDatabase {
// NSLog(#"%s", __FUNCTION__);
if (!_dbObject) {
NSString * _dbFileName = #"turfnutritiontool.db";
_dbObject = [[DBAccess alloc] initWithSSDBAccessFilename:_dbFileName];
}
return _dbObject;
}
#end
Prefer using property accessors to directly accessing instance variables in methods other than init... and dealloc. For example, there are a number of places where your code is currently doing things like this:
_getProductType = [NSString stringWithString:AllProduct];
Leaving aside that getProductType is a silly name for a property (which goes double for the instance variable, not to mention that the synthesized setter method name would be setGetProductType), this code directly assigns an object to an instance variable without taking ownership of the object. Don't do this. Instead, do one of the following:
// First let's rename your property. In the .h file, modify the current declaration like so:
#property (nonatomic, copy) NSString *productType;
// In the .m file, change the #synthesize statement:
#synthesize productType = _productType;
// In cleaning up any compiler errors/warnings from references to the old names,
// modify code that directly assigns to the instance variable (other than in `dealloc`):
// So change this:
_getProductType = [NSString stringWithString:AllProduct];
// to the following:
self.productType = [NSString stringWithString:AllProduct];
// ...or better yet:
self.productType = AllProduct;
// Note that the above statement is equivalent to the following:
[self setProductType:AllProduct];
EDIT
Also as #samfisher correctly points out, don't set ivars to nil before sending them release messages in dealloc.
use:
self._getProductType = [[NSString alloc] initWithString:AllProduct];
change this sequence:
[_getProductType release];
[_productTableView release];
[_productTypeSelector release];
[_turftypePopover release];
[_gotTurftype release];
[_resultProduct release];
[_productTableCellStyle release];
_productTableView = nil;
_productTypeSelector = nil;
_turftypePopover = nil;
_gotTurftype = nil;
_resultProduct = nil;
_productTableCellStyle = nil;
_getProductType = nil;
you were leaking memory as setting pointers to nil, //memory leak
first you should release then set pointers to nil;// no leak
When _getProductType is allocated on this line
_getProductType = [[NSString alloc] initWithString:AllProduct];
it is not retained in any way.
and as soon as _getProductType goes out of scope (which is after viewDidLoad finishes) you will not be able to reference a valid object.
so every time you reference _getProductType outside of viewDidLoad it will be as if 'released'.
Your dealloc method releases it correctly so all you have to do is add a 'retain' after allocation/initialisation, thus:
_getProductType = [[[NSString alloc] initWithString:AllProduct] retain];
However, it might be a really good idea to study properties and how they work, how they can be set up to do things like retaining or copying data as and when it is initialised. Your starting point is here.

GameCenter Invitation Handler

trying to implement a multiplayer. Using the sample from Game Center - Sending and receiving data.
Everything seems okay, but in apple documentation there is also said about invitation handler.
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite) {
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
[self presentModalViewController:mmvc animated:YES];
} else if (playersToInvite) {
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 4;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[self presentModalViewController:mmvc animated:YES];
}
};
The problem is quite simple: I do not know where to add this code.
As stated in the docs
Your application should set the
invitation handler as early as
possible after your application is
launched; an appropriate place to set
the handler is in the completion block
you provided that executes after the
local player is authenticated.
Somewhere in your code, you should have authenticated the local player with something like this
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
if (error == nil) {
// Insert your piece of code here
} else {
// Handle the error
}
}];
Hope that helps
My code is below, and it works very well. In the authenticateLocalUser, add the code below:
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { // Add for invite handler
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite) {
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] ;
mmvc.matchmakerDelegate = self;
// [self presentModalViewController:mmvc animated:YES];
[_delegate matchStart];
} else if (playersToInvite) {
GKMatchRequest *request = [[GKMatchRequest alloc] init] ;
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request] ;
mmvc.matchmakerDelegate = self;
// [self presentModalViewController:mmvc animated:YES];
[_delegate matchStart];
}
};
[self callDelegateOnMainThread:#selector(processGameCenterAuth:) withArg:NULL error:error];
}];