天天看點

【H5+ & Quick-cocos2dx整合】之iOS 二 內建H5+ SDK

下載下傳SDK

下載下傳HTML 5+ 離線打包SDK (http://ask.dcloud.net.cn/article/103)點選下載下傳SDK

下載下傳完成後打開可以看到SDK、demo、Feature-iOS.xls 、IOS平台5+SDK技術白皮書.docx

【H5+ & Quick-cocos2dx整合】之iOS 二 內建H5+ SDK

Feature-iOS.xls:iOS平台各擴充Feature API對應的庫檔案清單,我們需要參照這個清單逐個添加。

IOS平台5+SDK技術白皮書.docx: 有內建的詳細說明(照做即可)

添加 Frameworks 和Libraries

參照Feature-ios.xls清單,36行以内所有Frameword 和 Library都是H5+必須的依賴項,需要逐個添加到項目中(Link Binary With Libraries、Other LibrarianFlags中配置)

Feature名稱:功能包的名稱

LinkerFlagsà OtherLinker Flags

Library(.a) à Link Binary With Libraries

Info àInfo.plist File

Frameworkà LinkBinary With Libraries

下面以基礎包為例,對項目所需資源進行配置。

【H5+ & Quick-cocos2dx整合】之iOS 二 內建H5+ SDK

添加LinkerFlags

Target à BuildSetting à Linking à Other Linker Flags

輕按兩下在打開小窗中依次添加-llibNavigator -lcoreSupport -llibPDRCore –llibUI

添加Library 和Framework

Target à BuildPhases à Link Binary With Libraries

單擊“+”添加.a檔案和Framework包

添加資源

Target à BuildPhases à Copy Bundle Resources

單擊“+”添加.bundle檔案

配置Info.plist File

Target à BuildSettings à Packaging à Info.plist File

編輯Info.plist檔案,

添加:  App Transport SecuritySettings:[類型:Dictionary]

   Allow Arbitrary Loads:[類型:Boolean][值:YES]

【H5+ & Quick-cocos2dx整合】之iOS 二 內建H5+ SDK

配置引用包的路徑

Target à BuildSettings à Search Pathes

Framework Search Paths:添加Framework的所在目錄的路徑

Library Search Paths:添加Library檔案的所在目錄的路徑

引入H5+頭檔案

配置H5+ SDK資源檔案

PandoraApi.bundle檔案:5+SDK所必需要的資源檔案。

位于:SDK/ Bundles/ PandoraApi.bundle,将該檔案拖入項目中

【H5+ & Quick-cocos2dx整合】之iOS 二 內建H5+ SDK

或者通過targetàBuild PhasesàCopy Bundle Resources 添加資源檔案

添加後結果:

【H5+ & Quick-cocos2dx整合】之iOS 二 內建H5+ SDK

之後我們自己的插件也需要在些配置才能正常工作。

         control.xml檔案:檔案中配置了預設啟動應用的APPID,如使用離線打包方式則需要添加此檔案,如使用Widget或者Webview方式內建則不需要添加此檔案

<key>NSAppTransportSecurity</key><dict>    <key>NSAllowsArbitraryLoads</key>     <true/></dict>

添加H5+ WebApp

Target à BuildPhases à Copy Bundle Resources

單擊“+”,選擇下載下傳好的SDK目錄下的Pandora檔案夾。引用方式選擇“folder referneces”.引用成功後,修改目錄Pandora/apps/[appid]/www,apps的子目錄名稱應對control.xml中appid對應,同時修改manifest.json檔案中id的值改為control.xml中appid的值相同。

內建功能代碼

現在開始在原生生成代碼的基礎上進行內建。

1 設定5+SDK運作模式

在工程的AppDelegate類的添加代碼,當應用啟動時設定5+SDK的運作模式

-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    // 設定目前SDK運作模式

   return [PDRCoreinitEngineWihtOptions:launchOptions withRunMode:PDRCoreRunModeAppClient];

}

2     在應用退出時添加銷毀5+SDK運作環境

- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when theapplication is about to terminate. Save data if appropriate. See alsoapplicationDidEnterBackground:.

[PDRCore destoryEngine];

}

3      啟動5+運作環境

-(void)Start5pEngineAsWidget

{

    PDRCore *h5Engine = [PDRCoreInstance];

    CGRect newRect = self.view.bounds;

    _containerView = [[UIViewalloc] initWithFrame:newRect];

    _containerView.autoresizingMask =UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

    // 設定5+核心的Delegate,5+API在修改狀态風格和應用是否全屏時會調用

    h5Engine.coreDeleagete = self;

    h5Engine.persentViewController =self;

    [self.viewaddSubview:_containerView];

    // 設定WebApp所在的目錄,該目錄下必須有mainfest.json

    NSString* pWWWPath = [[[NSBundlemainBundle] bundlePath]stringByAppendingPathComponent:@"Pandora/apps/h5/www/"];

    // 設定5+SDK運作的View

    [[PDRCore Instance] setContainerView:_containerView];

    // 傳入參數可以在頁面中通過plus.runtime.arguments參數擷取

    NSString* pArgus = @"id=plus.runtime.arguments";

    // 啟動該應用

    pAppHandle = [[[PDRCoreInstance] appManager]openAppAtLocation:pWWWPath withIndexPath:@"index.html" withArgs:pArgus withDelegate:nil];

}

對應頭檔案引用:

#import "PDRCore.h"

#import "PDRToolSystem.h"

#import "PDRToolSystemEx.h"

#import "PDRToolSystem.h"

#import "PDRToolSystemEx.h"

#import "PDRCoreAppFrame.h"

#import "PDRCoreAppManager.h"

#import "PDRCoreAppWindow.h"

#import "PDRCoreAppInfo.h"

至些H5 Plus內建完成,編譯運作。

【H5+ &amp; Quick-cocos2dx整合】之iOS 二 內建H5+ SDK

出錯清單彙總:

錯誤1:

-canOpenURL: failed for URL:"hbuilder://" - error: "This app is not allowed to query forscheme hbuilder"

解決方法:在Info.plist中增加“LSApplicationQueriesSchemes類型 array”,再添加該項子項“streamapp”和“hbuilder”

錯誤2:線上更新h5報錯

-canOpenURL: failed for URL:"itms-apps://itunes.apple.com/cn/app/hello-h5+/id682211190?l=zh&mt=8"- error: "This app is not allowed to query for scheme itms-apps"

錯誤3:原因是引用的“Pandora/apps/h5/www”方式不對,正确引用資源方式是選擇“Createfolder referneces”

【H5+ &amp; Quick-cocos2dx整合】之iOS 二 內建H5+ SDK

繼續閱讀