使用UniWebView只能访问一次网页

时间:2014-12-29 05:29:25

标签: c# unity3d uniwebview

这是我的代码我试图使用UniWebView在UIView中打开一个网页。网页只会打开一次。不能再打开它。我正在使用按钮打开页面。该按钮附有此脚本和UniWebView。

using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
public class InternetCheck : MonoBehaviour 
{
    UniWebView _webView;

void Awake()
{
    _webView = GetComponent<UniWebView> ();
}

void Start()
{

    _webView.OnLoadComplete += OnLoadComplete;
    _webView.OnWebViewShouldClose += OnWebViewShouldClose;
}

void OnLoadComplete(UniWebView webView, bool success,string errorMessage)
{
    if(success)
    {
        webView.Show();
    }
    else
    {
        Debug.LogError("Unable to load");
    }
    webView.ShowToolBar (true);
}


void BtnClicked()
{
    if (_webView == null) 
    {
        _webView = GetComponent<UniWebView>();      
    }

    if(isInternetAvailable())
    {

        _webView.insets = new UniWebViewEdgeInsets(0,0,0,0);
        _webView.url = "http://google.com";
        _webView.Load();
    }
    else
        if(!isInternetAvailable())
    {
        _webView.insets = new UniWebViewEdgeInsets(0,0,0,0);
        _webView.url = Application.streamingAssetsPath + "/Privacy Policy _ Terms of Use _ Cartoon Network.html";
        _webView.Load();
    }
}



bool OnWebViewShouldClose(UniWebView webView) {
    if (webView == _webView) {
        _webView = null;

        return true;
    }
    return false;
}

public static bool isInternetAvailable()
{
    string HtmlText = GetHtmlFromUri("http://google.com");
    if(HtmlText == "")
    {
        //MNAndroidMessage.Create(Const.NO_NETWORK_ALERT_TITLE, Const.NO_NETWORK_ALERT_MESSAGE);
        //          Debug.Log (" Please check your internet conection ");
        return false;
    }
    else if(!HtmlText.Contains("schema.org/WebPage"))
    {
        //MNAndroidMessage.Create(Const.NETWORK_LOGIN_ALERT_TITLE, Const.NETWORK_LOGIN_ALERT_MESSAGE);
        //          Debug.Log (" Please check your internet conection might be you need to password to connect");
        return false;
    }
    else
    {
        //          Debug.Log("Network available ");
        return true;
    }
}

public static string GetHtmlFromUri(string resource)
{
    string html = string.Empty;
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource);
    try
    {
        using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
        {
            bool isSuccess = (int)resp.StatusCode < 299 && (int)resp.StatusCode >= 200;
            if (isSuccess)
            {
                using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
                {
                    //We are limiting the array to 80 so we don't have
                    //to parse the entire html document feel free to 
                    //adjust (probably stay under 300)
                    char[] cs = new char[80];
                    reader.Read(cs, 0, cs.Length);
                    foreach(char ch in cs)
                    {
                        html +=ch;
                    }
                }
            }
        }
    }
    catch
    {
        return "";
    }
    return html;
}
}

1 个答案:

答案 0 :(得分:1)

找出问题所在。只需要将UniWebView组件添加回去即可。