How to load a local .CSS file & JavaScript resources using iPhone UIWebView Class

This post will cover the basic setup and creation of an application with web content for iPhone that will load local CSS files and some useful JavaScript functions. Most of these hints I found partially in different blogs and forums listed in the reference section. The idea is to collect all them together. You can use the following technique to create a more attractive application design.

Creating an application using UIWebView control is not the focus of this post. Here is a helpful beginner’s tutorial for creating an application using UIWebView. We assume that you are already familiar with this topic.

UIWebView load

Our work will start with a little change in calling load method of the UIWebView instance. Instead of loadRequest

1

[[webView graphWebView] loadRequest:requestObj];

we use the loadHTMLString method, providing an NSString object that contains HTML code of our page as a parameter:

1

[[webView graphWebView] loadHTMLString:htmlPageStr baseURL:baseURL];

To get this page to load resources and styles correctly, you have to set up the right URL:

Initial loading screen with progress indicator

On the following reference[2] you can find a short and clear revision of the problem of displaying local images in a UIWebView using CSS. To use relative paths or files in UIWebView, you have to load the HTML into the view with the correct base URL parameter. The following example shows how to do this:

1

2

NSString *path = [[NSBundle mainBundle] bundlePath];

NSURL *baseURL = [NSURL fileURLWithPath:path];

And then call load method of UIWebView provide above NSURL parameter:

1

[[webView graphWebView] loadHTMLString:pageStr baseURL:baseURL];

A short explanation: An NSBundle object represents a location in the file system that groups code and resources that can be used in a program. mainBundlemethod returns the NSBundleobject that corresponds to the directory where the current application executable is located. bundlePath instance method of NSBundle returns the full pathname of the receiver’s bundle directory. NSURLclass is used to create an object which will hold the URL information. It provides a way to manipulate URLs and the resources they reference. loadHTMLStringmethod of UIWebView has parameter base URL that accept an NSURL object instead of a pathname as the file reference. Giving the above base URL to UIWebView, you can refer to your bundle’s resources directory like this:

1

<img src="myimage.png">

Or from within CSS like this:

1

background-image: url(loading.gif)

It’s important to note that resources (images, CSS and JavaScript Files) inside your application bundle are at the root of the bundle, even if you place them in an separate group (folder) in your project.

Giving the correct URL to UIWebView will allow you to refer to local resources in your page, but it does not work in all cases. If you want to use JavaScipt, some additional settings have to be done.

Link CSS and JavaScript files

Second loading screen with progress indicator

Now you can use generic HTML technique to add CSS and JavaScript to the web page and displaying in a UIWebView.

1

2

<link href="default.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="showhide.js"></script>

Do not forget to add your CSS and Javascript files to the xCode project (if you are using external files).

Some additional actions have to be taken before starting your application and using JavaScript functions.[3]

XCode setup (*.js) javascript as some type of source code needs to be compiled in the application. We would like to include it as a resource so I’ve solved it by doing two things:

  1. Select .js file and in the “Detail” view unselect the bullseye column indicating it is compiled code
  2. In the “Groups & files” view expand the “Targets” tree and expand the application then go to “Copy Bundle Resources” and drag the *.js files into it.

The Apple dev forums has a posted solution. [4] You need to do two things – select the .js file in your project, and turn off the checkbox that indicates that it is compiled (the “bullseye” column). If you don’t do this, you’ll get a warning in your build log about being unable to compile the file (which should be your first warning – always try to figure out and and correct any and all warnings that appear in your build).

Set transparent background color for UIWebView

Final screen with graph representation.

In our example we need to figure out a transparent background for UIWebView. On the parent View Container we have additional controls (buttons, labels) and already defined background. The following code will set this:

1

2

3

[[webView graphWebView] setBackgroundColor:[UIColor clearColor]];</code>

[[webView graphWebView] setOpaque:NO];

Example

MentorMateDemoJS is a simple application example which uses the settings described above. It is a view-based application that shows a graphical representation of some data. Application behavior consists of a predefined call with formatted data to Vvidget Service[5] for graph generation. The service returns an image that is loaded in UIWebView Design Resource. The update button is added to resend the graph request and update view.

The Problem: Graph image requires some time for loading.

Our Decision: Notify user of graph loading process by showing a progress bar during load time.

To achieve this, we use UIWebView with local resource of two JavaScript functions. One to show/hide notification messages and the other to visualize a progress bar during loading time. In the provided sources you can find all these steps implemented.

Sources of the example iPhone application

References

  1. iPhone SDK Articles, UIWebView Tutorial
  2. iPhone Development Blog, UIWebView – Loading External Images and CSS
  3. StackOverflow, iPhone UIWebView local resources using Javascript and handling on orientationChange
  4. Developer Forum, UIWebView and JavaScript
时间: 2024-11-13 09:05:44

How to load a local .CSS file & JavaScript resources using iPhone UIWebView Class的相关文章

网页提示[Not allowed to load local resource: file://XXXX]错误

网页通过http 访问时, 点击打开文件的link.在Chrome 中会报 Not allowed to load local resource: file// XXXX 的错误 <!--Add by oscar999--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document <

Chrome无法打开文件的错误 [Not allowed to load local resource: file://XXXX]

问题描述 <!--Add by oscar999--> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Author" CONTENT="oscar999"> <scri

HOWTO: Create native-looking iPhone/iPad applications from HTML, CSS and JavaScript

HOWTO: Create native-looking iPhone/iPad applications from HTML, CSS and JavaScript Though it's not widely known, you can write native-feeling iOS apps for the iPhone and iPad in JavaScript (+ HTML and CSS). In this article, I'll explain how to: stri

ERROR: Failed to load /usr/local/ERL_LIBS_FILE/esdl/rebar.config

***ERROR:Could not find sdl-config ERROR: Failed to load /usr/local/ERL_LIBS_FILE/esdl/rebar.config: {error, {228,file, {throw, error, [{erl_eval, do_apply, 5}, erl_eval, expr,5}, erl_eval, exprs, 5}, {erl_eval, expr,5}, {file, eval_stream2, 6}, {fil

otepad++ 配置 支持jquery、html、css、javascript、php代码提示

官网下载:http://notepad-plus-plus.org/ 获取插件的方法:打开软件,窗口工具栏有有一个问号,点获取插件. 我使用的插件(安装方法都是官方的方法): QuickText.v0.2.1.zip      //自定义缩写词,按快捷键后输出 定义的代码段   使用方法:http://immmmm.com/quicktext-for-notepad.html 安装方法: Just copy the QuickText.dll into Notepad++'s plugin di

Notepad++ 配置 支持jquery、html、css、javascript、php代码提示

原文:Notepad++ 配置 支持jquery.html.css.javascript.php代码提示 官网下载:http://notepad-plus-plus.org/ 获取插件的方法:打开软件,窗口工具栏有有一个问号,点获取插件. 我使用的插件(安装方法都是官方的方法): QuickText.v0.2.1.zip      //自定义缩写词,按快捷键后输出 定义的代码段   使用方法:http://immmmm.com/quicktext-for-notepad.html 安装方法: J

前端HTML、css、javascript学习

一.html.css.javascript基本认识 1.HTML 类型:超文本标记语言 用途:可用于网页结构的设计或搭建: 后缀:html.html 超文本简述:'超文本'就是指的是其可以包含图片.链接.音乐.程序等非文字元素 HTML简述如下: 1.标记语言:标记语言为非编程语言,不具备编程语言的程序逻辑 2.HTML组成部分:由标签.指令.转义字符(实体) 指令:被尖括号包裹有!开头的标记 转义字符:被&与:包裹的特殊字母组合或#开头的十进制数.(空格&nbsp,版权&copy

10 分钟,带你快速入门前端三大技术(HTML、CSS、JavaScript)

听到前端技术,不少朋友一定会感到有些陌生.但其实,前端,你每天都在接触. 你正在使用的APP,你正在浏览的网页,这些你能看到的界面,都属于前端. 而前端最重要的三大技术,HTML,CSS,JavaScript,则是每一个前端开发者必须具备的技能. 掌握这些技能,你可以快速地做出一个酷炫的APP界面或者一个简单大方的网站页面.因此,就让我们一起来快速学习一下这三门技术吧. 以下内容节选自课程<Vue.js 和 Node.js 构建内容发布系统>. 大家也可以点击课程链接,在实验楼提供的虚拟机环境

10个优化代码的CSS和JavaScript工具

  检查和测试代码来发现任何潜在错误,从而在放到网站上之前及时消除错误是一个非常重要的过程.代码检查的过程也俗称为是Web设计师和开发者之间的linting.作为一个设计师,如果你想要写出高度优化的代码,那么你一定需要linting工具.有两种类型的代码检查工具.一种是在执行时间检查代码中的错误和bug.另一种是使用静态代码分析技术并在执行前检查码.后者因为可以节省时间和麻烦显然更佳. 事实上,linting可以放在不同的阶段.如果你喜欢在敲代码的时候测试代码,那么你可以使用lint工具.当然,