Build mPaaS in Swift

项目正式启动还有点时间,mPaaS多少也了解了点,尝试通过*** Cocoapodsswift***运行一下mPaaS!!

*** swift比OC写着心情舒畅

前面按部就班

    1. 新建一个空的swift工程
    1. 在终端执行pod init、pod mpass init 初始化Podfile
      编辑 Podfile,导入需要的包,包名和依赖,可以执行pod mpaas info查看
    1. pod install,没啥好说的, 没报错,就都出来了
    1. 打开.xcworkspace 编译,没问题,程序运行起来了!
      (提示yw1222.jpg找不到,参照保镖生成,我们重点不是这)

这时候项目的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文件

    1. 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]);

执行结果
    1. 在这种情况下,修改MobileRuntime.plist中的delegate名称前缀为工程名.XXXXAppDelegate解决
      MobileRuntime.plist

至此, swift中使用mPaaS貌似已经没有问题了

你可能感兴趣的:(Build mPaaS in Swift)