UIActivity custom activityViewController crash on iPad - objective-c

I have a custom UIActivity that I use in order to create a Contact to the device's AddressBook. In this UIActivity, I create an ABNewPersonViewController, put it inside a UINavigationController and return it in UIActivity's
- (UIViewController *)activityViewController
The problem is that on the iPad I get a crash due to a reference to a released UINavigationController. Messages are of type:
*** -[UINavigationController _viewControllerForSupportedInterfaceOrientations]: message sent to deallocated instance 0xa6f1660
Also after my controller is dismissed, the iPad does not autorotate the view when the interface changes orientation.
The code I use is the following:
In UIActivity
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
// Prepare the AB View Controller
ABRecordRef aContact = ABPersonCreate();
CFErrorRef error = NULL;
ABRecordSetValue(aContact, kABPersonKindProperty, kABPersonKindOrganization, &error);
ABRecordSetValue(aContact, kABPersonOrganizationProperty, #"Apple Inc.", &error);
ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phone, #"+1 2345 784513", kABWorkLabel, NULL);
ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &error);
CFRelease(phone);
self.newContactVC.title = #"New company";
self.newContactVC.displayedPerson = aContact;
[self.navigation setViewControllers:[NSArray arrayWithObject:self.newContactVC]];
CFRelease(aContact);
}
- (UIViewController *)activityViewController
{
return self.navigation;
}
// Dismisses the new-person view controller.
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
[self activityDidFinish:YES];
}
In the ViewController that calls this UIActivity
- (IBAction)showActionsSheet:(id)sender {
AddToAddressBookActivity *abActivity = [[AddToAddressBookActivity alloc] init];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:nil
applicationActivities:[NSArray arrayWithObject:abActivity]];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
if (!self.popover || ![self.popover isPopoverVisible]) {
self.popover = [[UIPopoverController alloc] initWithContentViewController:activityVC];
[self.popover setDelegate:self];
self.popover.passthroughViews = nil;
[self.popover presentPopoverFromRect:self.showASBtn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
Any help would be greatly appreciated.
Link to a demo project:
http://ge.tt/23MeOYq/v/0?c

I had a similar problem, except the crash occurred on rotation after the activity view controller was dismissed. In my case, I had the custom view controller in a storyboard setup to present as "fullscreen", and on changing it to display as "form sheet" the crash went away. I don't know why that made a difference though.
Regarding your autorotation not working after the custom view controller dismisses, try adding a completion handler to your activity view controller to nil your popover view controller ivar. I noticed that if I didn't do this the custom activity and the custom view controller don't get dealloc'd, and autorotation doesn't happen. e.g.:
avc.completionHandler = ^(NSString* activityType, BOOL completed){
if ( UIUserInterfaceIdiomPad == [UIDevice currentDevice].userInterfaceIdiom )
self.popover = nil;
};
Also, check out the post at: "activityviewcontroller not dismissing", which I found helpful.

Related

ipad objective c using removeFromSuperview to remove UICollectionViewController throws an error

So I'm customizing this control I found since I think it works very well except with this issue of mine:
http://www.cocoacontrols.com/controls/fsverticaltabbarcontroller
I wanted to load a UICOllectionViewCOntroller instead of a regular ViewController whenever an item is tapped on the sidebar. So I did this modification when selecting an item:
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
NSLog(#"selected Index is %#", [NSNumber numberWithInt:selectedIndex]);
NSLog(#"_selected Index is %#", [NSNumber numberWithInt:_selectedIndex]);
NSLog(#"vc counts is %i", [self.viewControllers count]);
if (selectedIndex != _selectedIndex && selectedIndex < [self.viewControllers count])
{
// add new view controller to hierarchy
UIViewController *selectedViewController = [self getSelectedVCWithSelectedIndex:selectedIndex];
[self addChildViewController:selectedViewController];
selectedViewController.view.frame = CGRectMake(self.tabBarWidth,
0,
self.view.bounds.size.width-self.tabBarWidth,
self.view.bounds.size.height);
selectedViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:selectedViewController.view];
// remove previously selected view controller (if any)
if (_selectedIndex != NSNotFound)
{
UIViewController *previousViewController = [self.viewControllers objectAtIndex:_selectedIndex];
NSLog(#"ERROR HERE: remove previous: previousVC = %#", previousViewController);
[previousViewController.view removeFromSuperview];
[previousViewController removeFromParentViewController];
}
// set new selected index
_selectedIndex = selectedIndex;
// update tab bar
if (selectedIndex < [self.tabBar.items count])
{
self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:selectedIndex];
}
// inform delegate
if ([self.delegate respondsToSelector:#selector(tabBarController:didSelectViewController:)])
{
[self.delegate tabBarController:self didSelectViewController:selectedViewController];
}
}
}
So what I did is since it already handles the index number of the items on teh sidebar, I just made sure it instantiates the type of controller it needs to load using this line, I have 3 regular VC and 1 collection VC:
UIViewController *previousViewController = [self.viewControllers objectAtIndex:_selectedIndex];
This is how it looks like:
-(UIViewController *)getSelectedVCWithSelectedIndex:(NSUInteger)selectedIndex{
UIViewController *selectedVC = [[UIViewController alloc]init];
// do a switch case on this.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
// need to instantiate each and every custom uinav
switch(selectedIndex){
case 1:
selectedVC = [storyboard instantiateViewControllerWithIdentifier:#"UINavAdminCategoryIndexViewControllerID"];
break;
case 2:
selectedVC = [storyboard instantiateViewControllerWithIdentifier:#"UINavAdminParticipantIndexViewControllerID"];
break;
case 3:
selectedVC = [storyboard instantiateViewControllerWithIdentifier:#"UINavAdminTablesIndexCollectionViewControllerID"];
break;
case 4:
selectedVC = [self.viewControllers objectAtIndex:selectedIndex];
break;
default:
break;
}
return selectedVC;
}
Now everything would load smoothly, but whenever I would go to the Collection VC tab and then move away from it by going to another tab it would throw this error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be
initialized with a non-nil layout parameter'
The application bombs on this part when I remove it from the superview:
[previousViewController.view removeFromSuperview];
Was wondering why it would instantiate the UIView again when all I'm doing is removing it from the stack (is that the right term?)
EDIT: Added some more codes
So I finally figured it out and hopefully someone else can find this useful. When generating a UiCollectionView you need to initiate the Layout for some reason. Idk why, I will try to find out. But this is what led me to the solution:
http://www.rqna.net/qna/ikvmhu-uicollectionview-must-be-initialized-with-a-non-nil-layout-parameter.html
Before when I instantiated the CollectionViewController on the main ViewController before the FSVerticalTabbar is called I just used the class connected to the ViewController on the storyboard eg. AdminMainController, AdminEventsCollectionController, etc.
I basically just added the Layout and used that for the UICollectionViewController when being initiated. It now removes the VC without any errors.
UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setItemSize:CGSizeMake(200, 140)];
[aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
myCollectionViewController = [[MyCollectionViewController alloc]initWithCollectionViewLayout:flowLayout];

Pop up UIViewController with an IBAction UIViewController

Is it possible to pop up an UIViewController (xib file) like UIPopOverControl in iPad ?
I have a separate NIB file which is linked to an UIViewController. I want to popup that NIB file along with the button pressed with a customised size (200,200).
Is this possible?
I am trying to get something like this on the iPhone - http://www.freeimagehosting.net/c219p
You can also use one of these custom made clases to show a popup:
https://github.com/sonsongithub/PopupView
https://github.com/werner77/WEPopover
https://github.com/50pixels/FPPopover
Example with FPPopover:
//the view controller you want to present as popover
YourViewController *controller = [[YourViewController alloc] init];
//our popover
FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller];
//the popover will be presented from the okButton view
[popover presentPopoverFromView:okButton];
//release if you arent using ARC
[controller release];
yes it is. load Your pOpOver controller lazily at the point when it is needed. add its view as a subview (you could animate the addition). make its frame size what You need and add the image You have shown as a background subview of the pOpOver controller along with other controls You want in the pop up.
good luck
UPDATE:
alright, ii will show You how ii do this in my app Lucid Reality Check (deployment target iOS4.3).
one can use a UIPopoverController to present another controllers view. what ii do first is to make sure ii always know the current orientation of the device, so ii can reposition the popup on rotation (maybe this works by itself on iOS6?). so in my base controller (from where ii want to show a popup) ii have an instance variable like this:
UIInterfaceOrientation toOrientation;
and also:
UIPopoverController *popover;
UIButton *popover_from_button;
BOOL representPopover;
popover will be reused for all popups, and popover_from_button will hold the button from which the popup is initiated.
then the next code comes into the base controller:
- (void)popoverWillRotate {
if ([popover isPopoverVisible]) {
[self dismissPopover];
representPopover = YES;
}
}
- (void)popoverDidRotate {
if (popover && representPopover) {
representPopover = NO;
[self representPopover];
}
}
these two methods have to be called every time the device is rotated, like this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
//DLOG(#"willRotateTo %i", toInterfaceOrientation);
toOrientation = toInterfaceOrientation;
if ([Kriya isPad ]) {
[self popoverWillRotate];
}
}
as one can see, first the orientation is captured then popoverWillRotate is called. this would hide the popover during the orientation animation. and after rotating, the popover must be redisplayed like this:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
//DLOG(#"didRotateFrom %i", fromInterfaceOrientation);
//[self layout:toOrientation]; //do some layout if You need
if ([Kriya isPad]) {
[self popoverDidRotate];
}
}
- (void)layout:(UIInterfaceOrientation)toInterfaceOrientation {
//one can do view layout here, and call other controllers to do their layout too
}
now that the orientation changes are worked out, the code for presenting the popover arrives here:
#pragma mark Popovers
- (void)presentPopoverWith:(id)controller fromButton:(UIButton*)button {
if (popover)
[popover release];
if (popover_from_button)
[popover_from_button release];
popover_from_button = [button retain];
popover = [[UIPopoverController alloc] initWithContentViewController:controller];
[popover setDelegate:self];
[self representPopover];
}
- (void)representPopover{
if (popover) {
UIPopoverArrowDirection arrowDirection = UIPopoverArrowDirectionAny;
UIViewController *vc = (UIViewController*)[popover contentViewController];
CGSize contentSize = [vc contentSizeForViewInPopover];
if (contentSize.width > 0 && contentSize.height > 0) {
[popover setPopoverContentSize:contentSize animated:NO];
}
//DLOG(#"representPopover rect:%#", [Kriya printRect:popover_from_button.frame]);
[popover presentPopoverFromRect:CGRectOffset(popover_from_button.frame, 0, popover_from_button.frame.size.height + 7.0) inView:self.view permittedArrowDirections:arrowDirection animated:YES];
}
}
- (void)dismissPopover {
if (popover) {
[popover dismissPopoverAnimated:YES];
}
}
finally, if one wants to be notified when the popover is dismissed, the base controller must implement a delegate method:
#pragma mark UIPopoverControllerDelegate
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
//do something important here like drink some water
}
and don't forget to make the base controller a UIPopoverControllerDelegate in its header.
a use case for this way of doing popups would then look like this:
- (void)takeImage {
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
[picker setDelegate:self];
[picker setAllowsEditing:NO];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
if ([Kriya isPad]) {
[self presentPopoverWith:picker fromButton:backgroundImageButton];
} else {
//modals on iPhone/iPod
//DLOG(#"takeImage addSubview picker");
[self presentModalViewController:picker animated:YES];
}
} else {
//DLOG(#"no camera");
}
}
this would use an image picker as the content for the popup, but one can use any controller with a valid view. so just do this:
[self presentPopoverWith:popupsContentController fromButton:tappedButton];
one should not have any missing information, :), the method [Kriya isPad] is just this:
+ (BOOL)isPad {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
// iPad capable OS
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//this is an iPad
return YES;
}else {
//this is an iPod/iPhone
return NO;
}
#else
//can not pissible be iPad
return NO;
#endif
}
ENJOY!

Application unit testing in IOS

I am testing an application in IOS5 using OCUnit. Before posting my problem I read all the relevant posts here as much I could. It really helped. I am now facing the problem below ,which I tried to narrate as clearly as possible.
My application has a login screen which is nothing but a view controller say: initialController. The root controller of my app delegate is the Navigation controller. The property initWithRootViewController of the Navigation controller is set with this initialController.
Now after logging in , the application loads another controller and I want to do unit testing in that controller without having to go through this login process.
Post login, another controller say PostloginController gets loaded, this controller's Navigation item is customized with 2 button's say: button1 and button2.
These buttons are added as the subviews of UIBarButtonItem and this UIBarButtonItem is set as the rightBarButtonItem of the PostloginController.navigationItem.rightBarButtonItem.(pseudo code)
finally PostLoginController is pushed in the Navigation controller
navigationcontroller pushviewcontroller:postLoginController.(pseudo code)
I have to write unit test code which on button1's UIControlEventTouchUpInside should launch another controller say :Newcontroller.
Loading this new controller on button1's event complete one test case.
My problem is that I don't get the subview of UIBarButtonItem which should be 2 buttons. The log show me there is one controller in the Navigation Controller and only one subview. My code is as follows:
- (void)setUp
{
[super setUp];
// Set-up code here.
self.appDelegate = (MYAppDelegate*)[[UIApplication sharedApplication] delegate ];
self.navigationController = (UINavigationController *)self.appDelegate.window.rootViewController;
}
- (void)testAppDelegate
{
STAssertNotNil(self.appDelegate, #"Cannot find the application delegate");
NSArray *tempArray = [self.navigationController viewControllers];
NSLog(#"Number of controllers in navigationController = %i", [tempArray count]);
}
- (void) testButton1
{
//self.myController = (PostLoginController*)self.navigationController.topViewController;
UIBarButtonItem *barButtonItem = self.navigationController.topViewController.navigationItem.rightBarButtonItem;
UIView *customView = barButtonItem.customView;
NSArray *subviews1 = customView.subviews;
NSLog (# "Got subviews");
NSLog(#"Number of subviews = %i", [subviews1 count]);
if ([subviews1 count] == 0)
{
NSLog (# "no subviews");
}
for (UIView* view in subviews1)
{
NSLog(#"%#", view);
if([view isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton*)view;
NSLog (# " UIButton parsed");
// Check if Button1 clicked.
NSData *data1=UIImagePNGRepresentation([btn backgroundImageForState:UIControlStateNormal]);
NSData *data2=UIImagePNGRepresentation([UIImage imageNamed:#"BUTTON1.png"]);
if([data1 isEqualToData:data2]) // CHECKING IF BUTTON1 OR BUTTON2 PRESSED BY IMAGE SINCE THEY DONT HAVE ANY TAGS SET.
{
[btn sendActionsForControlEvents:(UIControlEventTouchUpInside)];
STAssertTrue([self.navigationController.visibleViewController isMemberOfClass: [NewController class]], #" NewController failed to load!"); //BUTTON1 CLICK SHOULD LAUNCH NEWCONTROLLER - COMPLETES ONE TEST CASE.
}
}
}
}

Present a Different View Controller After Dismissing a Modal View Controller

I present a modal view controller for various UI settings in an iOS app. One of those settings allows the user to select a different main view. When they hit "Done" I want to dismiss the modal view and have the newly-selected view controller appear, without a momentary delay where the old view controller segues to the new view controller. How could this be implemented?
Update:
Here is a method I successfully implemented using Eugene's technique, but without the app delegate. Instead, this implementation is specific to my scenario where a view controller in a navigation stack presents the modal view controller in a Utility app.
- (void)swapFrontSideViewController;
{
UINavigationController *navigationVC = (UINavigationController *)[self presentingViewController];
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navigationVC.viewControllers];
UIViewControllerSubclass *selectedViewController = nil;
if ([self.selectedFrontSide isEqualToString:FRONT_SIDE_NAME1]) {
selectedViewController = [self.storyboard instantiateViewControllerWithIdentifier:FRONT_SIDE_NAME1];
} else if ([self.selectedFrontSide isEqualToString:FRONT_SIDE_NAME2]) {
selectedViewController = [self.storyboard instantiateViewControllerWithIdentifier:FRONT_SIDE_NAME2];
}
if (selectedViewController) {
[viewControllers replaceObjectAtIndex:viewControllers.count -1 withObject:selectedViewController];
[navigationVC setViewControllers:viewControllers];
self.delegate = selectedViewController;
} else {
NSLog(#"Error: Undefined Front Side Selected.");
}
}
- (IBAction)doDismiss:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; // Get the app delegate
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:appDelegate.navigationController.viewControllers]; // fetch its navigationController viewControllers stack
UIViewController *replacementController; //initialize replacement controller
[viewControllers replaceObjectAtIndex:viewControllers.count -1 withObject:replacementController]; // replace the top view controller in stack with the replacement one
[appDelegate.navigationController setViewControllers:viewControllers]; //change the stack
[self dismissModalViewControllerAnimated:YES];
}

Game Center Calling Leaderboard function not responding?

I am calling the leader board like this:
-(void)viewscores:(SPEvent*)event
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil) {
leaderboardController.leaderboardDelegate = self;
UIWindow* window = [UIApplication sharedApplication].keyWindow;
[window addSubview: self.rootViewController];
[self presentModalViewController: leaderboardController animated: YES];
}
}
When I Click the leader board button, I receive an error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIViewController presentModalViewController:animated:]: unrecognized selector sent to class 0x3e2fc7f8'
Is this normal?
You should probably call the function in a viewController. In one of my app's the code looks like this:
-(IBAction)showLeaderBoard {
GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
if (leaderBoardCont) {
leaderBoardCont.category=#"1S";
leaderBoardCont.timeScope=GKLeaderboardTimeScopeWeek;
leaderBoardCont.leaderboardDelegate=self;
[self presentModalViewController:leaderBoardCont animated:YES];
}
}
and then you should also implement the delegate method:
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
viewController = nil;
}
Here 1S is the identifier for the leader board you created in iTunes Connect. Hope this helps.
Edit: since you are still having problems, check these tutorials out. They cover everything about leader boards and achievements.
1st part
2nd part
You're sending the presentModalViewController message to an object that doesn't recognize it. So the class you wrote in your "Game.m" file doesn't inherit from UIViewController. Dunno what framework you're using, but you'll have to init a UIViewController instance to show the GameCenter view.