iOS ShareKit Cancel button is not working with Xcode 4.2 - ios

The iOS Sharekit working with previous Xcode but will 4.2 it's not working anymore, When I hit the Cancel button it goes to this routine
Inside the SHK.m
- (void)hideCurrentViewControllerAnimated:(BOOL)animated
{
if (isDismissingView)
return;
if (currentView != nil)
{
// Dismiss the modal view
if ([currentView parentViewController] != nil)
{
self.isDismissingView = YES;
[[currentView parentViewController] dismissModalViewControllerAnimated:animated];
}
else
self.currentView = nil;
}
}
I stepped the code and it just hit if (isDissmissingView) and it just return.
So, I manually inserted the code
[[currentView parentViewController] dismissModalViewControllerAnimated:animated];
to the top of the routine but this doesn't do anything.
I also includes some other codes for reference
- (void)showViewController:(UIViewController *)vc
{
if (rootViewController == nil)
{
// Try to find the root view controller programmically
// Find the top window (that is not an alert view or other window)
UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
if (topWindow.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(topWindow in windows)
{
if (topWindow.windowLevel == UIWindowLevelNormal)
break;
}
}
UIView *rootView = [[topWindow subviews] objectAtIndex:0];
id nextResponder = [rootView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
self.rootViewController = nextResponder;
else
NSAssert(NO, #"ShareKit: Could not find a root view controller. You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
}
// Find the top most view controller being displayed (so we can add the modal view to it and not one that is hidden)
UIViewController *topViewController = [self getTopViewController];
if (topViewController == nil)
NSAssert(NO, #"ShareKit: There is no view controller to display from");
// If a view is already being shown, hide it, and then try again
if (currentView != nil)
{
self.pendingView = vc;
[[currentView parentViewController] dismissModalViewControllerAnimated:YES];
return;
}
// Wrap the view in a nav controller if not already
if (![vc respondsToSelector:#selector(pushViewController:animated:)])
{
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
if ([nav respondsToSelector:#selector(modalPresentationStyle)])
nav.modalPresentationStyle = [SHK modalPresentationStyle];
if ([nav respondsToSelector:#selector(modalTransitionStyle)])
nav.modalTransitionStyle = [SHK modalTransitionStyle];
nav.navigationBar.barStyle = nav.toolbar.barStyle = [SHK barStyle];
[topViewController presentModalViewController:nav animated:YES];
self.currentView = nav;
}
// Show the nav controller
else
{
if ([vc respondsToSelector:#selector(modalPresentationStyle)])
vc.modalPresentationStyle = [SHK modalPresentationStyle];
if ([vc respondsToSelector:#selector(modalTransitionStyle)])
vc.modalTransitionStyle = [SHK modalTransitionStyle];
[topViewController presentModalViewController:vc animated:YES];
[(UINavigationController *)vc navigationBar].barStyle =
[(UINavigationController *)vc toolbar].barStyle = [SHK barStyle];
self.currentView = vc;
}
self.pendingView = nil;
}
(void)hideCurrentViewController
{
[self hideCurrentViewControllerAnimated:YES];
}

This is a known error, which showed up on ios5. This has been fixed a long time ago in ShareKit 2.0. If you decide to upgrade, make sure to follow new install wiki very carefully and literally, since many things have changed comparing to the original sharekit
.

Related

How to clear Unsaved draft for contact in iOS?

Open the default contact form to add a number
Add a number and click on cancel to remove and open new modal for confirmation
Select discard changes modal close
Now when I open the contact book then first open the modal with the last number I added in the contact book form which added from my app
here is my code:
[self updateRecord:contact withData:contactData];
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
controller.delegate = self;
dispatch_async(dispatch_get_main_queue(), ^{
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:controller];
UIViewController *viewController = (UIViewController*)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (viewController.presentedViewController)
{
viewController = viewController.presentedViewController;
}
[viewController presentViewController:navigation animated:YES completion:nil];
self->updateContactPromise = resolve;
Below is delegate method code:
[viewController dismissViewControllerAnimated:YES completion:nil];
if(updateContactPromise) {
if (contact) {
NSDictionary *contactDict = [self contactToDictionary:contact withThumbnails:true];
updateContactPromise(contactDict);
} else {
updateContactPromise(nil);
}
updateContactPromise = nil;
}
}

How to identify the screen name or view controller in IOS app

I am automating IOS app test cases using Appium. I have a scenario in which I have to identify.. i am on which screen so that I can close the screen
In broader view , we have a inapp notification in our app which get open in random way.
So I can handle these condition if I would be able to identify the current screen.
I handle the same thing in Android using driver.currentActivity method
I need the some kind of same method for IOS apps
you get top most view controller by using below method
+ (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
Or if you have navigation or UIabbar controller then use below method to get visible view controller
- (void)applicationWillResignActive:(UIApplication *)application
{
UIViewController *vc = [self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)visibleViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil)
{
return rootViewController;
}
if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self visibleViewController:lastViewController];
}
if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]])
{
UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController;
UIViewController *selectedViewController = tabBarController.selectedViewController;
return [self visibleViewController:selectedViewController];
}
UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self visibleViewController:presentedViewController];
}

SWRevealViewController - RightViewController

I'm using SWRevealViewController for implementing two side navigation views in my application. I followed the story board method and successfully implemented the rear view and front view. I tried setting right view exactly like the rear view via storyboard, but couldn't do it.
I have set the reveal view controller segue to "sw_right" but it looks like it is not being recognized by - (void)prepareForSegue:(SWRevealViewControllerSegue *)segue sender:(id)sender which is called twice for "sw_rear" and "sw_front"
What Am I missing?
- (void)prepareForSegue:(SWRevealViewControllerSegue *)segue sender:(id)sender
{
// $ using a custom segue we can get access to the storyboard-loaded rear/front view controllers
// the trick is to define segues of type SWRevealViewControllerSegue on the storyboard
// connecting the SWRevealViewController to the desired front/rear controllers,
// and setting the identifiers to "sw_rear" and "sw_front"
// $ these segues are invoked manually in the loadView method if a storyboard
// was used to instantiate the SWRevealViewController
// $ none of this would be necessary if Apple exposed "relationship" segues for container view controllers.
NSString *identifier = segue.identifier;
if ( [segue isKindOfClass:[SWRevealViewControllerSegue class]] && sender == nil )
{
if ( [identifier isEqualToString:SWSegueRearIdentifier] )
{
segue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
{
[self _setRearViewController:dvc animated:NO];
};
}
else if ( [identifier isEqualToString:SWSegueFrontIdentifier] )
{
segue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
{
[self _setFrontViewController:dvc animated:NO];
};
}
//This is never executed even after setting the identifier
else if ( [identifier isEqualToString:SWSegueRightIdentifier] )
{
segue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
{
[self _setRightViewController:dvc animated:NO];
};
}
}
}
here is the sample project , this is working fine I worked out for your self, the download link is
https://www.sendspace.com/file/0l2ndd
after downloaded the project u want to delete the project , use this link
https://www.sendspace.com/delete/0l2ndd/1b1bd537ad852b2fdea9b9a0cce3390f
here u were need the right swipe on the front view controller , add the UIBarButtonItem in the particular view Controller
#property (strong, nonatomic) IBOutlet UIBarButtonItem *rightIcon; //this is for left bar button Item
#property (nonatomic) IBOutlet UIBarButtonItem* revealButtonItem; //this is for right bar button Item
and add the some functions is View DidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
//action for left Swipe
[self.revealButtonItem setTarget: self.revealViewController];
[self.revealButtonItem setAction: #selector( revealToggle: )];
[self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
//action for Right Swipe
[self.rightIcon setTarget: self.revealViewController];
[self.rightIcon setAction: #selector( rightRevealToggle: )];
[self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
}
Swift
override func viewDidLoad() {
super.viewDidLoad()
//action for left Swipe
self.revealButtonItem.target = self.revealViewController
self.revealButtonItem.action = "revealToggle:"
self.navigationController.navigationBar.addGestureRecognizer(self.revealViewController.panGestureRecognizer)
//action for Right Swipe
self.rightIcon.target = self.revealViewController
self.rightIcon.action = "rightRevealToggle:"
self.navigationController.navigationBar.addGestureRecognizer(self.revealViewController.panGestureRecognizer)
}
I figured out what the problem was. In loadStoryboardControllers method [self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil]; was commented out. If we implement right view controller this has to be uncommented.
You requested that I look at this thread.
I do not have experience with storyboards.
So, I am pasting below the code I have in my projects that allow for the Right Hand Side.
Take a look at the section I commented. Hopefully, this will set you on the right path.
Good luck
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
FrontViewController *frontViewController = [[FrontViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
// THIS SECTION RIGHT BELOW IS PROBABLY WHAT YOU WANT TO LOOK AT
RightViewController *rightViewController = rightViewController = [[RightViewController alloc] init];
rightViewController.view.backgroundColor = [UIColor greenColor];
revealController.rightViewController = rightViewController;
revealController.bounceBackOnOverdraw=NO;
revealController.stableDragOnOverdraw=YES;
self.viewController = revealController;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
There is a try-catch condition between line on 435 to 444 on SWRevealViewController.m. Just rearrange the statement as following
[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
[self performSegueWithIdentifier:SWSegueRearIdentifier sender:nil];
Now my right-menu/rightViewController is working even though i am not using the leftmenu/rearViewController.
you can also do what shad suggested. But if you are getting a flag with the right you can just replace
- (void)loadStoryboardControllers
if ( self.storyboard && _rearViewController == nil )
{
//Try each segue separately so it doesn't break prematurely if either Rear or Right views are not used.
#try
{
[self performSegueWithIdentifier:SWSegueRearIdentifier sender:nil];
}
#catch(NSException *exception) {}
#try
{
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
}
#catch(NSException *exception) {}
#try
{
[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
}
#catch(NSException *exception) {}
}
}
with
- (void)loadStoryboardControllers
{
//[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
[self performSegueWithIdentifier:SWSegueRearIdentifier sender:nil];
}
as you can see i commented out
//[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
becasue thats the one giving you an issue. If its the front or rear you can do the same

Get reference to top viewcontroller in iOS

How do I get reference to top visible view controller in my app. I saw some solutions which make use of navigationcontroller.[top|visible]viewcontroller. But I don't use navigation controllers in my app.
This seems like a pretty common use case, and I find it strange it is difficult to get access to top|visible view controller
This should also follow your modal views and navigation controllers (if any):
- (UIViewController *)deepestPresentedViewControllerOf:(UIViewController *)viewController
{
if (viewController.presentedViewController) {
return [self deepestPresentedViewControllerOf:viewController.presentedViewController];
} else {
return viewController;
}
}
- (UIViewController *)topViewController
{
UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
UIViewController *deepestPresentedViewController = [self deepestPresentedViewControllerOf:rootViewController];
if ([deepestPresentedViewController isKindOfClass:[UINavigationController class]]) {
return ((UINavigationController *)deepestPresentedViewController).topViewController;
} else {
return deepestPresentedViewController;
}
}
You should probably be using the delegate pattern here (giving the child view controller a reference to an object that it can call on). If you edit your post to explain why you think you need a reference to the top view controller, we can give you advice about how to use the delegate pattern in your situation.
But for now I'll just give you the rope you need to hang yourself:
UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
-(UIViewController *)getCurrentViewController
{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
result = nextResponder;
else
result = window.rootViewController;
return result;
}
-(UIViewController *) getTopMostController
{
UIWindow *topWindow = [UIApplication sharedApplication].keyWindow;
if (topWindow.windowLevel != UIWindowLevelNormal)
{
topWindow = [self returnWindowWithWindowLevelNormal];
}
UIViewController *topController = topWindow.rootViewController;
if(topController == nil)
{
topWindow = [UIApplication sharedApplication].delegate.window;
if (topWindow.windowLevel != UIWindowLevelNormal)
{
topWindow = [self returnWindowWithWindowLevelNormal];
}
topController = topWindow.rootViewController;
}
while(topController.presentedViewController)
{
topController = topController.presentedViewController;
}
if([topController isKindOfClass:[UINavigationController class]])
{
UINavigationController *nav = (UINavigationController*)topController;
topController = [nav.viewControllers lastObject];
while(topController.presentedViewController)
{
topController = topController.presentedViewController;
}
}
return topController;
}
-(UIWindow *) returnWindowWithWindowLevelNormal
{
NSArray *windows = [UIApplication sharedApplication].windows;
for(UIWindow *topWindow in windows)
{
if (topWindow.windowLevel == UIWindowLevelNormal)
return topWindow;
}
return [UIApplication sharedApplication].keyWindow;
}

Add close button to UIModalPresentationPageSheet corner

Is there a way to add a button at the corner of an UIModalPresentationPageSheet? I mean, I want to put that Apple-like (x) button at the corner of a Page Sheet, but adding it to the parent view makes it appear behind the Page Sheet (and also impossible to tap) and adding it to the Page Sheet will make part of it hidden, since it's out of the view area.
Is there a solution?
Thanks.
Here's a solution that I use. It's not quite what you describe, which would be neat too, but would be tricky since you'd want the button to be partially out of the view's bounds (as you say, it would have to be a child of the view-controller-view's superview).
My solution is to put a close button in the left-button area of a navigation bar. I do this automagically via a UIViewController class extension. To use it, just call [currentViewController presentAutoModalViewController: modalViewController animated: YES];
#implementation UIViewController (Modal)
- (void) presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction: (SEL) onDismiss animated:(BOOL)animated
{
UINavigationController* nc = nil;
if ( NO == [ modalViewController isKindOfClass: [UINavigationController class]] )
{
nc = [[[UINavigationController alloc] initWithRootViewController: modalViewController] autorelease];
[nc setToolbarHidden:YES animated: NO];
nc.modalPresentationStyle = modalViewController.modalPresentationStyle;
modalViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:onDismiss] autorelease];
}
else
{
nc = (UINavigationController*) modalViewController;
UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0];
rootViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:onDismiss] autorelease];
}
[nc setNavigationBarHidden: NO];
nc.navigationBar.barStyle = UIBarStyleBlack;
nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
[self presentModalViewController: nc animated: animated ];
}
- (void) presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
[self presentAutoModalViewController:modalViewController withDismissAction: #selector(autoModalViewControllerDismiss:) animated: animated];
}
- (void) autoModalViewControllerDismiss: (id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL) isAutoModalViewController
{
return ( self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.modalViewController == self.navigationController );
}
#end
EDIT: Actually, the first thing I would suggest you do is use a different kind of close button. You can, for example, add a toolbar at the top with a Done button.
If you still want the Apple-style floating X, then try setting properties to make sure it doesn't get hidden or clipped. I would say the better way to do this would be to create a UIButton with a foreground image which is the styled button image, and a background image which fades from the background colour/patterns of the page sheet to a transparent background around the button. It effectively gives you a "floating close button" without having to go outside the bounds of the page sheet.
I found #TomSwift's suggestion very helpful. Here is a version that uses the non-deprecated iOS7 methods and ARC.
#implementation UIViewController (Modal)
- (void)presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction:(SEL) onDismiss animated:(BOOL)animated
{
UINavigationController* nc = nil;
if ( NO == [ modalViewController isKindOfClass: [UINavigationController class]] )
{
nc = [[UINavigationController alloc] initWithRootViewController: modalViewController];
[nc setToolbarHidden:YES animated: NO];
nc.modalPresentationStyle = modalViewController.modalPresentationStyle;
modalViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:onDismiss];
}
else
{
nc = (UINavigationController*) modalViewController;
UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0];
rootViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:onDismiss];
}
[nc setNavigationBarHidden: NO];
nc.navigationBar.barStyle = UIBarStyleBlack;
nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
[self presentViewController:nc animated:animated completion:nil];
}
- (void)presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
[self presentAutoModalViewController:modalViewController withDismissAction: #selector(autoModalViewControllerDismiss:) animated: animated];
}
- (void)autoModalViewControllerDismiss: (id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)isAutoModalViewController
{
return ( self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.presentedViewController == self.navigationController );
}
#end
And I call it like...
MyController *vc = [[MyController alloc] init];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentAutoModalViewController:info animated:YES];

Resources