DCloud框架IOS平台以Widget方式集成HTML5+SDK方法

0、不知道什么是DCloud的就暂时飘过吧,虽然这东西也不是很有名,但最近有用到这个就分享一下。

1、官方的文档实在是写得太随意了,很不全面并且有些问题在官方文档甚至论坛中根本就找不到相关解决方案,这次基于Xcode7.3以新建一个工程开始从零集成Dcloud框架,以及集成过程中出现的一些典型问题做出解决说明;

2、框架集成
2.1、下载官方SDK包,下载链接没有固定的URL或页面,所以各位自己去官方找吧;
2.2、解压出来,找到我们需要的一些基础文件

1
2
3
4
Bundles--目录至少需要PandoraApi.bundle这一个资源包,简单起见就先只引进这一个即可
inc------目录所有的文件及目录都原封不同的需要
Libs-----目录找到5个静态文件libcoreSupport.a、liblibNativeUI.a、liblibNavigator.a、liblibPDRCore.a、liblibUI.a、liblibWidget.a
#该死的进度条

2.3、好了,新建一个文件夹比如叫DCloud,然后把上面整理出来的三个文件夹放里面,现在结构是

20160719-0@2x

2.4、将上面这个目录加入到项目中,Added folders这个选项选Create groups

3、调试处理
3.1、将SDK集成到项目中直接编译还不会有任何错误提示
3.2、启动时初始化DCloud框架引擎,在AppDelegate中修改方法

1
2
3
4
5
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return [PDRCore initEngineWihtOptions:launchOptions withRunMode:PDRCoreRunModeAppClient];
}

PDRCoreRunMode有三种模式,这里一定要选PDRCoreRunModeAppClient
其他两种模式的说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef NS_ENUM(NSInteger, PDRCoreRunMode) {
    /**
     正常启动runtime
     使用该方法启动runtime具有全部功能
     包括具有应用管理、窗口管理、插件管理、权限管理、资源管理等功能
     */
    PDRCoreRunModeNormal = 0,
    /**
     使用该方法启动的runtime不具有应用管理窗口管理功能
     当需要显示页面时,需要自己创建PDRCoreAppFrame
     */
    PDRCoreRunModeWebviewClient = 1,
    /**
     使用该方法启动的runtime具有正常启动所有功能但不包含启动图片
     应用升级、应用copy运行逻辑,该启动会自动启动app管理器但
     不会自动创建app,需要自己创建app
     */
    PDRCoreRunModeAppClient
};

3.3、加了这句后报出30个错误,贴出来一个一个解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Undefined symbols for architecture x86_64:
  "_AudioServicesPlaySystemSound", referenced from:
      -[PGDeviceStatus beep:] in liblibPDRCore.a(PGDeviceStatus.o)
      -[PGDeviceStatus vibrate:] in liblibPDRCore.a(PGDeviceStatus.o)
  "_CTRadioAccessTechnologyHSDPA", referenced from:
      +[PTNetInfo reachabilityStatusForFlags:] in liblibPDRCore.a(PDRToolSystemEx.o)
  "_CTRadioAccessTechnologyLTE", referenced from:
      +[PTNetInfo reachabilityStatusForFlags:] in liblibPDRCore.a(PDRToolSystemEx.o)
  "_CTRadioAccessTechnologyWCDMA", referenced from:
      +[PTNetInfo reachabilityStatusForFlags:] in liblibPDRCore.a(PDRToolSystemEx.o)
  "_OBJC_CLASS_$_CTTelephonyNetworkInfo", referenced from:
      objc-class-ref in liblibPDRCore.a(PDRToolSystemEx.o)
  "_OBJC_CLASS_$_MPMusicPlayerController", referenced from:
      objc-class-ref in liblibPDRCore.a(PGDeviceStatus.o)
  "_UTTypeCopyPreferredTagWithClass", referenced from:
      +[PTPathUtil getMimeTypeFromPath:] in liblibPDRCore.a(PTPathUtil.o)
  "_UTTypeCreatePreferredIdentifierForTag", referenced from:
      +[PTPathUtil getMimeTypeFromPath:] in liblibPDRCore.a(PTPathUtil.o)
  "___gxx_personality_v0", referenced from:
      +[PTDevice sharedDevice] in liblibPDRCore.a(PDRToolSystemEx.o)
      Dwarf Exception Unwind Info (__eh_frame) in liblibPDRCore.a(PDRToolSystemEx.o)
      +[PGDeviceStatusJSBuilder sharedJSBulider] in liblibPDRCore.a(PGDeviceStatus.o)
      Dwarf Exception Unwind Info (__eh_frame) in liblibPDRCore.a(PGDeviceStatus.o)
      -[PTOperationQueue startAll] in liblibPDRCore.a(PTOperation.o)
      -[PTOperationQueue schedule] in liblibPDRCore.a(PTOperation.o)
      -[PTOperationQueue truePause] in liblibPDRCore.a(PTOperation.o)
      ...
  "_crc32", referenced from:
      -[ZipArchive addFileToZip:newname:] in libcoreSupport.a(ZipArchive.o)
      _unzReadCurrentFileZBQ in libcoreSupport.a(unzip.o)
      _zipWriteInFileInZip in libcoreSupport.a(zip.o)
  "_deflate", referenced from:
      _zipWriteInFileInZip in libcoreSupport.a(zip.o)
      _zipCloseFileInZipRaw in libcoreSupport.a(zip.o)
  "_deflateEnd", referenced from:
      _zipCloseFileInZipRaw in libcoreSupport.a(zip.o)
  "_deflateInit2_", referenced from:
      _zipOpenNewFileInZip3 in libcoreSupport.a(zip.o)
  "_get_crc_table", referenced from:
      _unzOpenCurrentFile3ZBQ in libcoreSupport.a(unzip.o)
      _zipOpenNewFileInZip3 in libcoreSupport.a(zip.o)
  "_inflate", referenced from:
      _unzReadCurrentFileZBQ in libcoreSupport.a(unzip.o)
  "_inflateEnd", referenced from:
      _unzCloseCurrentFileZBQ in libcoreSupport.a(unzip.o)
  "_inflateInit2_", referenced from:
      _unzOpenCurrentFile3ZBQ in libcoreSupport.a(unzip.o)
  "_kUTTagClassFilenameExtension", referenced from:
      +[PTPathUtil getMimeTypeFromPath:] in liblibPDRCore.a(PTPathUtil.o)
  "_kUTTagClassMIMEType", referenced from:
      +[PTPathUtil getMimeTypeFromPath:] in liblibPDRCore.a(PTPathUtil.o)
  "_xmlDocGetRootElement", referenced from:
      -[PDRCoreSettings load] in liblibPDRCore.a(PDRCoreSettings.o)
      -[PDRCoreUpdateParse parse:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlFirstElementChild", referenced from:
      -[PDRCoreSettings load] in liblibPDRCore.a(PDRCoreSettings.o)
      -[PDRCoreUpdateParse parseRemove:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRCoreUpdateParse parse:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlFree", referenced from:
      -[DC5PAppStartParams initXmlNode:] in liblibPDRCore.a(PDRCoreSettings.o)
      -[PDRCoreSettings load] in liblibPDRCore.a(PDRCoreSettings.o)
      +[PDRCoreUpdateRItem parse:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRCoreUpdateParse parseRoot:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRCoreUpdateParse parseBasic:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlFreeDoc", referenced from:
      -[PDRCoreSettings load] in liblibPDRCore.a(PDRCoreSettings.o)
      -[PDRCoreUpdateParse parse:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlGetProp", referenced from:
      -[DC5PAppStartParams initXmlNode:] in liblibPDRCore.a(PDRCoreSettings.o)
      -[PDRCoreSettings load] in liblibPDRCore.a(PDRCoreSettings.o)
      +[PDRCoreUpdateRItem parse:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRCoreUpdateParse parseRoot:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRCoreUpdateParse parseBasic:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlNextElementSibling", referenced from:
      -[PDRCoreSettings load] in liblibPDRCore.a(PDRCoreSettings.o)
      -[PDRCoreUpdateParse parseRemove:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRCoreUpdateParse parse:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlParseFile", referenced from:
      -[PDRCoreSettings load] in liblibPDRCore.a(PDRCoreSettings.o)
      -[PDRCoreUpdateParse parse:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlSaveFileEnc", referenced from:
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlSetProp", referenced from:
      -[PDRIDERefreshParse initWithPath:] in liblibPDRCore.a(HbuilderSyncFileParse.o)
  "_xmlStrcasecmp", referenced from:
      -[PDRCoreSettings load] in liblibPDRCore.a(PDRCoreSettings.o)
      -[PDRCoreUpdateParse parseRemove:] in liblibPDRCore.a(UpdateFileParse.o)
      -[PDRCoreUpdateParse parse:] in liblibPDRCore.a(UpdateFileParse.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

3.3、先从简单的入手,最后10个错都以_xml开头,造成这个错误的原因是没有加入libxml2.tdb这个库,在Linked Frameworks and Libraries下添加就能减少10个错误,离成功前进了一大步;
3.4、其他报错也一样,将需要添加的库或框架都添加进来,我列举了一下,以下都是必须的

1
2
3
4
5
6
7
8
9
10
11
12
13
libxml2.tbd
libz.tbd
libc++.tbd
JavaScriptCore.framework
WebKit.framework
CoreLocation.framework
AVFoundation.framework
AssetsLibrary.framework
AddressBook.framework
MobileCoreServices.framework
MediaPlayer.framework
AudioToolbox.framework
CoreTelephoney.framework

都添加好了之后再编译就不会有任何报错了,至少我新建的项目是没有的,如果在现有的项目可能会有其他的报错,后面会慢慢讲解,为什么要添加这么多的框架和库呢?因为这个DCloud的插件非常多,支持各种扩展,而各种扩展是需要各种框架来支持的,所以才会需要这么多。不过framework都是动态库,不用担心占用App空间问题,不影响App包大小。
3.5、这些问题都解决后将官方的H5内容包加入到项目中来,将官方那个SDK包里的Pandora目录添加到项目中,Added folders选项中选择Create folder references,说到这里有童鞋要提问了,Create folder references和Create groups这两者有什么区别,参考: Xcode引入外界文件时选Create groups或Create folder references的区别
3.6、一切准备就绪了,在ViewController控制器的viewDidApper方法中加载页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
//  ViewController.m
//  DCloud
//
//  Created by Sian on 16/7/19.
//  Copyright ? 2016年 Sian. All rights reserved.
//
 
#import "ViewController.h"
#import "PDRCore.h"
#import "PDRCoreAppManager.h"
 
@interface ViewController () 
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
}
 
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
 
    // 设置5+内核的Delegate,5+API在修改状态风格和应用是否全屏时会调用
    PDRCore *h5Engine = [PDRCore Instance];
    h5Engine.coreDeleagete = self;
    h5Engine.persentViewController = self;
 
    // 设置5+SDK运行的View
    UIView *content = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:content];
    [[PDRCore Instance] setContainerView:content];
 
    // 启动该应用
    // 传入参数可以在页面中通过plus.runtime.arguments参数获取
    NSString* pArgus = @"id=plus.runtime.arguments";
    // 设置WebApp所在的目录,该目录下必须有mainfest.json
    NSString* pWWWPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Pandora/apps/HelloH5/www"];
    [[[PDRCore Instance] appManager] openAppAtLocation:pWWWPath withIndexPath:@"index.html" withArgs:pArgus withDelegate:nil];
}
@end

3.7、这个时候运行就会崩溃,崩溃原因很明显,前面我们都加了C++相关的库,这不是加着好看的,在Build Settings–Other Linker Flags中添加-ObjC,注意区分大小写!
3.8、再次运行应该就差不多可以看到如下所示页面了

Simulator Screen Shot 2016年7月19日 下午4.12.13

3.9、到这里还没完,但至少可以看到基础页面视图了,比如说控制台的错误提示:

1
2
2016-07-19 16:08:35.771 DCloud[12786:1811138] -canOpenURL: failed for URL: "hbuilder://" - error: "This app is not allowed to query for scheme hbuilder"
2016-07-19 16:08:35.791 DCloud[12786:1811709] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

这里iOS9及以后的特性,默认https访问,其他协议头的url访问需要在info.plist文件是加相关参数,还有Enable Bitcode要设置为NO之类的,就大家自己去慢慢玩吧,这里重点在DCloud的框架引入就不多展开了。

4、Demo下载:http://pan.baidu.com/s/1qY7u0PY 密码: dm3f

Leave a Reply