參考: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
運行結果: