Accessing external website from PhoneGap application

I wanted to create a PhoneGap application, which instead of pointing to a HTML page inside the application should open my blog site http://wpcertification.blogspot.com these are the steps that i followed to build the application, you can download it from here
  • I followed the instructions in Getting Started with Android to build application that points to index.html inside the application, i tried it once to make sure that it works
  • Then i changed the HelloCordovaActivity like this so that it points to http://wpcertification.blogspot.com
    
    package com.webspherenotes.cordova.sample;
    
    import org.apache.cordova.DroidGap;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class HelloCordovaActivity extends DroidGap {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.loadUrl("http://wpcertification.blogspot.com");
        }
    }
    
    The onCreate() method is calling super.loadUrl("http://wpcertification.blogspot.com"); so that http://wpcertification.blogspot.com page is opened in the WebView as soon as application starts
  • The default configuration of the PhoneGap application disallows opening of any external URL so i had to change the cordova.xml like this to allow access to http://wpcertification.blogspot.com
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <cordova>
     <!--  
     access elements control the Android whitelist.  
     Domains are assumed blocked unless set otherwise
      -->
    
        <access origin="http://127.0.0.1*"/> <!-- allow local pages -->
     
     
     <access origin="http://wpcertification.blogspot.com" />
     
     
     <!-- <access origin="https://example.com" /> 
     allow any secure requests to example.com -->
     <!-- <access origin="https://example.com" subdomains="true" /> 
     such as above, but including subdomains, such as www -->
     <!--  <access origin=".*"/>   
     Allow all domains, suggested development use only -->
    
        <log level="DEBUG"/>
        <preference name="classicRender" value="true" />
    </cordova>
    
    Take a look at XML comments in this file for syntax on how to allow access to external URLs
Now the application is ready and when i launch it i can see my blog being opened like this

2 comments:

Tuấn Hoàng Anh said...

why "" error "Description Resource Path Location Type Attribute is missing the Android namespace prefix"

Bangalore Web Guru said...

I am a regular user of your post, this one also was very interesting and well written. keep sharing the great work Web Design Agency in Bangalore | Website Developers in Bangalore