如何从Xamarin Mobile应用程序向钱包打开pkpass文件

时间:2019-11-27 11:42:37

标签: xamarin xamarin.forms applepay wallet passkit

我已经从服务器端创建了pkpass文件,它的工作正常。我尝试通过服务使用pkpass,并尝试通过PKAddPassesViewController添加通行证。但这不起作用。我遵循了this的资源来添加通行证文件来添加钱包

  try
        {
            _oHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesstoken);

            //this response is pkpass file
            HttpResponseMessage response = await _oHttpClient.GetAsync(new Uri(Constants.Urls.DigitalMembershipCardApple + _dataHelper.GetPersonID()));

            byte[] filebytes = await response.Content.ReadAsByteArrayAsync();

            string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), Constants.CISIMembershipCardFields.FileDownloadName);

            string builtInPassPath = Path.Combine(System.Environment.CurrentDirectory, "cisi.pkpass");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
                File.WriteAllBytes(filePath, filebytes);
                //File.Copy(builtInPassPath, filePath);
            }
            else
            {
                File.Copy(builtInPassPath, filePath);
                File.WriteAllBytes(filePath, filebytes);
            }

            NSData nsdata;
            using (FileStream oStream = File.Open(filePath, FileMode.Open))
            {
                nsdata = NSData.FromStream(oStream);
            }

            NSError err = new NSError(new NSString("42"), -42);
            PKPass newPass = new PKPass(nsdata, out err);

            PKAddPassesViewController pkapvc = new PKAddPassesViewController(newPass);
            UINavigationController navigationController = new UINavigationController();

            navigationController.PresentModalViewController(pkapvc, true);             


        }
        catch (Exception ex)
        {
            Debug.WriteLine("exeption for pk pass");
            Debug.WriteLine(ex.StackTrace);
            throw ex;
        }

0 个答案:

没有答案