示例#1
0
        async Task RequestStorefrontCountryCodeAsync()
        {
            string countryCode;

            if (SKCloudServiceController.AuthorizationStatus == SKCloudServiceAuthorizationStatus.Authorized)
            {
                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                {
                    /*
                     * On iOS 11.0 or later, if the
                     * `SKCloudServiceController.authorizationStatus()`
                     * is `.authorized` then you can request the
                     * storefront country code.
                     */
                    countryCode = (await CloudServiceController.RequestStorefrontCountryCodeAsync()).ToString();
                }
                else
                {
                    countryCode = await AppleMusicManager.PerformAppleMusicGetUserStorefrontAsync(UserToken);
                }
            }
            else
            {
                countryCode = await DetermineRegion();
            }

            if (string.IsNullOrWhiteSpace(countryCode))
            {
                throw new ArgumentNullException(nameof(countryCode), "Unexpected value from SKCloudServiceController for storefront country code.");
            }

            CloudServiceStorefrontCountryCode = countryCode;

            InvokeOnMainThread(() => NSNotificationCenter.DefaultCenter.PostNotificationName(CloudServiceDidUpdateNotification, null));
        }
示例#2
0
        public AuthorizationManager(AppleMusicManager appleMusicManager)
        {
            AppleMusicManager        = appleMusicManager;
            CloudServiceController   = new SKCloudServiceController();
            CloudServiceCapabilities = new SKCloudServiceCapability();

            /*
             * It is important that your application listens to the
             * `CloudServiceCapabilitiesDidChangeNotification` and
             * `StorefrontCountryCodeDidChangeNotification` notifications
             * so that your application can update its state and
             * functionality when these values change if needed.
             */
            var notificationCenter = NSNotificationCenter.DefaultCenter;

            CloudServiceCapabilitiesDidChangeNotificationToken = notificationCenter.AddObserver(SKCloudServiceController.CloudServiceCapabilitiesDidChangeNotification,
                                                                                                async(obj) => await RequestCloudServiceCapabilitiesAsync());

            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                StorefrontCountryCodeDidChangeNotificationToken = notificationCenter.AddObserver(SKCloudServiceController.StorefrontCountryCodeDidChangeNotification,
                                                                                                 async(obj) => await RequestStorefrontCountryCodeAsync());
            }

            /*
             * If the application has already been authorized in a
             * previous run or manually by the user then it can
             * request the current set of `SKCloudServiceCapability`
             * and Storefront Identifier.
             */

            if (SKCloudServiceController.AuthorizationStatus == SKCloudServiceAuthorizationStatus.Authorized)
            {
                Task.Factory.StartNew(async() => {
                    await RequestCloudServiceCapabilitiesAsync();

                    // Retrieve the Music User Token for use in the application
                    // if it was stored from a previous run.
                    if (NSUserDefaults.StandardUserDefaults.StringForKey(UserTokenUserDefaultsKey) is string token)
                    {
                        UserToken = token;
                        await RequestStorefrontCountryCodeAsync();
                    }
                    else                       // The token was not stored previously then request one.
                    {
                        await RequestUserTokenAsync();
                    }
                });
            }
        }
示例#3
0
        async Task <string> DetermineRegion()
        {
            /*
             * On other versions of iOS or when
             * `SKCloudServiceController.AuthorizationStatus` is not
             * `SKCloudServiceAuthorizationStatus.Authorized`, your
             * application should use a combination of the device's
             * `Locale.current.regionCode` and the Apple Music API
             * to make an approximation of the storefront to use.
             */
            var currentRegionCode = NSLocale.CurrentLocale.CountryCode.ToLower();

            return(await AppleMusicManager.PerformAppleMusicStorefrontsLookupAsync(currentRegionCode));
        }
示例#4
0
        async Task RequestUserTokenAsync()
        {
            var developerToken = AppleMusicManager.FetchDeveloperToken();

            if (developerToken == null)
            {
                throw new ArgumentNullException(nameof(developerToken), "Developer Token not configured. See README for more details.");
            }

            if (SKCloudServiceController.AuthorizationStatus != SKCloudServiceAuthorizationStatus.Authorized)
            {
                return;
            }

            string token;

            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                token = await CloudServiceController.RequestUserTokenAsync(developerToken);
            }
            else
            {
                token = await CloudServiceController.RequestPersonalizationTokenAsync(developerToken);
            }

            if (string.IsNullOrWhiteSpace(token))
            {
                Console.WriteLine("Unexpected value from SKCloudServiceController for user token.");
                return;
            }

            UserToken = token;

            // Store the Music User Token for future use in your application.
            var userDefaults = NSUserDefaults.StandardUserDefaults;

            userDefaults.SetString(token, UserTokenUserDefaultsKey);
            userDefaults.Synchronize();

            if (string.IsNullOrWhiteSpace(CloudServiceStorefrontCountryCode))
            {
                await RequestStorefrontCountryCodeAsync();
            }

            InvokeOnMainThread(() => NSNotificationCenter.DefaultCenter.PostNotificationName(CloudServiceDidUpdateNotification, null));
        }