如何在iOS应用中访问网页而无需再次登录

时间:2019-05-16 16:41:39

标签: ios swift wkwebview

我的应用程序中有一个webview。使用帖子,我无需登录即可进入网站首页,但是如果我尝试访问其他页面,则需要再次登录。我在iOS应用程序中仅遇到此问题,Android应用程序使用类似的代码即可正常工作。如何直接访问网站上的任何页面而无需再次登录?

以下是Swift中的代码。被注释掉的html是有效的。 html未注释是问题所在。

import UIKit
import WebKit

class ViewController: UIViewController {

    let webView: WKWebView = {
        let view = WKWebView()
        view.translatesAutoresizingMaskIntoConstraints = false

        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

  /*
        let html = """
        <!DOCTYPE html>
        <html>
        <head>
        <script>
        function submitData(){
        document.form1.submit();
        }
        </script>
        </head>
        <body onload='submitData()'>
        <form action="https://www.example.com" method='post' name='form1'>\n
        <input type='hidden' name='user' value='user@example.com'><br>
        <input type='hidden' name='password' value='expassword'><br>
        </form>
        </body>
        </html>
        """
  */



        let html = """
        <!DOCTYPE html>
        <html>
        <head>
        <script>
        function submitData(){
        document.form1.submit();
        }
        </script>
        </head>
        <body onload='submitData()'>
        <form action="https://www.example.com/page1" method='post' name='form1'>\n
        <input type='hidden' name='user' value='user@example.com'><br>
        <input type='hidden' name='password' value='expassword'><br>
        </form>
        </body>
        </html>
        """
       webView.loadHTMLString(html, baseURL: nil)
        view.addSubview(webView)
        setupWebViewScreen()
    }

    func setupWebViewScreen(){
        webView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        webView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        webView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
        webView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    }


}

这是适用于Android的Java代码:

package com.example.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.net.URLEncoder;

public class MainActivity extends Activity {

    private WebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = findViewById(R.id.activity_main_webview);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // REMOTE RESOURCE
         /*
         mWebView.loadUrl("http://example.com");
          */
        try {
/*
            String url = "https://example.com";
            String my_username = "user@example.com";
            String my_password = "expassword";
            String postData = "user=" + URLEncoder.encode(my_username, "UTF-8") + "&password=" + URLEncoder.encode(my_password, "UTF-8");
            mWebView.postUrl(url, postData.getBytes());
*/
/*
            String url = "https://example.com";
            String postData = "user=username@example.com&password=expassword";
            mWebView.postUrl(url, postData.getBytes());
*/
            //     mWebView.setWebViewClient(new MyWebViewClient());


            String html = "<!DOCTYPE html>" +
                    "<html>" +
                    "<head>" +
                    "<script>" +
                    "function submitData(){ "+
                    "document.form1.submit();"+
                    "}" +
                    "</script>" +
                    "</head>" +
                    "<body onload='submitData()'> "+
                    "<form action=\"https://www.example.com\" method='post' name='form1'>\n"+
                    "<input type='hidden' name='user' value='user@example.com'><br/>" +
                    "<input type='hidden' name='password' value='expassword'><br>" +
                    "</form>" +
                    "</body>" +
                    "</html>";


/*
            String html = "<!DOCTYPE html>" +
                    "<html>" +
                    "<head>" +
                    "<script>" +
                    "function submitData(){ "+
                    "document.form1.submit();"+
                    "}" +
                    "</script>" +
                    "</head>" +
                    "<body onload='submitData()'> "+
                    "<form action=\"https://www.example.com/page1" method='post' name='form1'>\n"+
                    "<input type='hidden' name='user' value='user@example.com'><br/>" +
                    "<input type='hidden' name='password' value='expassword'><br>" +
                    "</form>" +
                    "</body>" +
                    "</html>";
*/
            mWebView.loadData(html, "text/html", "UTF-8");


          //  WebView wv = new WebView(this);
          //  setContentView(wv);

            /*
            String url = "https://example.com";
            String data = "user=user@example.com&password=expassword";

            mWebView.postUrl(url, data.getBytes());
            Message message = new Message(data);
            mWebView.postWebMessage(message,url);
            */
        }
        catch(Exception e)
        {
            System.out.println("error message:"+e.getMessage());
        }
        // LOCAL RESOURCE
        // mWebView.loadUrl("file:///android_asset/index.html");
    }

    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

}

0 个答案:

没有答案