Nick
Nick

Reputation: 49

Launch Another IOS App from Xamarin Forms App

I am trying to test if an app is present from MyApp (CanOpen). If so, I wish to open the app, otherwise I have an https address to open a webview. I am getting the false return on the can open test. I believe my code is sound but I am not sure on the info.plist. I have a url type (in info.plist) for MyApp. I have the LSApplicationQueriesSchemes entry for the other app (health), but unsure of how that reference is tied back to the actual app.... Any help is greatly appreciated:

in MyApp PCL is the following Interface:

   public interface IAppHandler
   {
       Task<bool> LaunchApp(string uri);
   }

In the Model View where I making the deoendency call to the actual handler:

    string appid = @"health://";
    var result = await DependencyService.Get<IAppHandler>().LaunchApp(appid);

    if (!result)
    {
        Show_WebView(url);
    }

In the platform specific AppHandler:

public Task<bool> LaunchApp(string uri)
{
    try
    {
        var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(uri));
        if (!canOpen)
            return Task.FromResult(false);
        return Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(uri)));
    }
    catch (Exception ex)
    {
        return Task.FromResult(false);
    }
}

In the info.plist:

<key>CFBundleURLSchemes</key>
<array>
    <string>MyApp</string>
    <string>com.apple.Health</string>
</array>  
<key>LSApplicationQueriesSchemes</key>
 <array>
    <string>health</string>
 </array>

Upvotes: 4

Views: 2587

Answers (1)

Shabir jan
Shabir jan

Reputation: 2427

You need to add the URL Scheme of the target app into the info.plist file in order to use canOpen functionality. i.e enter image description here

Upvotes: 4

Related Questions