项目正式启动还有点时间,mPaaS多少也了解了点,尝试通过*** Cocoapods和swift***运行一下mPaaS!!
*** swift比OC写着心情舒畅
前面按部就班
-
- 新建一个空的swift工程
-
- 在终端执行
pod init、pod mpass init
初始化Podfile
编辑 Podfile,导入需要的包,包名和依赖,可以执行pod mpaas info
查看
- 在终端执行
-
- pod install,没啥好说的, 没报错,就都出来了
-
- 打开.xcworkspace 编译,没问题,程序运行起来了!
(提示yw1222.jpg找不到,参照保镖生成,我们重点不是这)
- 打开.xcworkspace 编译,没问题,程序运行起来了!
这时候项目的mPaaS文件夹里面是创建好的
MPLauncherAppDelegate、MPTabbarController、DemoViewController
,
- 5 mPaaS是基于OC的,swift使用肯定要桥接, 头文件引用mPaaS的headers头文件(mPaaS自动创建)
#ifndef StepBySwift_bridge_h
#define StepBySwift_bridge_h
#import "StepBySwift-mPaaS-Headers.h"
#endif /* StepBySwift_bridge_h */
我这边然后想做的事儿就是让mPaaS可以加载swift文件
-
- 把
MPLauncherAppDelegate、MPTabbarController、DemoViewController
都先转成swift,删掉原文件 ,代码都很少,就全贴出来了
- 把
class MPLauncherAppDelegate: NSObject, DTMicroApplicationDelegate {
var tabBarController: UITabBarController?
var rootController: UIViewController?
override init() {
let vc1 = DemoViewController()
vc1.title = "Home"
let vc2 = DTViewController()
vc2.title = "Mine"
let tabBarController = MPTabBarController()
tabBarController.viewControllers = [vc1, vc2]
tabBarController.selectedIndex = 0
tabBarController.delegate?.tabBarController?(tabBarController, didSelect: vc1)
self.rootController = tabBarController
self.tabBarController = tabBarController
}
func rootController(in application: DTMicroApplication!) -> UIViewController! {
guard let root = rootController else { fatalError("rootController 不能为空") }
return root
}
}
class MPTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self;
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
self.title = viewController.title
self.navigationItem.leftBarButtonItem = viewController.navigationItem.leftBarButtonItem
self.navigationItem.leftBarButtonItems = viewController.navigationItem.leftBarButtonItems
self.navigationItem.rightBarButtonItem = viewController.navigationItem.rightBarButtonItem
self.navigationItem.rightBarButtonItems = viewController.navigationItem.rightBarButtonItems
}
}
class DemoViewController: DTViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.green
}
}
重点来了
- 7 此时再次运行app 就变白板了,控制台:
2019-08-08 17:11:50.603411+0800 StepBySwift[11067:545905] [APMobileRuntime] The application manager cannot find the application named 'Launcher'.
原因是Swift的类名,在编译的时候默认加上了工程名如MPLauncherAppDelegate 变成了:StepBySwift.MPLauncherAppDelegate
MPLauncherAppDelegate * del = [[MPLauncherAppDelegate alloc]init];
id vc;
Class cl = NSClassFromString(@"StepBySwift.MPLauncherAppDelegate");
if (cl != Nil) {
vc = [[cl alloc]init];
}
else {
vc = (DTViewController *)[[DTViewController alloc] init];
}
NSString * str = NSStringFromClass([MPLauncherAppDelegate class]);
NSString * str2 = NSStringFromClass([MPLauncherAppDelegate class]);
-
- 在这种情况下,修改MobileRuntime.plist中的delegate名称前缀为工程名.XXXXAppDelegate解决
- 在这种情况下,修改MobileRuntime.plist中的delegate名称前缀为工程名.XXXXAppDelegate解决
至此, swift中使用mPaaS貌似已经没有问题了