宽容他人,放过自己。

如何实现iOS热更新

Posted on By anchoriteFili

如何实现iOS热更新
热更新.zip

相关代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        JPEngine.startEngine()
        let sourcePath = NSBundle.mainBundle().pathForResource("mq", ofType: "js")
        
        /**
         * 使用do-try-catch错误模式执行方法
         * 本地获取js入口
         */
        do {
            let script = try String(contentsOfFile: sourcePath!, encoding: NSUTF8StringEncoding)
            JPEngine.evaluateScript(script)
        } catch let err as NSError {
            err.description
        } catch DAOError.NoData {
            print("没有数据")
        } catch DAOError.PrimaryKeyNull {
            print("主键为空")
        }
        
        // 网络获取js脚本入口
        let session = NSURLSession.sharedSession()
        let request = NSURLRequest(URL: NSURL(string: "http://脚本连接.com")!)
        let task = session.dataTaskWithRequest(request) { (data, response, error) in
            
            if (error != nil) {
                print(error?.description)
            } else {
                
                let script = String.init(data: data!, encoding: NSUTF8StringEncoding)
                JPEngine.evaluateScript(script)
            }
        }
        task.resume()
        
        return true
    }