Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

DismissViewController with Xamarin.Forms

$
0
0

Does anyone know how to dismiss a UIViewController that has been added using a PageRenderer?

Here is my PageRenderer.

    using System;
    using Xamarin.Forms.Platform.iOS;
    using Xamarin.Auth;
    using Xamarin.Forms;
    using DriveStraightMobile;
    using DriveStraightMobile.iOS;

    [assembly: ExportRenderer (typeof(LoginPage), typeof(LoginPageRenderer))]

    namespace DriveStraightMobile.iOS {
        public class LoginPageRenderer : PageRenderer {
            public override void ViewDidAppear(bool animated) {
                base.ViewDidAppear (animated);

                PresentViewController (new LoginViewController (), true, null);
            }
        }
    }

I have hijacked the following UIViewController from the Facebook sdk example for ios.

public class LoginViewController : UIViewController {
        // For extensive list of available extended permissions refer to 
        // https://developers.facebook.com/docs/reference/api/permissions/
        private string [] ExtendedPermissions = new [] { "user_about_me", "read_stream"};

        FBLoginView loginView;
        FBProfilePictureView pictureView;
        IFBGraphUser user;
        UILabel nameLabel;

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            // Create the Facebook LogIn View with the needed Permissions
            // https://developers.facebook.com/ios/login-ui-control/
            loginView = new FBLoginView (ExtendedPermissions) {
                Frame = new RectangleF (85, 20, 151, 43)
            };

            // Hook up to FetchedUserInfo event, so you know when
            // you have the user information available
            loginView.FetchedUserInfo += (sender, e) => {
                user = e.User;
                // pictureView.ProfileID = user.GetId ();
                nameLabel.Text = user.GetName ();

                // DismissViewController(true, () => {
                    App.Instance.SuccessfulLoginAction.Invoke();
                // });
            };

            // Clean user Picture and label when Logged Out
            loginView.ShowingLoggedOutUser += (sender, e) => {
                pictureView.ProfileID = null;
                nameLabel.Text = string.Empty;
            };

            // Create view that will display user's profile picture
            // https://developers.facebook.com/ios/profilepicture-ui-control/
            pictureView = new FBProfilePictureView () {
                Frame = new RectangleF (40, 71, 240, 240)
            };

            // Create the label that will hold user's facebook name
            nameLabel = new UILabel (new RectangleF (20, 319, 280, 21)) {
                TextAlignment = UITextAlignment.Center,
                BackgroundColor = UIColor.Clear
            };

            // Add views to main view
            View.AddSubview (loginView);
            View.AddSubview (pictureView);
            View.AddSubview (nameLabel);
        }
    }

When "FetchedUserInfo" is called I make a call back to the shared library -> App.Instance.SuccessfulLoginAction.Invoke(). This call issues the NavigationPage.PopAsync() which should remove the view from the screen and display the previous view. This does not happen, however, if I inspect the NavigationPage variable I do see that the CurrentPage has changed back to my previous page.

It is almost as if I need to somehow manually unload the LoginViewController. I did try issuing DismissViewController inside the "FetchedUserInfo" but that resulted in a really nasty error ("attempt to dismiss from view controller while a presentation or dismiss is in progress!").

Any help will be greatly appreciated. I've been going in circles with auth for a week or more. Facebook did not like my usage of Xamarin.Auth because it internally uses a custom web view. In addition, Xamarin.Auth only worked in Android for my app anyway due to the issue I've documented at http://forums.xamarin.com/discussion/25183/xamarin-forms-xamarin-auth-and-facebook#latest.

Thanks ahead!


Viewing all articles
Browse latest Browse all 58056

Trending Articles