Home‎ > ‎

Accessing the Internet

Accessing the Internet
There are a number of ways to access the Internet from LiveCode programs. The easiest is using the "Launch" command. It runs (launches) the default application on your computer (e.g. I.E., Safari, etc) then transferring control to it. We did this in the last lesson.

The better way (but more difficult one) is by creating your own browser window inside of LiveCode. You stay inside of your Livecode application and have full control over what the user does.

1. Accessing the Internet - Externally in another program/window (for Desktops only)
This will open up a url in your default browser (another window). So if your default browser is FireFox, then it will open up a new window with firefox in it.

This works well on a desktop computer but not so well on a mobile computer (cell phone, tablet). Each launch command opens up a new window. On a desktop, most people have more than one window open at a time but how many windows can you open on a small cellphone?

The other disadvantage of doing it this way is that you leave your program and you are not in control any more.
 
Accessing the Internet (using the Launch command with a url address)  

You can access the Internet from within LiveCode by using the Launch command with URL specified

    e.g. 
    

         launch url "http://www.livecode.com"



Ask the user for the url:

Create a button and add this to the script:


        on mouseUp

               ask "What is the Internet address to go to?"

               put it into myurl

               launch url "http://" & myurl

        end mouseUp


It will pop-up a dialog box asking the user for an Internet address, then open it up in a browser window.



Example of a card with your favorite websites:


add buttons with images and code to go to each of your favorite websites:


e.g. 



The following is a sample of the code from each button on a student 's app:

        on mouseUp

               launch url "https://www.gmail.com/"

        end mouseUp


        on mouseUp

               launch url "https://family.sis.pgcps.org/schoolmax/reset.do?0Mjumkffz13-SgYEjWekr3%3dxguw3YEa.aU7zaju.xnn.xGOSG-OG-                 Sh%2bSS%256UOd%256U6D.qhhgwEkeUsrgwUVm3sEV"   - cut and pasted from a normal browser window after going to this site

        end mouseUp


        on mouseUp

               launch url "http://www.collegeboard.org/"

        end mouseUp

    
        etc


        Notes: 

          if a filename has a space in the document name then revBrowser will silently fail. Replacing space with %20 cures this. replace " " with "%20" in myUrl



2. Accessing the Internet - Internally in LiveCode (in a browser window, Desktop and Mobile)


If you want to keep everything inside your app (without going to another program), you can use a  browser window (revBrowser object).

This works well in either a desktop or a mobile computer. The advantage of this is that you stay within your own program or app. You open a "browser" window on your card, and go to the web address.  When done, you have to close that "browser" window.  

It is tricky only because you have to write code to prevent errors and be very careful how and where you issue the commands. (You get errors if you do not do things in the right sequence or try to open 2 browsers with the same "ID".) 

First you need to initialize the revBrowser. It only has to be done once. (Just as you open up a normal browser - IE, FireFox, Safari, Chrome- and just use that one, to visit different sites). Most of the time, we put this in the openCard handler that runs when you first open the card.

a. Initializing the Browser Object (Window)
First create an image area for the browser window using the "image Area Tool" and size it on your card. Name it "browser image"
                                 


-----------Put this code on the Card Script --------------------------------

        global myBrowser

        on openCard
                browserOpen
        end openCard

        on browserOpen
                 put revBrowserOpen(the windowid of this stack,"") into myBrowser
                revBrowserSet myBrowser"showborder","true"
                revBrowserSet myBrowser"visible","true"
                revBrowserSet myBrowser"rect",rect of image "browser image"
        end browserOpen

        on browserClose
                revBrowserClose myBrowser
                put empty into myBrowser
        end browserClose

-----------Put this code on the Card Script --------------------------------

b. Showing the webpage

Put the code below on a button on your card. This will open the pdf in the "browser image" field that you created above

        global myBrowser
        on mouseUp
              revBrowserSet myBrowser , "url""http://www.msn.com"
        end mouseUp

Notes: 

1. if a filename has a space in the document name then revBrowser will silently fail. Replacing space with %20 cures this.

replace " " with "%20" in myUrl

2. You can do a lot more with revBrowser that we have discussed in this lesson, for more information have a look at the revBrowser functions and commands in the Dictionary.

C. Add code for the mobile platforms

        global myBrowser

         if the environment is "mobile" then
                mobileControlCreate "browser"
                put the result into myBrowser
                mobileControlSet myBrowser, "rect"the rect of group "Browser"
                mobileControlSet myBrowser, "visible""true"
                mobileControlSet myBrowser, "url""http://www.google.com"
        else
                put revBrowserOpen(the windowid of this stack,"www.google.com") into myBrowser
                revBrowserSet myBrowser"showborder","true"
                revBrowserSet myBrowser"visible","true"
                revBrowserSet myBrowser"rect",rect of image "browser image"
        end if


                note: if you are using version 6.7 or higher of LiveCode
                         Replace the line (above)
                                    put revBrowserOpen(the windowid of this stack,"www.google.com") into myBrowser
                         With
                                    put revBrowserOpenCef(the windowid of this stack,"www.google.com") into myBrowser
                 It has controls and works so much better


        on closeCard
                if the environment is "mobile" then
                        mobileControlDelete myBrowser
                else
                       revBrowserClose myBrowser
                      put empty into myBrowser
                end closeCard


For more examples of what you can do with revBrowser there is a Browser Sampler stack in the Resources/Examples folder of your Rev distribution.

You might also find the Explore LiveCode: The Internet tutorial interesting

3. If you are developing a program that is running on a mobile device, then have a look at this lesson:  http://lessons.runrev.com/s/lessons/m/4069/l/22836

old: revBrowser is only supported on desktop platforms. You can view PDF files from the Internet on iOS using Browser Control. Check out this lesson on how to do it: http://lessons.runrev.com/s/lessons/m/4069/l/22836-How-do-I-use-the-Browser-Control-



Advanced Coding:


Another Way with more detail and checks:

Here, you may just want to have separate buttons to open and close a browser window and others to go to specific websites: There are a set of commands to create and work within a browser window in LiveCode

Lets build a desktop browser:

1. Open a new Main Stack

Add the following handlers to your card or stack:

global xBrowser

on openBrowser   
   if xBrowser is not empty then
      answer "This app cannot create more than on browser instance. Please close this before creating another."
      exit openBrowser
   else
      put revBrowserOpen(the windowId of this stack, "http://www.msn.com") into xBrowser
      revBrowserSet xBrowser, "visible", true
      revBrowserSet xBrowser, "rect", the rect of graphic "template"
      if xBrowser is not an integer then
         answer "Failed to open browser"
      end if      
   end if   
end openBrowser

on closeBrowser
   revBrowserClose xBrowser
   put empty into xBrowser
end closeBrowser

After you open a browser window, to open a website use the following code:

      put revBrowserOpen(the windowId of this stack, "http://www.msn.com") into xBrowser

Dont forget to close it when you are finished

2  Single Use Websites

    on openMSN
          put revBrowserOpen(the windowId of this stack, "http://www.msn.com") into xBrowser
          revBrowserSet xBrowser, "visible", true
          revBrowserSet xBrowser, "rect", the rect of graphic "template"
           --revBrowserClose xBrowser
    end openMSN     
Comments