打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【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

 

 运行结果:

 

  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
ionic 2减少载入app时间设置
Cordova 3.x 基础(1)
iOS 获取当前正在显示的ViewController
iOS 获取通讯录的4种方式详解
iOS10.3 起,将支持应用内评分
MVC 3.0 在各个版本IIS中的部署
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服