Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xamarin ios 10 open app store application programmatically in my app

i'm trying to open app store application programmatically in my app.

what i'm trying to do is that i'm calling a service to check at the current app version and if it needs update i should open app store application to let the user update the my app.

note: the app not published yet to the store, i'm still in coding phase.

i tried to use the following code in ViewDidLoad method, but it's not working (nothing happened):

var nsurl = new NSUrl("itms://itunes.apple.com");
UIApplication.SharedApplication.OpenUrl(nsurl);
like image 495
Ahmad Aqrabawi Avatar asked Jan 05 '23 01:01

Ahmad Aqrabawi


2 Answers

A direct link via itms: will only work in an actual device, if you are testing on a simulator, use https://itunes.apple.com/us/genre/ios/id36?mt=8 instead.

I would recommend using itms:// link on the actual device as it prevents the redirects that user sees when using a https:// link to open iTunes.

bool isSimulator = Runtime.Arch == Arch.SIMULATOR;
NSUrl itunesLink;
if (isSimulator)
{
    itunesLink = new NSUrl("https://itunes.apple.com/us/genre/ios/id36?mt=8");
}
else
{
    itunesLink = new NSUrl("itms://itunes.apple.com");
}
UIApplication.SharedApplication.OpenUrl(itunesLink, new NSDictionary() { }, null);

Instead of opening the external Store app on the device, you might want to consider keeping the user inside of your app by using a SKStoreProductViewController:

bool isSimulator = Runtime.Arch == Arch.SIMULATOR;
if (!isSimulator)
{
    var storeViewController = new SKStoreProductViewController();
    storeViewController.Delegate = this;
    var id = SKStoreProductParameterKey.ITunesItemIdentifier;
    var productDictionaryKeys = new NSDictionary("SKStoreProductParameterITunesItemIdentifier", 123456789);
    var parameters = new StoreProductParameters(productDictionaryKeys);
    storeViewController.LoadProduct(parameters, (bool loaded, NSError error) =>
    {
        if ((error == null) && loaded)
        {
            this.PresentViewController(storeViewController, true, () =>
            {
                Console.WriteLine("SKStoreProductViewController Completed");
            });
        }
        if (error != null)
        {
            throw new NSErrorException(error);
        }
    });
}
else
{
    var itunesLink = new NSUrl("https://itunes.apple.com/us/genre/ios/id36?mt=8");
    UIApplication.SharedApplication.OpenUrl(itunesLink, new NSDictionary() { }, null);
}
like image 195
SushiHangover Avatar answered Feb 22 '23 22:02

SushiHangover


NSBundle.MainBundle.InfoDictionary["CFBundleVersion"]

Returns you the current app version.

To open the Apple Appstore just let the user navigate to the appstore link, Apple will automaticly detect that the user is using an iPhone and will open the Appstore for them.

Test yourself:

Open the following link in safari: Whatsapp in the Appstore

It will automatically open the appstore.

like image 34
Justinfailber Avatar answered Feb 22 '23 22:02

Justinfailber