Trouble with matchmaking in iOS with gamekit and game centre - objective-c

At the moment I'm trying to add online multiplayer to my iOS game (that uses UIKit) using gameKit/GameCenter for matchmaking. I'm trying to present game center's matchmaker interface but with varied success. I have two view controllers (with respective .xib files), mainMenuViewController and onlineViewController, along with a file called GCHelper.h.
In CGHelper I have this:
- (void)findMatchWithMinPlayers:(int)minPlayers
maxPlayers:(int)maxPlayers
viewController:(UIViewController *)viewController
delegate:(id<GCHelperDelegate>)theDelegate
{
if (!gameCenterAvailable) return;
self.presentingViewController = viewController;
delegate = theDelegate;
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
}
I use this function in my views to show the matchmaking interface. At first I tried it in mainMenuViewController, which is my first view controller using this:
[[GCHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:2 viewController:self delegate:self];
And that worked fine but that's not where I wanted to do it, so I tried the exact same thing from onlineViewController and nothing happens, no error, just the blank view of onlineViewController.view stares back at me. I'm fairly new at this so I'm wondering if it's down to how I'm dealing with the views, here's the code I use to switch from the mainMenu to the other view:
-(IBAction)showOnlineView
{
OnlineViewController *onlineViewController = [[OnlineViewController alloc] initWithNibName:#"OnlineViewController" bundle:nil];
UIView *currentView = self.view;
UIView *theWindow = [currentView superview];
[currentView removeFromSuperview];
[theWindow addSubview:onlineViewController.view];
}
Any help would be greatly appreciated. Also, I'm not sure how I should deal with going back to the main menu if the user presses cancel (although that may become self evident once I've got this working, I just thought I'd mention it in case this lack of knowledge on my part makes it obvious I'm going about the whole thing incorrectly).

Related

UIRefreshControl without UITableViewController not calling selector

I have a UIViewController that has a UITableView as a subview. I am trying to add a pull-to-refresh on the table.
Following some examples and discussions that I found here, I have the UIRefresh showing but it never calls the selector. I am not getting notified that the pull action happened.
I cannot use a UITableViewController as my main controller as I need to add a fixed button at the bottom of the screen.
I have the feeling I am missing something out that hopefully is obvious to someone else.
#interface ActivityViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
#end
- (void)viewDidLoad{
[super viewDidLoad];
_myTableView.delegate = self;
_myTableView.dataSource = self;
_tableViewController = [UITableViewController new];
_tableViewController.tableView = _myTableView;
[self addChildViewController:_tableViewController]; // Not sure this is necessary
_refreshControl = [UIRefreshControl new];
[_refreshControl addTarget:self action:#selector(loadMoreData) forControlEvents:UIControlEventValueChanged];
_tableViewController.refreshControl = _refreshControl;
}
- (void)loadMoreData{
NSLog(#"loadMoreData");
}
Ok this is now working. But I am not sure why!! I kinda left it for a bit, changed other things that needed doing and then tested once more last night and it was working. I admit I am a bit confused. I used the code below from this answer given in earlier posts here as I did a few days ago. So thank you for the time and input. It works perfect now, as expected.
// Refresh control
UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = self.myTableView;
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:#selector(loadMoreData) forControlEvents:UIControlEventValueChanged];
tableViewController.refreshControl = self.refreshControl;
You should instantiate your tableviewcontroller with
[UITableViewController alloc] initWithStyle:(UITableViewStyle)
using new does an alloc init but you should use a designated initializer

UIPageViewControllerSpineLocation Delegate Method Not Firing

Major head-scratcher all day on this one :-(
I have an instance of a UIPageViewController that does not appear to be firing the delegate method:
-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
I have tried various methods of displaying the UIPageViewController and have settled on a programatic approach (as opposed to a Storyboard one) that appears to be working correctly, with one exception... when rotating the iPad to landscape the spine does not appear mid-point as expected. I simply cannot find out why the delegate method does not get called.
Code Explanation (simplified for example)
Consider three classes as follows:
RootViewController - loaded when the app starts
PageViewController - loaded by RootViewController upon user initiation
PageContentViewController - loaded by PageViewController when pages are needed
Fairly self-explanatory. The RootViewController is loaded by the app upon launch. When the user taps an image within this view controller's view (think magazine cover opening a magazine) it launches the PageViewController as follows:
PageViewController *pvc = [[PageViewController alloc] initWithNibName:#"PageView"
bundle:[NSBundle mainBundle]];
pvc.view.frame = self.view.bounds;
[self.view addSubview:pvc.view];
In the actual app there is animation etc to make the transition all nice, but essentially the PageViewController's view is loaded and takes fullscreen.
PageViewController
This is the workhorse (only relevant methods shown). I have tried various examples from the infinite world of Google and written directly from the Apple docs...
#interface PageViewController : UIViewController <UIPageViewControllerDelegate, UIPageViewControllerDataSource>
#property (nonatomic, strong) UIPageViewController *pageViewController;
#property (nonatomic, strong) NSMutableArray *modelArray;
#end
#implementation TXCategoryController
-(void)viewDidLoad
{
[super viewDidLoad];
// Simple model for demo
self.modelArray = [NSMutableArray alloc] init];
for (int i=1; i<=20; i++)
[self.modelArray addObject:[NSString stringWithFormat:#"Page: %d", i]];
self.pageViewController = [[UIPageViewController alloc]
initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
PageContentViewController *startupVC = [[PageContentViewController alloc] initWithNibName:#"PageContent" bundle:nil];
startupVC.pageLabel = [self.modelArray objectAtIndex:0];
[self.pageViewController setViewControllers:[NSArray arrayWithObject:startupVC]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
self.pageViewController.view.frame = self.view.bounds;
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
// Relevant code to add another view...
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController
{
// Relevant code to add another view...
}
-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
// Setting a break point in here - never gets called
if (UIInterfaceOrientationIsPortrait(orientation))
{
// Relevant code to create view...
return UIPageViewControllerSpineLocationMin;
}
// Relevant code to create 2 views for side-by-side display and
// set those views using self.pageViewController setViewControllers:
return UIPageViewControllerSpineLocationMid
}
#end
This all works perfectly well as I mentioned earlier. The PageViewController's view gets shown. I can swipe pages left and right in both portrait and landscape and the respective page number appears. However, I don't ever see two pages side-by-side in landscape view. Setting a breakpoint in the spineLocationForInterfaceOrientation delegate method never gets called.
This is such a head-scratcher I have burned out of ideas on how to debug/solve the problem. It almost behaves like the UIPageViewController isn't responding to the orientation changes of the device and therefore isn't firing off the delegate method. However, the view gets resized correctly (but that could be just the UIView autoresizing masks handling that change).
If I create a brand new project with just this code (and appropriate XIb's etc) it works perfectly fine. So something somewhere in my actual project is causing this. I have no idea where to continue looking.
As usual, any and all help would be very much appreciated.
Side Note
I wanted to add the tag 'uipageviewcontrollerspinelocation' but couldn't because it was too long and I didn't have enough reputation (1500 required). I think this is a devious ploy on Apple's part to avoid certain tags in Stackoverflow... ;-)
Finally found the problem. It was something of a red herring in its symptoms, but related just the same.
Putting a break point in the shouldAutorotateToInterfaceOrientation: method was a natural test to see if the UIViewController was even getting a rotation notification. It wasn't which led me to Apple's technical Q&A on the issue: http://developer.apple.com/library/ios/#qa/qa1688/_index.html
The most relevant point in there was:
The view controller's UIView property is embedded inside UIWindow but alongside an additional view controller.
Unfortunately, Apple, in its traditional documentation style, doesn't provide an answer, merely confirmation of the problem. But an answer on Stack Overflow yielded the next clue:
Animate change of view controllers without using navigation controller stack, subviews or modal controllers?
Although my RootViewController was loading the PageViewController, I was doing it as a subview to the main view. This meant I had two UIViewController's in which only the parent would respond to changes.
The solution to get the PageViewController to listen to the orientation changes (thus triggering the associated spine delegate method) was to remove addSubview: and instead present the view controller from RootViewController:
[self presentViewController:pac animated:YES completion:NULL];
Once that was done, the orientation changes were being picked up and the PageViewController was firing the delegate method for spine position. Only one minor detail to consider. If the view was launched in landscape, the view was still displaying portrait until rotated to portrait and back to landscape.
That was easily tweaked by editing viewDidLoad as follows:
PageContentViewController *page1 = [[PageContentViewController alloc] initWithNibName:#"PageContent" bundle:nil];
NSDictionary *pageViewOptions = nil;
NSMutableArray *pagesArray = [NSMutableArray array];
if (IS_IPAD && UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
pageViewOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMid]
forKey:UIPageViewControllerOptionSpineLocationKey];
PageContentViewController *page2 = [[PageContentViewController alloc] initWithNibName:#"PageContent" bundle:nil];
[pagesArray addObject:page1];
[pagesArray addObject:page2];
}
else
{
[pagesArray addObject:page1];
}
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:pageViewOptions];
self.pageViewController.delegate = self;
[self.pageViewController setViewControllers:pagesArray
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
Job done and problem solved.

In app settings (application is closed)

My application crashes when I close the settings.
Even with a standard animation (UIModalTransitionStyleCoverVertical) button "Done" is not working..
What could be wrong?
-EDIT- Apparently I was not quite accurate in describing the problem: XCode does not receive notice of a crash. The application simply closes. As a result, achievements gained during the session are lost.
- (IBAction)BarButtonPageCurlAction:(id)sender {
IASKAppSettingsViewController *SettingsViewController;
SettingsViewController = [[[IASKAppSettingsViewController alloc] initWithNibName:#"IASKAppSettingsView" bundle:nil] autorelease];
SettingsViewController.delegate = self;
//SettingsViewController.showDoneButton = NO;
//SettingsViewController.showCreditsFooter = NO;
SettingsViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
UINavigationController *NavController = [[[UINavigationController alloc] initWithRootViewController:SettingsViewController] autorelease];
//[NavController setNavigationBarHidden:YES];
[self presentModalViewController:NavController animated:YES];}
my class files: .m.h
You don't have the proper delegate methods implemented. Look at the documentation of Inappsettings, there are methods used for dismissing the settings view.

UIImagePickerController - tapping the Album TableView or Selecting Album causes table to go blank? iOS5

I'm trying to reproduce a Similar Media Picker that is like the one in Pages. Within a UIPopoverController There is a UISegmentedControl that selects different media Types. One of the SegmentedControls I have is Labeled Images. I want to be able to select that Segment and have the view below present the ImagePicker.
I'm Close. I have a few issues. When presenting the VC, I get:
I get the Following in the Debugger:
UIStatusBarStyleBlackTranslucent is not available on this device. Not sure where that is coming from. I've tried with and without:
imagePicker.modalInPopover = YES;
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
Though I still cannot get it to work Right. It Presents just fine. I see the UISegmentedControl I see my other Media Pages, I click the Image segment and I see the ImagePicker, Title is 'Photos' Has a Cancel Button I need to get rid of, and shows the two Albums I have on the device.
If I tap anywhere in the TableView (on an album or not), the two Albums go away. The NavBar and Cancel button are still there, though no Albums anymore. Tapping an Album Highlights the Row, though does not show the Images within the Album.
The other odd part of my code is that the Delegate for the Image Picker is the VC that Presented the UIPopoverController. Not sure if that plays into it. When I do hit the Cancel Button, I get:
-[PLUILibraryViewController performSelector:withObject:withObject:]: message sent to deallocated instance
Here is my Code to present the Picker.
- (void) setupImagePicker {
IoScreenEditorViewController * ioScreenEditorViewController = (IoScreenEditorViewController *)[UIAppDelegate.ioMainViewController currentViewController];
ioScreenEditorViewController.elementSelectedFromList = [elementsForPage objectAtIndex:0];
// Show an image picker to allow the user to choose a new photo.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = ioScreenEditorViewController;
imagePicker.allowsEditing = NO;
NSArray * ourMediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
[imagePicker setMediaTypes: ourMediaTypes];
[ourMediaTypes release];
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
// imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[imagePicker.view setFrame:CGRectMake(0,0, 340, 500)]; // just for testing
//imagePicker.modalInPopover = YES;
//imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
[self.view addSubview:imagePicker.view];
[imagePicker release];
}
I changed the class type that the Picker was being called from to a UIViewController and then altered the presentation code to look like this:
imagePicker.modalInPopover = YES;
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:imagePicker animated:NO completion:^{ }];
I still get the Cancel Button, though the Image Picker does seem to be working like it should.
Although I was also as of yet unable to hide the Cancel button of the picker, I would suggest you revise how you are presenting it.
As an effort to get help from the community of iOS developers about this, I've made a sample project, please go and download it. In the project you can see that I am using view controller containment in order to get the picker on screen.
This may perhaps be a better way for you do do this too?
https://bitbucket.org/danielphillips/image-picker-demo

UISplitViewController programmatically without nib/xib

I usually create my projects without IB-stuff. The first thing I do is to strip off all references to xibs, outlets updated plist, etc and so forth. No problems, works great (in my world)!
Now, I just installed 3.2 and tried to develop my first iPad app. Following same procedure as before, I created a UISplitView-based application project and stripped off all IB-stuff. Also, I followed the section in Apple's reference docs: Creating a Split View Controller Programmatically, but nevertheless, the Master-view is never shown, only the Detail-view is (no matter what the orientation is). I really have tried to carefully look this through but I cannot understand what I have missed.
Is there a working example of a UISplitViewController without the nibs floating around somewhere? I have googled but could not find any. Or do you know what I probably have missed?
Declare your splitviewcontroller in your delegate header, use something like this in your didfinishlaunching
ensure you add the UISplitViewControllerDelegate to the detailedViewController header file and that you have the delegate methods aswell. remember to import relevant header files
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
splitViewController = [[UISplitViewController alloc] init];
rootViewController *root = [[rootViewController alloc] init];
detailedViewController *detail = [[detailedViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:root];
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detail];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
splitViewController.delegate = detail;
[window addSubview:splitViewController.view];
EDIT - as per Scott's excellent suggestion below, don't add to the windows subview, instead
[self.window setRootViewController:(UIViewController*)splitViewController]; // that's the ticket
[window makeKeyAndVisible];
return YES;
}
//detailedView delegate methods
- (void)splitViewController:(UISplitViewController*)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController:(UIPopoverController*)pc
{
[barButtonItem setTitle:#"your title"];
self.navigationItem.leftBarButtonItem = barButtonItem;
}
- (void)splitViewController:(UISplitViewController*)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
self.navigationItem.leftBarButtonItem = nil;
}
I also prefer code to IB ;-)
Oldish thread, but thought I'd spare reader time + grief when the above technique fails to produce a UISplitViewController that responds correctly to device orientation change events. You'll need to:
Ensure all subordinate views respond properly in
shouldAutorotateToInterfaceOrientation. Nothing new here.
Rather than add the UISplitViewController's view to the main window,
[window addSubview:splitViewController.view]; // don't do this
instead set the main window's root controller to the UISplitViewController:
[self.window setRootViewController:(UIViewController*)splitViewController]; // that's the ticket
Adding the splitviewcontroller's view as a subview of the main window (barely) allows it to co-present with sibling views, but it doesn't fly with UISplitViewController's intended use case. A UISplitViewController is a highlander view; there can only be one.
Swift 5.2
iOS 13
Both master and detail view controllers are embedded in navigation controllers
let splitViewController = UISplitViewController()
splitViewController.delegate = self
let masterVC = MasterViewController()
let detailVC = DetailViewController()
let masterNavController = UINavigationController(rootViewController: masterVC)
let detailNavController = UINavigationController(rootViewController: detailVC)
splitViewController.viewControllers = [masterNavController,detailNavController]
You can put this code in your AppDelegate's (or in SceneDelegate if your target is iOS 13.0+)didFinishLaunchingWithOptions function. Just remember to make the splitViewController your rootViewController like this
self.window!.rootViewController = splitViewController
I had just met the same problem.
make sure that your child viewController of splitview can Autorotate to interface orientation.
you can change the function in your childViewController like this:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
then the master view will be shown.