IOS ContactUI navigate back to app after edit - ios

In my Xamarin IOS app I open the ContactUI with the following code:
var contact = new AddressBookService().GetCNContactById(addressbookId);
var view = CNContactViewController.FromNewContact(contact);
view.Editing = true;
// Display the view
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
var navController = vc as UINavigationController;
if (navController != null)
{
vc = navController.ViewControllers.Last();
}
vc.PresentViewController(new UINavigationController(view), true, null);
I wrap it in a INavigationController because without it won't load. There is the following message printed to the output:
[CNUI ERROR] Contact view delayed appearance timed out
The Edit Dialog is displayed correctly. But after I click on Save I come to the details view:
As you can see is the second problem, that there is no back button. So the only way to come back to my app is to kill the app and start again.
Is there a way to navigate directly back to my app after I saved the contact? Or to wrap the ContactUI in my current ViewController so that the TabBar is still visible below?

Implement the CNContactViewControllerDelegate and use the DidComplete delegate of CNContactViewController. DidComplete will be fired when the "Done" button is clicked.
Set the delegate:
var view = CNContactViewController.FromNewContact(contact);
view.Delegate = new MyCNConatactViewControllerDelegate();
Implement the DidComplete, and dismiss the NavigationController which contains your CNContactViewController here:
public class MyCNConatactViewControllerDelegate : CNContactViewControllerDelegate
{
public override void DidComplete(CNContactViewController viewController, CNContact contact)
{
viewController.NavigationController.DismissViewController(true, null);
}
}

Related

Xamarin.IOS go back from CNContactViewController.FromContact to Application

I have an application written with Xamarin for IOS and Android where I have a list of contacts and when I click on one of them I want to open the according contact in the addressbook of the phone. I have an interface for that implement the method on IOS that way:
private void ShowContactDialog(string addressbookId, bool isEdit = false)
{
var contact = new AddressBookService().GetCnContactById(addressbookId);
var view = CNContactViewController.FromContact(contact);
view.Editing = isEdit;
DisplayView(view);
}
private static void DisplayView(CNContactViewController view)
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
if (vc is UINavigationController navController)
{
vc = navController.ViewControllers.Last();
}
vc.PresentViewController(new UINavigationController(view), true, null);
}
That works so far as it opens the contact:
The issue now is, that there is just an edit button, but now Done, cancel or back button. Therefore I have to kill the whole application and start it again to come back.
Is there a way to add a extra button similar to the edit button to dismiss the dialog again?
EDIT: I adjusted the code as suggested in the comments by Kevin Li.
private void ShowContactDialog(string addressbookId, bool isEdit = false)
{
var contact = new AddressBookService().GetCnContactById(addressbookId);
var view = CNContactViewController.FromContact(contact);
view.Editing = isEdit;
GetCurrentViewController().NavigationController.PushViewController(view, true);
}
private static UIViewController GetCurrentViewController()
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
if (vc is UINavigationController navController)
{
vc = navController.ViewControllers.Last();
}
return vc;
}
Now it is shown within the Tabs:
Unfortunately is the back button not displayed as well.
I tried to make a new UINavigationController, but that didn't change anything.
EDIT2:
I adjusted the way the tabs are created another time. My MainViewController looks like that:
[MvxRootPresentation(WrapInNavigationController = true)]
public class MainViewController : MvxTabBarViewController<MainViewModel>
{
private bool constructed;
private bool firstTimePresented = true;
public MainViewController()
{
constructed = true;
// need this additional call to ViewDidLoad because UIkit creates the view before the C# hierarchy has been constructed
ViewDidLoad();
}
public override async void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (firstTimePresented)
{
// Initialize the tabs
firstTimePresented = false;
await ViewModel.GoToContactListCommand.ExecuteAsync();
await ViewModel.GoToBirthdayListCommand.ExecuteAsync();
await ViewModel.GoToProfileListCommand.ExecuteAsync();
await ViewModel.GoToSettingsCommand.ExecuteAsync();
}
}
public override void ViewDidLoad()
{
if (!constructed)
return;
base.ViewDidLoad();
Title = "Connect Update";
EdgesForExtendedLayout = UIRectEdge.None;
View.BackgroundColor = UIColor.White;
}
}
On each View who shall be a tab has to be this Attribute:
[MvxTabPresentation(TabIconName = "IconCake", TabName = "Birthdays")]
public partial class BirthdayView : MvxViewController<BirthdayViewModel>
The code for opening the contact is the same as above. Now it makes a new View without the tabs but with the back button, exactly as wished :)
The issue now is, that there is just an edit button, but now Done, cancel or back button.
It is because the value of isEdit at view.Editing = isEdit; is false. So, when you present the CNContactViewController, there's only an Edit button. When you click the Edit button, it will change to the editing mode and the Cancel and Done buttons will appear.
Or you can set the value of isEdit to true, then the Cancel and Done buttons will show when the CNContactViewController presents.
Therefore I have to kill the whole application and start it again to come back.
Is there a way to add a extra button similar to the edit button to dismiss the dialog again?
You don't have to kill and restart the app to dismiss the dialog. You can just implement the DidComplete in the delegate: CNContactViewControllerDelegate to dismiss the dialog when user finishes editing the contact and click the Done button or cancel it with clicking the Cancel button.
Here's the related code snippet:
var view = CNContactViewController.FromContact(contact);
view.Delegate = new MyCNConatactViewControllerDelegate();
public class MyCNConatactViewControllerDelegate : CNContactViewControllerDelegate
{
public override void DidComplete(CNContactViewController viewController, Contacts.CNContact contact)
{
viewController.NavigationController.DismissViewController(true, null);
}
}
Update:
To show the Back button in the CNContactViewController:
Make sure the UI hierarchy like this: Navigation->Viewcontroller(Page)->CNContactViewController. Then change the method from Present to Push to show the CNContactViewController, like this:
this.NavigationController.PushViewController(view, true);

Push more than 3 ViewController in NavigationController from AppDelegate App crash

I am working on Push Notification. Now When the Application is raise to Device and When I Tap on it. I want to push 3 ViewController to Navigation Stack.
So I am using below code to do this.
AppDelegate.cs Code
Window = new UIWindow(UIScreen.MainScreen.Bounds);
Menu = new SlideoutNavigationController();
var storyboard = UIStoryboard.FromName("Main", null);
var webController = storyboard.InstantiateViewController("DashBoardViewController") as DashBoardViewController;
Menu.MainViewController = new MainNavigationController(webController, Menu);
Menu.MenuViewController = new MenuNavigationController(new DummyControllerLeft(), Menu) { NavigationBarHidden = false };
Window.RootViewController = Menu;
Window.MakeKeyAndVisible();
var storyboarddd = UIStoryboard.FromName("Main", null);
var webControllerdd = storyboarddd.InstantiateViewController("DashBoardViewController") as DashBoardViewController;
webControllerdd.reloadNotication();
UINavigationController nav = webController.NavigationController;
var notifyWebController = storyboard.InstantiateViewController("NotificationListViewController") as NotificationListViewController;
notifyWebController.navigationContoller = nav;
nav.PushViewController(notifyWebController, true);
if (type.Equals("Damage Report"))
{
var webController2 = storyboard.InstantiateViewController("DamageReportViewController") as DamageReportViewController;
webController2.DamageReportId = id;
webController2.navigationContoller = nav;
nav.PushViewController(webController2, true);
}
if (type.Equals("Overloss"))
{
var webController2 = storyboard.InstantiateViewController("OverlossViewController") as OverlossViewController;
webController2.PacketId = id;
webController2.navigationContoller = nav;
nav.PushViewController(webController2, true);
}
The upper code is working fine to open Specific ViewController.
But My App crash after it with the crash Log.
Crash Report :
2017-07-26 15:25:18.330 Aastha.iOS[6357:2021514] nested push animation can result in corrupted navigation bar
2017-07-26 15:25:18.740 Aastha.iOS[6357:2021514] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
My Solution :
When i search on google and SO someone says that Open ViewController one after another from each ViewController ViewDidAppear method but I am not sure If this is correct way.
Any Help be Appreciated.
You can't push more than one view controller with animation. If you want user to see transtions A -> B -> C you indeed have to do it in A's and B's viewDidAppear method.
If you want user to only see the last view controller C and be able to go back through A and B, you can push them all at once, but you need to do it without animation:
nav.PushViewController(webController2, false);
Another option is to use SetViewControllers like this:
nav.SetViewControllers([A, B, C], true);
this will push all 3 view controllers at the same time and only the topmost one will be animated using push transition. I don't think this fits your situation, just adding it here for completeness sake.

DialogViewController not showing navigationbar

I am instantiating a DialogViewController within a MvvmCross View like this
var myDialog = new MyDialogViewController();
var navController = new UINavigationController();
controller.NavigationBar.TintColor = UIColor.Black;
controller.PushViewController(myDialog, false);
Add(controller.View);
And this is how the MyDialogViewController looks like:
public class MyDialogViewController : DialogViewController
{
public MyDialogViewController()
: base(UITableViewStyle.Grouped, new RootElement("MyRoot"), true)
{
var root = new RootElement("MyRoot") {
new Section
{
new HtmlElement("MyWebsite", https://www.mywebsite.com")
}
};
Root.Add(root);
}
}
The dialog appears fine with the NavigationBar but if I select the Html element MyWebsite the Webview is displayed but without the NavigationBar and I am not able to navigate back.
The same thing occurs for elements that require to navigate to a new window, the navigationbar is not shown.
Any idea how to make the NavigationBar show after navigating to the WebView?
This worked for me:
var settings = new SettingsDialogViewController((SettingsViewModel)ViewModel, new RootElement("Settings"));
var window = UIApplication.SharedApplication.KeyWindow;
navigationController = window.RootViewController.ChildViewControllers[1].NavigationController;
navigationController.PushViewController(settings, true);
navigationController.NavigationBarHidden = false;

Monotouch back to first ViewController

I'm having a problem going back again to my first Apps VC, the first "screen/ViewController)" is a login screen, then I call a UITabbar with their respective ViewControllers, when I'm on a certain level of a ViewController (for example the 5th one) I want to get a "Logout" behavior in my App and get back to the first ViewController (Login). But I think I can only Navigate back to the first ViewController of my Tabbar control. I was trying with this methods of my VC:
this.NavigationController.PopToViewController(previousVC,true);
or
this.NavigationController.PopToRootViewController(true);
Any help would be appreciated.
(If you need more details with the code please tell me about it)
//Call to the tab bar from the login viewcontroller
mainTBC = new TabBarMenuPpal ();
this.NavigationController.PushViewController (mainTBC, true);
…
//Tabbarmenuppal with a set of navcontrollers
public TabBarMenuPpal ()
{
var customerVC = new Customers ();
navCustomers = new UINavigationController ();
navCustomers.PushViewController (customerVC, true);
navCustomers.TopViewController.Title = customersTitle;
navCustomers.TabBarItem = new UITabBarItem (customersTitle, UIImage.FromFile ("Utility/Images/Cust-30x30.png"), 0);
…
mainVC = new UIViewController[]
{
navCustomers
, navSettings
} ;
this.ViewControllers = mainVC;
}
The problem is that when I start to navigate in the navCustomers I can't find a way to go back to the login VC (deleting my tab bar and releasing all their resources). (Sorry about my English).
I don't know if it's the best solution but I have implemented a "GoToLogin" method in my UITabBarController which only does this:
public void GoToLogin()
{
this.NavigationController.PopToRootViewController(true);
}
So when I need to go to my first page I only do this:
if(MainDialog.NavigationController.TabBarController != null)//loaded from the tab bar
((TabBarMenuPpal)MainDialog.NavigationController.TabBarController).GoToLogin();
else//loaded from the login screen
MainDialog.NavigationController.PopToRootViewController(true);

MonoTouch for IPad: How to show another UIViewController in a UIPopoverController?

As title said, I want to show another UIViewController from an existing UIViewController which is hosted in UIPopoverController. I tried the following method:
_secondViewController = new SecondViewController();
this.ModalPresentationStyle = UIModelPresentationStyle.CurrentContext;
this.ModelInPopover = true;
this.PresentModelViewController(_secondViewController, true);
However, the secondViewController is shown in the main view controller, instead of the popover controller.
In this post somebody mentions that it cannot be done and it violates the HIG. However, I have seen this in other apps (e.g. Yahoo! Email) if I'm not mistaken.
I'm also thinking about another approach: If I could create a UINavigationController within the popover context, it might work by just adding new ViewController to the NavigationController. But how?
Remember that UINavigationController derives from UIViewController.
So, you can use the controller contained within UIPopover just like any other container... in this case it's best to use UINavigationController inside UIPopover to display ViewControllers.
Usage:
var _NavController = new NavController();
Popover = new UIPopoverController(_NavController);
Popover.PopoverContentSize = new SizeF(..., ...);
Popover.PresentFromRect(...);
NavController:
public class NavController : UINavigationController
{
UIViewController _FirstViewController;
UIViewController _SecondViewController;
public NavController()
: base()
{
}
public override void LoadView()
{
base.LoadView();
_FirstViewController = new UIViewController();
// Initialize your originating View Controller here.
// Only view related init goes here, do everything else in ViewDidLoad()
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// When a button inside the first ViewController is clicked
// The Second ViewController is shown in the stack.
_FirstViewController.NavButton.TouchUpInside += delegate {
PushSecondViewController();
};
this.PushViewController(_FirstViewController, true);
}
public void PushSecondViewController()
{
_SecondViewController = new UIViewController();
this.PushViewController(_SecondViewController, true);
}
}

Resources