Set BaseURL in xamarin forms

User_101User_101 Member ✭✭
edited September 3 in Xamarin.Forms

I am creating a simple xamarin form page. I want to set a base url for the app. This is what I have done so far, further help needed.

  1. Form page is complete and is working fine.
  2. Created a interface file under project with following code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace TestingLogin
    {
    public interface IBaseUrl
    {
    string Get();
    }
    }

Created a class file under Android->Assets. Code follows:

using TestingLogin.Droid.Assets;
using Xamarin.Forms;


[assembly: Xamarin.Forms.Dependency(typeof(Android_Baseurl))]
namespace TestingLogin.Droid.Assets
{
   public class Android_Baseurl : IBaseUrl
 {
    public string Get()
    {
        return "https://www.urlhere";    <---URL HERE
    }
 }

Now, how do I link these?

Answers

  • GraverobberGraverobber Member ✭✭✭

    Hi,

    I don't fully understand, do you have different baseUrls for Android and iOS?
    In that case in order to do it the way you want to do it you need a dependency service
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

    If you have the same Base URL for Android and iOS, why not just setting it up in the Forms project only?

  • User_101User_101 Member ✭✭

    I guess my que is not clear. I am creating a login form and need to set base url for the same.
    What I understood is we need to use MVVM concept to set base url, but not knowing how to do it. Kindly help

  • ColeXColeX Member, Xamarin Team Xamurai
    edited September 4

    how do I link these?

    You mean get url by dependency service ?

    IBaseUrl service = DependencyService.Get<IBaseUrl>();
    string  url = service.Get();
    
Sign In or Register to comment.