一.首先從官網(wǎng)下載最新的的SDK
參照IOS平臺(tái)5+SDK技術(shù)白皮書(shū)把需要的環(huán)境配置好,這些都沒(méi)有問(wèn)題。
二.打開(kāi)H5界面是參照下圖中項(xiàng)目
首先將這個(gè)類(lèi)復(fù)制到項(xiàng)目中
三.以下是部分關(guān)鍵代碼
1.在工程的AppDelegate類(lèi)設(shè)置當(dāng)前SDK運(yùn)行模式 使用WebApp集成
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController* pNavCon = [[UINavigationController alloc]
initWithRootViewController:_window.rootViewController];
_window.rootViewController = pNavCon;
[pNavCon release];
// 設(shè)置當(dāng)前SDK運(yùn)行模式
// 使用WebApp集成是使用的啟動(dòng)參數(shù)
return [PDRCore initEngineWihtOptions:launchOptions withRunMode:PDRCoreRunModeAppClient];
}}
2.修改WebAppController中的代碼打開(kāi)自己的項(xiàng)目
@interface WebAppController()
{
PDRCoreApp* pAppHandle;
BOOL _isFullScreen;
UIStatusBarStyle _statusBarStyle;
}
@end
//這個(gè)子view一定要設(shè)置為靜態(tài),不然應(yīng)用退出時(shí)候會(huì)有問(wèn)題
static UIView* pContentVIew = nil;
@implementation WebAppController
- (void)loadView
{
[super loadView];
if(pContentVIew == nil)
pContentVIew = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: pContentVIew];
PDRCore *h5Engine = [PDRCore Instance];
[self setStatusBarStyle:h5Engine.settings.statusBarStyle];
_isFullScreen = [UIApplication sharedApplication].statusBarHidden;
if ( _isFullScreen != h5Engine.settings.fullScreen ) {
_isFullScreen = h5Engine.settings.fullScreen;
if ( [self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)] ) {
[self setNeedsStatusBarAppearanceUpdate];
} else {
[[UIApplication sharedApplication] setStatusBarHidden:_isFullScreen];
}
}
h5Engine.coreDeleagete = self;
h5Engine.persentViewController = self;
// 設(shè)置WebApp所在的目錄肤京,該目錄下必須有mainfest.json
NSString* pWWWPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Pandora/apps/H563EBC0A/www"];
// 如果路徑中包含中文抗斤,或Xcode工程的targets名為中文則需要對(duì)路徑進(jìn)行編碼
//NSString* pWWWPath2 = (NSString *)CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)pTempString, NULL, NULL, kCFStringEncodingUTF8 );
// 用戶在集成5+SDK時(shí)蹈垢,需要在5+內(nèi)核初始化時(shí)設(shè)置當(dāng)前的集成方式八千,
// 請(qǐng)參考AppDelegate.m文件的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法
// 設(shè)置5+SDK運(yùn)行的View
[[PDRCore Instance] setContainerView:pContentVIew];
// 傳入?yún)?shù)可以在頁(yè)面中通過(guò)plus.runtime.arguments參數(shù)獲取
NSString *pArgus=@"{\"name\": \"2.1.2\",\"code\": \"20102\"}";
// 啟動(dòng)該應(yīng)用
pAppHandle = [[[PDRCore Instance] appManager] openAppAtLocation:pWWWPath withIndexPath:@"index.html" withArgs:pArgus withDelegate:nil];
// pAppHandle = [[[PDRCore Instance] appManager] openAppWithAppid:@"H563EBC0A" withArgs:pArgus withDelegate:nil];
// 如果應(yīng)用可能會(huì)重復(fù)打開(kāi)的話建議使用restart方法
// [[[PDRCore Instance] appManager] restart:pAppHandle];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textClose:) name:@"CloseWebAPP" object:nil];
}
- (void)textClose:(NSNotification *)not{
//不要在消息觸發(fā)的方法里關(guān)閉應(yīng)用需要使用異步的方式關(guān)閉APP
[self performSelectorOnMainThread:@selector(classWebApp) withObject:nil waitUntilDone:NO];
}
- (void)classWebApp{
//調(diào)用AppManager的方法關(guān)閉應(yīng)用
[[PDRCore Instance].appManager end:pAppHandle];
[self.navigationController popViewControllerAnimated:NO];
}
注意:
// [[[PDRCore Instance] appManager] restart:pAppHandle];
這里不用這段代碼原因是因?yàn)槲疫@里要傳參數(shù)吗讶,所以不能用這個(gè)方法,否則第二次打開(kāi)界面的時(shí)候參數(shù)不會(huì)過(guò)去
以下這點(diǎn)尤為重要叼丑,被坑了很久关翎,提醒一下大家
//這個(gè)pContentVIew一定要設(shè)置為靜態(tài)扛门,不然應(yīng)用退出后再次打開(kāi)時(shí)候會(huì)有問(wèn)題鸠信,這個(gè)當(dāng)時(shí)折騰了很久,好不容易才找到论寨,
默認(rèn)這段代碼是注釋掉的星立。
static UIView* pContentVIew = nil;
if(pContentVIew == nil)
pContentVIew = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: pContentVIew];
// 設(shè)置5+SDK運(yùn)行的View
[[PDRCore Instance] setContainerView:pContentVIew];
3.調(diào)用WebAppController 打開(kāi)H5頁(yè)面
WebAppController* pWebAppController = [[WebAppController alloc] init];
if (pWebAppController) {
self.navigationController.navigationBarHidden = YES;
[self.navigationController pushViewController:pWebAppController animated:YES];
}
到這里打開(kāi)基本沒(méi)啥問(wèn)題了
4.關(guān)閉H5頁(yè)面
首先要說(shuō)的是,IOS關(guān)閉MUI和android的方式是不一樣的,plus.runtime.quit();只支持android葬凳,IOS要調(diào)用原生的代碼來(lái)關(guān)閉才行
上代碼
//MUI界面代碼
mui.back = function(event) {
if(mui.os.ios) {
var notiClass = plus.ios.importClass("NSNotificationCenter");
notiClass.defaultCenter().postNotificationNameobject("CloseWebAPP", null);
} else {
plus.runtime.quit();
}
}
WebAppController中對(duì)應(yīng)的代碼
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textClose:) name:@"CloseWebAPP" object:nil];
- (void)textClose:(NSNotification *)not{
//不要在消息觸發(fā)的方法里關(guān)閉應(yīng)用需要使用異步的方式關(guān)閉APP
[self performSelectorOnMainThread:@selector(classWebApp) withObject:nil waitUntilDone:NO];
}
- (void)classWebApp{
//調(diào)用AppManager的方法關(guān)閉應(yīng)用
[[PDRCore Instance].appManager end:pAppHandle];
[self.navigationController popViewControllerAnimated:NO];
}
以上關(guān)于IOS調(diào)用H5頁(yè)面 要注意的一些地方绰垂,至于H5+ API調(diào)用的類(lèi)請(qǐng)參考
Feature-iOS.xls 有對(duì)應(yīng)。