【问题标题】:OperationCanceledException from http request when Xamarin.iOS app asking permissionsXamarin.iOS 应用请求权限时来自 http 请求的 OperationCanceledException
【发布时间】:2021-01-05 10:17:26
【问题描述】:

我在使用 Xamarin.iOS 应用时遇到问题。在启动时,我向服务器发送请求以进行授权。同时,应用程序会询问用户发送通知的权限。看起来应用程序进入前台并使用 OperationCanceledException 中断连接。由于这个问题,我无法验证用户。对于解决方法,我在捕获 OperationCanceledException 时提出了第二个请求。因此,有时用户会被授权两次。在这种情况下如何发送请求而不取消?

    private async Task<string> PostAndHandleHttpRequestAsync(Dictionary<string, string> content)
    {
        var cancellationTokenSource = new CancellationTokenSource();
        cancellationTokenSource.CancelAfter(15000); 
        CancellationToken token = cancellationTokenSource.Token;

        var newClient = new HttpClient();
        var contentSerialize = JsonConvert.SerializeObject(content);
        try
        {
            var postAsyncResult = await newClient.PostAsync(GlobalSettings.urlToPHP, new StringContent(contentSerialize), token);
            var responseBody = await postAsyncResult.Content.ReadAsStringAsync();
            return responseBody;
        }
        catch (OperationCanceledException c)
        {
            // onetime retry
            if (cancellationCounter == 0)
            {
                //Console.WriteLine("second");
                cancellationCounter = 1;
                var postAsyncResult = await newClient.PostAsync(GlobalSettings.urlToPHP, new StringContent(contentSerialize), token);
                var responseBody = await postAsyncResult.Content.ReadAsStringAsync();
                return responseBody;
            }
            else
            {
                cancellationCounter = 0;
                return "Cancel";
            }
            return "Cancel";
        }
        catch (Exception e)
        {
            Console.WriteLine("Error PostAndHandleHttpRequestAsync " + e.Message);
            return "Error";
        }
    }

【问题讨论】:

    标签: ios xamarin push-notification httprequest httpclient


    【解决方案1】:

    当App从后台进入前台时,您可以再次发布请求。

    在 AppDelegate.cs 中

    public override void WillEnterForeground(UIApplication application)
            {
                base.WillEnterForeground(application);
    
                if(!UIDevice.CurrentDevice.CheckSystemVersion(13,0))
                {
                    NSNotificationCenter.DefaultCenter.PostNotificationName("willActive", null);
                }          
            }
    

    在场景委托中

    [Export ("sceneWillEnterForeground:")]
    public void WillEnterForeground (UIScene scene)
    {
        NSNotificationCenter.DefaultCenter.PostNotificationName("willActive",null);
    
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }
    

    在您的视图控制器中

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
    
            NSNotificationCenter.DefaultCenter.AddObserver(new NSString("willActive"),(notification)=> {
            
               if (cancellationCounter == 1)
                {
                    //request again
                }
            
            });
    
        }
    
    
        public override void ViewWillDisappear(bool animated)
        {
            base.ViewWillDisappear(animated);
    
            NSNotificationCenter.DefaultCenter.RemoveObserver(this, "willActive");
    
        }
    
    catch (OperationCanceledException c)
    {
        cancellationCounter =1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多