App linking example

Sometimes we might need to write a renderer in order to use features that are not available in the Xamarin.Forms framework. One example of that is App Links.

App Links is a way to link our app with other apps or to handle our app's incoming links. Let's develop a solution that allows us to insert a phone number inside our app and make a call with the Skype app when installed in the user device.

In our CustomRenderers core project, we can create a new Content page that contains an entry box to insert the phone number and a button that will redirect us to the Skype app.

We will also create an event that will transform the click on the button as a Call event, passing the phone number to call:

public class SkypeCallPage : ContentPage 
{ 
    public event CallHandler Call; 
 
    public delegate void CallHandler (string number); 
 
    public void OnCall (string number) 
    { 
        if (Call != null) { 
            Call (number); 
        } 
    } 
 
    public SkypeCallPage () 
    { 
        Entry phoneNumber = new Entry () {  
            HorizontalOptions = LayoutOptions.FillAndExpand, 
            Placeholder = "insert a phone number"  
        }; 
        Button call = new Button () {  
            HorizontalOptions = LayoutOptions.FillAndExpand, 
            Text = "Call" 
        }; 
        call.Clicked += (object sender, EventArgs e) =>  
            OnCall(phoneNumber.Text); 
 
        Content = new StackLayout {  
            Orientation = StackOrientation.Vertical, 
            VerticalOptions = LayoutOptions.Center, 
            HorizontalOptions = LayoutOptions.Center, 
            Children = { 
                phoneNumber, call 
            } 
        }; 
    } 
 
} 

We now need to implement the renderer of the SkypeCallPage in the platform-specific projects. We will follow the Skype documentation in order to make the link between our app and the Skype app.

We will start with the iOS version of the renderer. The documentation on how to integrate an iOS app with Skype can be found at https://msdn.microsoft.com/EN-US/library/office/dn745885.aspx.

We need to subscribe the Call event from the SkypeCallPage that we will find in the Element property of the PageRenderer.

In the override of the OnElementChanged method, we have to include the call to the Skype app passing in the URL the number that we want to call:

public class SkypeCallPageRenderer : PageRenderer 
{ 
    protected override void OnElementChanged (VisualElementChangedEventArgs e) 
    { 
        base.OnElementChanged (e); 
        SkypeCallPage page = (Element as SkypeCallPage); 
        page.Call += Page_Call; 
    } 
 
    void Page_Call (string number) 
    { 
        Foundation.NSUrl url = new Foundation.NSUrl ("skype:"
        + number); 
        bool installed = UIApplication.SharedApplication
        .CanOpenUrl (url); 
        if (installed) { 
            UIApplication.SharedApplication.OpenUrl (url); 
        } else { 
            UIApplication.SharedApplication.OpenUrl ( 
                new Foundation.NSUrl
                ("http://itunes.com/apps/skype/skype")); 
        } 
    } 
} 

We always need to add the attribute, ExportRenderer, in order to bind the platform-specific behavior with the core library:

[assembly: ExportRenderer (typeof(SkypeCallPage), typeof(SkypeCallPageRenderer))]

In Android, we need to follow the documentation available at https://msdn.microsoft.com/EN-US/library/office/dn745884.aspx.

The component package name we need to use is com.skype.raider and the main activity we need to run is called com.skype.raider.Main:

public class SkypeCallPageRenderer : PageRenderer 
{ 
    string packageName = "com.skype.raider"; 
    string componentMainActivity = "com.skype.raider.Main"; 
    ... 
} 

As per iOS, we need to override the OnElementChanged method of PageRenderer and subscribe the event:

protected override void OnElementChanged (ElementChangedEventArgs<Page> e) 
{ 
    base.OnElementChanged (e); 
    SkypeCallPage skypeCallPage = (Element as SkypeCallPage); 
    skypeCallPage.Call += SkypeCallPage_Call; 
} 

We can handle the event first by checking if the Skype app is installed in the mobile phone of the user, and moving them to the market in the case that the app has not been installed.

We then need to build the Skype URL composed by the string skype:, followed by the number we want to call.

After having done that, we just need to build the intent with the Skype URI and set the component with the Skype app package name and component class name:

void SkypeCallPage_Call (string number) 
{ 
     
    // Make sure the Skype for Android client is installed. 
    if (!IsSkypeClientInstalled (Context)) { 
        GoToMarket (Context); 
        return; 
    } 
    string mySkypeUri = "skype:" + number; 
 
    // Create the Intent from our Skype URI. 
    Uri skypeUri = Uri.Parse (mySkypeUri); 
    Intent myIntent = new Intent (Intent.ActionView, skypeUri); 
 
    // Restrict the Intent to being handled by the Skype
    for Android client only. 
    myIntent.SetComponent (new ComponentName (packageName,
    componentMainActivity)); 
    myIntent.SetFlags (ActivityFlags.NewTask); 
 
    // Initiate the Intent. It should never fail because
    you've already established the 
    // presence of its handler (although there is an extremely
    minute window where that 
    // handler can go away). 
    Context.StartActivity (myIntent); 
 
} 

In order to determine if the Skype client is installed, we can use the PackageManager.GetPackageInfo method:

public bool IsSkypeClientInstalled (Context myContext) 
{ 
    PackageManager myPackageMgr = myContext.PackageManager; 
    try { 
        myPackageMgr.GetPackageInfo (packageName,
        PackageInfoFlags.Activities); 
    } catch (PackageManager.NameNotFoundException) { 
        return (false); 
    } 
    return (true); 
} 

If the Skype client is not installed; our app will direct our user to the Android Market (Google Play Store). To do that, our app can use a market: scheme Intent to navigate directly to the Skype for Android install page:

public void GoToMarket (Context myContext) 
{ 
    Uri marketUri =  
      Uri.Parse ("market://details?id=com.skype.raider"); 
 
    Intent myIntent =  
               new Intent (Intent.ActionView, marketUri); 
 
    myIntent.SetFlags (ActivityFlags.NewTask); 
    myContext.StartActivity (myIntent); 
    return; 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.187.106