Languages

Menu
Sites
Language
How to load local html in WebView using C

Hi,

Been going through the WebView Native Sample https://developer.tizen.org/community/tip-tech/webview-native-application.

I would like to load a html file stored in the apps assets using the above sample, how can this be done?

Your help is greatly appreciated.

View Selected Answer

Responses

4 Replies
Mark as answer
Yasin Ali

Hi~,

You may try this idea.

1. Read a .html file from your desired resource in c string format.
2. Correct that string for escape-sequence character(s), like " to \".
3. Use EXPORT_API Eina_Bool ewk_view_html_string_load(Evas_Object* o, const char* html, const char* base_url, const char* unreachable_url) function to load that html string in webView.

See attached code for clarification.

static void create_base_gui(appdata_s *ad) {
    /* set up policy to exit when last window is closed */
 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
 /* Window */
 ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
 elm_win_autodel_set(ad->win, EINA_TRUE);

 int rots[4] = { 0, 90, 180, 270 };
 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *) (&rots),
   4);

 eext_object_event_callback_add(ad->win, EEXT_CALLBACK_BACK, win_back_cb,
   ad);

 {
  /* Box to put the table in so we can bottom-align the table
   * window will stretch all resize object content to win size */
  Evas_Object *box = elm_box_add(ad->win);
  evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND,
    EVAS_HINT_EXPAND);
  elm_win_resize_object_add(ad->win, box);
  evas_object_show(box);

  /* Table */
  Evas_Object *table = elm_table_add(ad->win);
  /* Make table homogenous - every cell will be the same size */
  elm_table_homogeneous_set(table, EINA_TRUE);
  /* Set padding of 10 pixels multiplied by scale factor of UI */
  elm_table_padding_set(table, 5 * elm_config_scale_get(),
    10 * elm_config_scale_get());
  /* Let the table child allocation area expand within in the box */
  evas_object_size_hint_weight_set(table, EVAS_HINT_EXPAND,
    EVAS_HINT_EXPAND);
  /* Set table to fiill width but align to bottom of box */
  evas_object_size_hint_align_set(table, EVAS_HINT_FILL, EVAS_HINT_FILL);
  elm_box_pack_end(box, table);
  evas_object_show(table);

  {
   /* Entry */
   ad->entry = elm_entry_add(ad->win);
   elm_entry_scrollable_set(ad->entry, EINA_TRUE);
   eext_entry_selection_back_event_allow_set(ad->entry, EINA_TRUE);
   elm_object_text_set(ad->entry, "http://www.tizen.org");
   my_table_pack(table, ad->entry, 0, 0, 3, 1);

   /* Button-1 */
   Evas_Object *btn = elm_button_add(ad->win);
   elm_object_text_set(btn, "Prev");
   evas_object_smart_callback_add(btn, "clicked", btn_prev_cb, ad);
   my_table_pack(table, btn, 0, 1, 1, 1);

   /* Button-2 */
   btn = elm_button_add(ad->win);
   elm_object_text_set(btn, "Go");
   evas_object_smart_callback_add(btn, "clicked", btn_go_cb, ad);
   my_table_pack(table, btn, 1, 1, 1, 1);

   /* Button-3 */
   btn = elm_button_add(ad->win);
   elm_object_text_set(btn, "Next");
   evas_object_smart_callback_add(btn, "clicked", btn_next_cb, ad);
   my_table_pack(table, btn, 2, 1, 1, 1);

   /* WebView */
   Evas *evas = evas_object_evas_get(ad->win);
   ad->web_view = ewk_view_add(evas);
   //ewk_view_url_set(ad->web_view, elm_object_text_get(ad->entry) );

    char html[99999] = "<!DOCTYPE html><html>"
      "<body>\" html in WEbView \""
      "<form>   First name:<br>   <input type=\"text\" name=\"firstname\">   <br>   Last name:<br>   <input type=\"text\" name=\"lastname\"> </form>"
      "<button onclick=\"JavaScript:alert(\'Well done!\')\">Click Me!</button>"
      "</body></html>";

   ewk_view_html_string_load(ad->web_view , html, NULL, NULL);
   my_table_pack(table, ad->web_view, 0, 2, 3, 8);

  }
 }

 /* Show window after base gui is set up */
 evas_object_show(ad->win);
}

Here html code is written statically.

Hope it will work for you.

Image Link : http://imgur.com/a/2wlSj

 

Karanja Kiniaru

Thanks Yasin

Karanja Kiniaru

I am using this as a fallback plan to show a local html when a webpage fails to load eg throws a 404 error.

Would you know of approprite page load listeners to be able to catch such cases(Errors like 404s) and consequently load the local html as you have explained above.

I am currently using what was suggested here--> https://developer.tizen.org/forums/native-application-development/webview-page-load-errors  and wondering how to merge that with your solution above.

Yasin Ali

Hi Karanja Kiniaru,

Now you are tryiing to catch error and to show an error page in response.

This is different from main focus of this post "How to load local html in WebView using C ".

So, it would be better to discuss if you create another thread in this respect.

Thanks