【iOS Web App】嵌入 Cordova WebView 到 iOS(XCode 4.6,Cordova 2.3.0+)

参考:Embedding Cordova WebView on iOS

 

开发环境:

XCode 4.6

Cordova 2.3.0

 

准备工作

<1 新建config.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<cordova>
    <preference name="KeyboardDisplayRequiresUserAction" value="true" />
    <preference name="SuppressesIncrementalRendering" value="false" />
    <preference name="UIWebViewBounce" value="true" />
    <preference name="TopActivityIndicator" value="gray" />
    <preference name="EnableLocation" value="false" />
    <preference name="EnableViewportScale" value="false" />
    <preference name="AutoHideSplashScreen" value="true" />
    <preference name="ShowSplashScreenSpinner" value="true" />
    <preference name="MediaPlaybackRequiresUserAction" value="false" />
    <preference name="AllowInlineMediaPlayback" value="false" />
    <preference name="OpenAllWhitelistURLsInWebView" value="false" />
    <preference name="BackupWebStorage" value="cloud" />

    <plugins>
        <plugin name="LocalStorage" value="CDVLocalStorage" />
        <plugin name="Device" value="CDVDevice" />
        <plugin name="Logger" value="CDVLogger" />
        <plugin name="Compass" value="CDVLocation" />
        <plugin name="Accelerometer" value="CDVAccelerometer" />
        <plugin name="Camera" value="CDVCamera" />
        <plugin name="NetworkStatus" value="CDVConnection" />
        <plugin name="Contacts" value="CDVContacts" />
        <plugin name="Debug Console" value="CDVDebugConsole" />
        <plugin name="File" value="CDVFile" />
        <plugin name="FileTransfer" value="CDVFileTransfer" />
        <plugin name="Geolocation" value="CDVLocation" />
        <plugin name="Notification" value="CDVNotification" />
        <plugin name="Media" value="CDVSound" />
        <plugin name="Capture" value="CDVCapture" />
        <plugin name="SplashScreen" value="CDVSplashScreen" />
        <plugin name="Echo" value="CDVEcho" />
        <plugin name="Battery" value="CDVBattery" />
        <plugin name="Globalization" value="CDVGlobalization" />
        <plugin name="InAppBrowser" value="CDVInAppBrowser" />
    </plugins>
</cordova>

新建config.xml文件,添加到项目中。

 

<2 创建www目录

在项目根目录下,创建文件夹www

在www文件夹下,新建文件index.html文件

范例:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
        <meta charset="utf-8">
    
            <script type="text/javascript" charset="utf-8" src="cordova.ios.js"></script>
            <script type="text/javascript">
                function onBodyLoad()
                {
                    document.addEventListener("deviceready", onDeviceReady, false);
                }

                function onDeviceReady()
                {
                    window.pageIsLoaded = true;
                    navigator.notification.alert("Cordova is working")        
                }
                </script>
            </head>
    <body onload="onBodyLoad()">
        <p>Hello World!</p>
    </body>
</html>

“Create folder references for any added folder”,添加到项目中

 

1、添加CordovaLib子项目

cordova-2.3.0/cordova-ios/CordovaLib下,将CordovaLib.xcodeproj拖拽到项目中

TARGET-> Build Settings -> Other Linker Flags,添加 -all_load 和 -Obj-C 

TARGET -> Build Phases -> Link Binaries with Libraries,添加一下frameworks:

  • AddressBook.framework
  • AddressBookUI.framework
  • AudioToolbox.framework
  • AVFoundation.framework
  • CoreLocation.framework
  • MediaPlayer.framework
  • QuartzCore.framework
  • SystemConfiguration.framework
  • MobileCoreServices.framework
  • CoreMedia.framework

TARGET -> Build Phases -> Target Dependencies,添加CordovaLib

TARGET -> Build Phases -> Link Binaries with Libraries,添加CordovaLia.a

TARGET-> Build Settings -> Header Search Path,添加一下项:(注意:带引号

"$(TARGET_BUILD_DIR)/usr/local/lib/include"

"$(OBJROOT)/UninstalledProducts/include"

"$(BUILT_PRODUCTS_DIR)" 

 

2、使用

新建CDVViewController子类

示例:

#import <UIKit/UIKit.h>
#import <Cordova/CDVViewController.h>

@interface ViewController : CDVViewController

@end

 

设置该对象的wwwFolderName属性,startPage属性

示例:

#import "AppDelegate.h"

#import "ViewController.h"

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[ViewController new] autorelease];
    self.viewController.wwwFolderName = @"www";
    self.viewController.startPage = @"index.html";
    self.viewController.useSplashScreen = YES;
    
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

@end

 

 运行结果:

 

 

posted on 2013-02-01 11:32  Anthony Li  阅读(10611)  评论(2编辑  收藏  举报

博客园博客已停止更新,博客地址:dyinigbleed.com