Open Native iOS app from Xamarin.Forms - xamarin

I have an native iOS sample app installed in my iPad and also my sample xamarin.Forms App installed. I want to open native iOS app from Xamarin.forms when clicked on button. I have set URL schema in my native iOS app in info.plist.
How do I open native app from xamarin.forms on click of a button. I already tried
var request = Device.OnPlatform(
string.Format("native app url"),
string.Format("native app url"),
string.Format("native app url"));
Device.OpenUri(new Uri(request));
but its not working.

It's basically how you are doing it.
var url = Device.OnPlatform(iOSUrl, androidUrl, winPhoneUrl);
Device.OpenUri(new Uri(request));
Be aware that each platform have some requirements in the way to url is configured and parameters are sent.
Let say to get the maps app open in each platform you would use these urls schemes:
// iOS doesn't like %s or spaces in their URLs, so manually replace spaces with +s
var iOSUrl = string.Format("http://maps.apple.com/maps?q={0}&sll={1}", name.Replace(' ', '+'), loc);
var androidUrl = string.Format("geo:0,0?q={0}({1})", string.IsNullOrWhiteSpace(addr) ? loc : addr, name);
var winPhoneUrl = $"bingmaps:?cp={loc}&q={name}";
var url = Device.OnPlatform(iOSUrl, androidUrl, winPhoneUrl);
Also you cannot open any arbitrary app unless you know its Url scheme.
Anyway, if the Device.OpenUri() doesn't fulfill your requirements you are good to create your own implementations using the native APIs.

Please go through the below link and attached photo
Link:-launch-an-app-from-within-another-iphone
I am using below code in xamarin forms
NSUrl request = new NSUrl("Camera://");
var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(request.ToString()));
if (!canOpen)
{ Task.FromResult(false); }
else
{
Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(request.ToString())));
}

Related

Passing values from native project to shared project Xamarin

Im currently facing an issue with using a 3rd party authenticator for my xamarin forms app. The code to execute has to be done natively in the platform project. The issue im facing is how to get the information(successful login, token, email etc) from the ios/android native project to the shared project for me to access it and continue the application. Below is an example of how the code looks below in the ios native project.
var client = new Auth0Client(new Auth0ClientOptions
{
Domain = "example.us.auth0.com",
ClientId = "123456789"
});
var loginResult = await client.LoginAsync();

Implementing a ViewController for Android for a shared application.

Im trying to do this for a shared application in Xamarin. In the iOS section is looks like:
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
var user = await auth0.LoginAsync(vc, withRefreshToken: true);
but for android I have no idea how to implement the view controller 'vc' to pass to LoginAsync().
You can always refer to https://auth0.com/docs/quickstart/native/xamarin. In Android referring this will give the current context you are in. this context is very much similar to window.RootViewController for iOS. So all you need to do is calling
var user = auth0.LoginAsync(this, withRefreshToken: true);
If you want to show the login in other window for android, get the context and assign it to a Android.App.Application.Context type variable and replace this with the variable in the previously mentioned code.

Get mac address and ip in Nativescript

We are implementing a server for app distribution and we need restrict the access to the apps by:
mac address
ip
At the moment I have not found any module that can obtain this data from the device in nativescript, so i don't know if there's a plugin or how else can I achieve this.
In nativescript you can access native apis of device
so if there isn't any module/plugin for it you can use this option for accessing native apis.
https://docs.nativescript.org/core-concepts/accessing-native-apis-with-javascript
for example there is solution for mac adress here
in JAVA:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String mac = wInfo.getMacAddress();
we can write it in javascript like this:
first we should fine where is this getSystemService:
after searching in documentation of android we found:
getSystemService is in android.content.Context
for accessing context in nativescript http://docs.nativescript.org/cookbook/application
we can do:
import app = require("application");
app.android.context;
so let's write it in javascript:
we don't have types in javascript so we use var instead;
var context = android.content.Context;
var wifiManager = app.android.context.getSystemService(context.WIFI_SERVICE);
var wInfo = wifiManager.getConnectionInfo();
var mac = wInfo.getMacAddress();
NOTE1 : as mentioned in above java solution link you should add this permision <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> to app/App_Resources/AndroidManifest
NOTE2 : the above solution was for android,for ios you should find the solution with objective_c and convert to javascript with help of nativescript documentation.
NOTE3:In android 6 you might need request permision
You can also use this method to create a plugin for nativescript.

How to open app from other app within app in iOS

How to move from one app to another app in iOS.
How to go from one to another app if app exist in device at that moment.
let url = NSURL(string: "NameOfTheAppAsOnAppStrore:")
// canOpenURL will return a bool value checking the presence of app in your device and if app is present, it will open the same.
if UIApplication.sharedApplication().canOpenURL(url!)
UIApplication.sharedApplication().openURL(url!)
}

Xamarin.forms OnAppLinkRequestReceived

I'm trying to use https://developer.xamarin.com/api/member/Xamarin.Forms.Application.OnAppLinkRequestReceived/p/System.Uri/ but can't seem to get it to be called in any way.
Did anyone ever tried to implement this new method?
I've tried several ways to implement different deeplinks, all of them open the app fine so they're correctly configured but that method never gets called.
Thanks.
This is how I set it up in Android.
Put this above your MainActivity.cs
[IntentFilter(new[] { Android.Content.Intent.ActionView },
Categories = new[]
{
Android.Content.Intent.CategoryDefault,
Android.Content.Intent.CategoryBrowsable
},
DataScheme = "http",
DataPathPrefix = "/tesla/",
DataHost = "exrin.net")]
This registers the activity when the app is installed. Change the URL to your desired URL.
For Android only (no need to do this with iOS) you need to also install the Nuget Xamarin Forms AppLinks
In your OnCreate make sure you do this after your Xamarin Forms Init
AndroidAppLinks.Init(this);
Then when you load the URL in a browser (in my example http://exrin.net/tesla) you will get this:
Then if you open the app it will enter here with the full URL as the URI parameter. This is in the App.cs (Xamarin.Forms.Application)
protected override void OnAppLinkRequestReceived(Uri uri)
{
base.OnAppLinkRequestReceived(uri);
}
You can then decode the URI as you see fit to move to the specific page in your app that the URL relates to/
More details at Xamarin Forms AppLinks
Also, your MainActivity must be derived from FormsAppCompatActivity for it to work. Once I changed it to that from FormsApplicationActivity, it started working.

Resources