generic2709 generic2709 - 1 year ago 71
C# Question

Web page can be accessed only once using UniWebView

This is my code i am trying to open a web page in UIView using UniWebView. The web page opens but just once. cannot open it again. I am using a button to open the page. the button has this scrip and UniWebView attached to it.

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;
}
}

Answer Source

Figured out the issue. Just need to add the UniWebView component back as its being removed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download