Xamarin.Mac AppDelegate.OpenUrls() Not Invoked

AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

I'm using Clancey.SimpleAuth to log into Google's cloud and Google's API issues a RedirectUri back to the client to provide the Access Token. What I've implemented works on 10.13 and 10.14, but not 10.11 nor 10.12. The AppDelegate.OpenUrls() is not getting invoked on those 2 older OSs. Instead, I get a popup saying...

"There is no application set to open the URL..."

Anybody know what I need to do to get this working?

Thanks

Tagged:

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Did you do this?

    You must declare the URL types that your app supports in your Info.plist file using the CFBundleURLTypes key.

  • AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

    Yes Chris, I've done that. That's why it works on 10.13 and 10.14.

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.googleusercontent.apps.357036961144-65oqh7kgrf8khb9drkgl7cje2lllldju</string>
            </array>
            <key>CFBundleURLTypes</key>
            <string>Viewer</string>
        </dict>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>https://localhost.hp.com</string>
            </array>
            <key>CFBundleURLTypes</key>
            <string>Viewer</string>
        </dict>
    </array>
    
  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Hmm, sounds like an Apple bug then, I don't see how our bits could introduce such strange behavior.

    Consider writing a small sample in ObjC and/or filing a radar w\ Apple.

  • AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

    Ok Chris. But, I would think that the Xamarin layer would be integral in propagating a RedirectUrl to the app. In any case, while I'm investigating the ObjC angle, I've attached the SimpleAuth sample which clearly demonstrates the problem, in case you have time to look at it from your end. Just...

    1. Under the "samples" folder, open the SimpleAuth.Samples.sln in Visual Studio.
    2. Restore NuGets.
    3. Select the Sample.Mac project under the "Samples" solution folder as the Startup Project.
    4. Build, run, and hit the "Login" button.
    5. Sign into any google account and observe.

    In the AppDelegate, I've added this override.

        public override void OpenUrls(NSApplication application, NSUrl[] urls)
        {
            //base.OpenUrls(application, urls);
            var alert = new NSAlert();
            alert.MessageText = "AppDelegate.OpenUrls()";
            alert.InformativeText = urls[0].AbsoluteString;
            alert.AddButton("Ok");
            alert.RunModal();
        }
    

    On 10.13 and 10.14, you'll see this alert. But not on 10.11 nor 10.12.

  • AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

    Thanks Chris.

    For 10.11 and 10.12, I had to use this mechanism.

        public AppDelegate()
        {
            // https://forums.xamarin.com/discussion/118220/app-launch-via-custom-url-scheme
            var appleEventManager = NSAppleEventManager.SharedAppleEventManager;
            appleEventManager.SetEventHandler(this, new ObjCRuntime.Selector("handleGetURLEvent:withReplyEvent:"), AEEventClass.Internet, AEEventID.GetUrl);
        }
    
        [Export("handleGetURLEvent:withReplyEvent:")]
        private void HandleGetURLEvent(NSAppleEventDescriptor evt, NSAppleEventDescriptor replyEvent)
        {
            // Received event is a list (1-based) of URL strings
            for (int i = 1; i <= evt.NumberOfItems; i++)
            {
                var innerDesc = evt.DescriptorAtIndex(i);
    
                if (!string.IsNullOrEmpty(innerDesc.StringValue))
                {
                    Console.WriteLine("Get URL: " + innerDesc.StringValue);
                }
            }
        }
    
Sign In or Register to comment.