Web View, Detect If Error And What Page The User Is On...

I have an application that I need to ‘phone home’ to my web server to check if it is allowed to continue launching (like a serial key check).
What I have is a webview (setup is working, can load pages) and I have it load a page
say http://nuttysoftware.com/phone_home/myapp.php?key=0123456789

What i need the application to then do is display the page, but if it is not loading (proxy block or not internet etc) display an error dialog.
The PHP page (i have that under control) will then redirect the user to one of two pages, http://nuttysoftware.com/phone_home/continue.php and then the window will close and the application will finish launching or http://nuttysoftware.com/phone_home/fail.php?error=(error number here represents a error dialog)
and the application will display the error dialog and the app will quit.
Thanks SO MUCH :smiley: If you can help!
(I really on need the reading a page and reading a variable to a certain point)
try help me :slight_smile:

I assume that by now, you’ve either figured it out, or have given up. But for the benefit of posterity, I’ll see if I can’t point you in the right direction! :smiley:

I do something somewhat similar, myself. (I had to, since Safari won’t cooperate with window.open() on file:// URLs.)

Besides my “normal” app windows, I have a couple of utility windows. One has a webview so that I can show web pages to the user at the appropriate times, and I’ve created 2 related to “load time”: loadingUserInfo and userInfoError. My main window is displayMain. None of them are set to be visible at launch.

The following will: display the “Loading” window, do the magic in the still-hidden webview, and display either the main interface or the error window when done. With some minor magic, you can keep the rest of your UI inaccessible until you display the main interface. (Indeed, I stripped out some of my app’s magic, and changed the window names slightly, for clarity!)

on launched theObject
	tell window "loadingUserInfo"
		set visible to true
	end tell
	if not my getLoginInfo() then
		tell window "userInfoError"
			set visible to true
		end tell
		set displayMain to false
		set displayMain to true
	end if
	tell window "loadingUserInfo"
		set visible to false
	end tell
	if displayMain then
		tell window "Main"
			set visible to true
		end tell
	end if
	loadPage from "about:blank" into "htmlWindow"
end launched

on will close theObject
	else if name of theObject is "userInfoError" then
	end if
end will close

on getLoginInfo()
	loadPage from "http://www.mysite.com/profile?mode=login" into "htmlWindow"
	set webView to view 1 of window "htmlWindow"
	-- Without a delay, the checking of JS values seems to abort page load. Funk-ay!
	delay 3
	repeat with timer from 1 to 15
		tell window "htmlWindow"
			if (((call method "stringByEvaluatingJavaScriptFromString:" of webView with parameter ("document.readyState")) is "complete") and ((call method "stringByEvaluatingJavaScriptFromString:" of webView with parameter ("window.location")) is not "about:blank")) then
				set userName to call method "stringByEvaluatingJavaScriptFromString:" of webView with parameter ("document.forms.profile.username.value")
				set userID to call method "stringByEvaluatingJavaScriptFromString:" of webView with parameter ("document.forms.profile.user_id.value")
				set SID to call method "stringByEvaluatingJavaScriptFromString:" of webView with parameter ("document.forms.profile.sid.value")
				return true
			else if timer ≥ 15 then
				return false
				delay 1
			end if
		end tell
	end repeat
	-- Better safe than sorry!
	return false
end getLoginInfo

My only real complaint is that I like doing things by name, not number. And I don’t know the name of “view 1”. There’s a lot of stuff going on up there. If you need help with any of it, please do ask.

The major magic being done above is with checking whether the document’s loaded. That took quite a bit of reading and tinkering to get working right. (I refused to write any Objective C just to see if the document’s done!) Pretty much, it tries to load the page into the web view. Then, after the initial delay (2 worked fine for me, but I decided that 3 would be safer!), it starts a 15 second timer. It then uses JavaScript to ensure that the page has finished loading and that it’s not about:blank. (about:blank’s the default page when a webview is created.)

Once you have a loaded page, you can do whatever magic you need. I have all of my data in form fields to make life easier for processing. :wink: Oh, and do be sure to wire up your script to the appropriate windows! :lol:

And, if you don’t actually have a webview in your project, you’ll probably start wondering how I load my pages into it (you can see loadPage above!). Again, I didn’t like the Objective C route. :wink: I’ve included the original source of the code so that you can see the minor changes I have.

-- http://lists.apple.com/archives/Applescript-studio/2005/Apr/msg00142.html
on loadPage from theURL into windowName
	set URLWithString to call method "URLWithString:" of class "NSURL" with parameter theURL
	set requestWithURL to call method "requestWithURL:" of class "NSURLRequest" with parameter URLWithString
	tell window windowName
		set mainFrame to call method "mainFrame" of object (view "htmlArea")
	end tell
	call method "loadRequest:" of the mainFrame with parameter requestWithURL
end loadPage