How to handle OAuth URL callbacks with Intent filters if authentication is done with webview?

后端 未结 3 990
情深已故
情深已故 2021-02-02 14:35

I am developing an app which uses OAuth for authentication but I have a little problem handling OAuth callbacks.

THE AUTHENTICATION

My app has a

3条回答
  •  青春惊慌失措
    2021-02-02 14:53

    As you have the Object of WebView so you can overload it and then handle the url on which you wana navigate to your Activity,in these two way.

    private  class MyWebView extends WebViewClient{   
                @TargetApi(Build.VERSION_CODES.N)
                @Override
                public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
                {
                    Uri uri = request.getUrl();
                    return shouldOverrideUrlLoading(uri.toString());
                }
    
                private boolean shouldOverrideUrlLoading(final String url)
                {
                    Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);
                       //your code to handle the url for navigation will go here
                    try{
                          if(url.contains("yourcondition")){
                            //1st way to handle 
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            intent.setData(Uri.parse(url));
                            YourActivity.this.startActivity(intent);
                           }else{
                               //2nd way 
                            Intent intent = new Intent(YourActivity.this,NewActivity.class);
                            //you can set the data 
                            intent.setData(Uri.parse(url));
                            YourActivity.this.startActivity(intent);
                           }
                         }catch(ActivityNotFoundException e) {    
                           Log.e(LOGTAG,"Could not load url"+url);
                        }                    
                   return false;
                   // return true; // this True means that application wants to leave the WebView and try to go to browser and handle the url itself, otherwise return false.
                 } 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
    
    
            try {
                  if(url.contains("yourcondition")){
                    //1st way to handle 
                  Intent intent = new Intent(Intent.ACTION_VIEW);
                  intent.setData(Uri.parse(url));
                  YourActivity.this.startActivity(intent);
                }else{
               //2nd way 
                Intent intent = new Intent(YourActivity.this,NewActivity.class);
               //you can set the data 
                intent.setData(Uri.parse(url));
                YourActivity.this.startActivity(intent);
                }
    
            }   catch(ActivityNotFoundException e) {
    
                Log.e(LOGTAG,"Could not load url"+url);
            }
    
            return false;     
        }
    }
    

    you can set the mWebView.setWebViewClient(new WebViewClient()); which is default but by just adding a default custom WebViewClient will only allow the WebView to handle any loaded urls itself i.e. within the webView instead of browser.

    but if you overload by passing above class MyWebView like mWebView.setWebViewClient(new MyWebView());then you can control the the loading of url same thing u can achive

    myWebView.setWebViewClient(new WebViewClient(){ 
    
                    @TargetApi(Build.VERSION_CODES.N)
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
                    {
                        Uri uri = request.getUrl();
                        return shouldOverrideUrlLoading(uri.toString());
                    }
    
                    private boolean shouldOverrideUrlLoading(final String url)
                    {
                        Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);
                           //your code to handle the url for navigation will go here
                        try{
                              if(url.contains("yourcondition")){
                                //1st way to handle 
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setData(Uri.parse(url));
                                YourActivity.this.startActivity(intent);
                               }else{
                                   //2nd way 
                                Intent intent = new Intent(YourActivity.this,NewActivity.class);
                                //you can set the data 
                                intent.setData(Uri.parse(url));
                                YourActivity.this.startActivity(intent);
                               }
                             }catch(ActivityNotFoundException e) {    
                               Log.e(LOGTAG,"Could not load url"+url);
                            }                    
                       return false;
                       // return true; // this True means that application wants to leave the WebView and try to go to browser and handle the url itself, otherwise return false.
                     } 
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
    
            try {
                 if(url.contains("yourcondition")){
                 //1st way to handle
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                YourActivity.this.startActivity(intent);
                }else{
                 //2nd way 
                    Intent intent = new Intent(YourActivity.this,NewActivity.class);
               //you can set the data 
                intent.setData(Uri.parse(url));
                YourActivity.this.startActivity(intent);
               }    
            }   catch(ActivityNotFoundException e) {
    
                Log.e(LOGTAG,"Could not load url"+url);
            }
    
            return false;      
    
        }});
    

    In case you don't have control of webview object then this is not the solution i will update once i will solve this.

提交回复
热议问题