Tulasi
Tulasi

Reputation: 75

Xamarin IOS custom URL Scheme not working in iOS 14

I created a Xamarin forms application and configured custom URL Scheme for iOS but it didn't fire in iOS 14.

So I created a sample native iOS project and test custom URL Scheme and it is working. This is the sample iOS swift code.

 import SwiftUI

 @main
 struct TestApp: App {
 //@UIApplicationDelegateAdaptor var delegate: AppDelegate
 var body: some Scene {
    WindowGroup {
        ContentView().onOpenURL(perform: handleURL)
    }
 }

func handleURL(_ url: URL) {
    print("source application")
    print(url)
}

}

But how can I convert this same to Xamarin. Because how to call onOpenURL in Xamarin iOS?

Upvotes: 2

Views: 1333

Answers (1)

Adrain
Adrain

Reputation: 1934

Add info about your custom scheme in your info.plist. You can do this in your iOS Project Options:

Double-click your iOS project >> Build > iOS Application >> Advanced (tab)

At the bottom, you'll see a section URL Types. Add your info about your custom scheme there. For example:

In the "Identifier" field, enter "com.myapp.urlscheme". In the "URL Scheme" field, enter "myapp".

Next you need to override in your AppDelegate file:

public override bool OpenUrl (UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
// custom stuff here using different properties of the url passed in
return true;
}

Go ahead and set a breakpoint on "return true" in the override above

Now if you build and run on the simulator, then enter "myapp://com.myapp.urlscheme" into the url bar in Safari (on the simulator), it should launch your app, hit your breakpoint, and the "url" param will be "myapp://com.myapp.urlscheme".

Upvotes: 2

Related Questions