diff --git a/.gitignore b/.gitignore index 0645743..2ea8df5 100755 --- a/.gitignore +++ b/.gitignore @@ -57,4 +57,6 @@ fastlane/screenshots # After new code Injection tools there's a generated folder /iOSInjectionProject # https://github.com/johnno1962/injectionforxcode -iOSInjectionProject/ \ No newline at end of file +iOSInjectionProject/ + +.DS_Store \ No newline at end of file diff --git a/SwiftJavaScriptCore.xcodeproj/project.pbxproj b/SwiftJavaScriptCore.xcodeproj/project.pbxproj index ea5e428..386a927 100644 --- a/SwiftJavaScriptCore.xcodeproj/project.pbxproj +++ b/SwiftJavaScriptCore.xcodeproj/project.pbxproj @@ -94,11 +94,12 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0830; ORGANIZATIONNAME = Mayanlong; TargetAttributes = { 2330DDB81D07D04E00230385 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0830; }; }; }; @@ -179,8 +180,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -223,8 +226,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -243,6 +248,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.2; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; VALIDATE_PRODUCT = YES; }; name = Release; @@ -255,6 +261,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.mayanlong.SwiftJavaScriptCore; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -266,6 +273,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.mayanlong.SwiftJavaScriptCore; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -288,6 +296,7 @@ 2330DDCD1D07D04E00230385 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/SwiftJavaScriptCore/AppDelegate.swift b/SwiftJavaScriptCore/AppDelegate.swift index 37acc71..f9c0e54 100644 --- a/SwiftJavaScriptCore/AppDelegate.swift +++ b/SwiftJavaScriptCore/AppDelegate.swift @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/SwiftJavaScriptCore/Assets.xcassets/AppIcon.appiconset/Contents.json b/SwiftJavaScriptCore/Assets.xcassets/AppIcon.appiconset/Contents.json index 118c98f..b8236c6 100644 --- a/SwiftJavaScriptCore/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/SwiftJavaScriptCore/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -1,5 +1,15 @@ { "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "29x29", diff --git a/SwiftJavaScriptCore/Base.lproj/Main.storyboard b/SwiftJavaScriptCore/Base.lproj/Main.storyboard index 3a2a49b..9070b68 100644 --- a/SwiftJavaScriptCore/Base.lproj/Main.storyboard +++ b/SwiftJavaScriptCore/Base.lproj/Main.storyboard @@ -1,21 +1,26 @@ - - + + + + + - + + + - + - + - + diff --git a/SwiftJavaScriptCore/ViewController.swift b/SwiftJavaScriptCore/ViewController.swift index b8bd6c3..943fc92 100644 --- a/SwiftJavaScriptCore/ViewController.swift +++ b/SwiftJavaScriptCore/ViewController.swift @@ -13,16 +13,16 @@ import JavaScriptCore @objc protocol SwiftJavaScriptDelegate: JSExport { // js调用App的微信支付功能 演示最基本的用法 - func wxPay(orderNo: String) + func wxPay(_ orderNo: String) // js调用App的微信分享功能 演示字典参数的使用 - func wxShare(dict: [String: AnyObject]) + func wxShare(_ dict: [String: AnyObject]) // js调用App方法时传递多个参数 并弹出对话框 注意js调用时的函数名 - func showDialog(title: String, message: String) + func showDialog(_ title: String, message: String) // js调用App的功能后 App再调用js函数执行回调 - func callHandler(handleFuncName: String) + func callHandler(_ handleFuncName: String) } @@ -32,32 +32,32 @@ import JavaScriptCore weak var controller: UIViewController? weak var jsContext: JSContext? - func wxPay(orderNo: String) { + func wxPay(_ orderNo: String) { print("订单号:", orderNo) // 调起微信支付逻辑 } - func wxShare(dict: [String: AnyObject]) { + func wxShare(_ dict: [String: AnyObject]) { print("分享信息:", dict) // 调起微信分享逻辑 } - func showDialog(title: String, message: String) { + func showDialog(_ title: String, message: String) { - let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) - alert.addAction(UIAlertAction(title: "确定", style: .Default, handler: nil)) - self.controller?.presentViewController(alert, animated: true, completion: nil) + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil)) + self.controller?.present(alert, animated: true, completion: nil) } - func callHandler(handleFuncName: String) { + func callHandler(_ handleFuncName: String) { let jsHandlerFunc = self.jsContext?.objectForKeyedSubscript("\(handleFuncName)") - let dict = ["name": "sean", "age": 18] - jsHandlerFunc?.callWithArguments([dict]) + let dict = ["name": "sean", "age": 18] as [String : Any] + let _ = jsHandlerFunc?.call(withArguments: [dict]) } } @@ -88,12 +88,12 @@ class ViewController: UIViewController, UIWebViewDelegate { // 通过js方法名调用方法 let result2 = context.evaluateScript("sum(num1, num2)") - print(result2) // 输出30 + print(result2 as AnyObject) // 输出30 // 通过下标来获取js方法并调用方法 let squareFunc = context.objectForKeyedSubscript("sum") - let result3 = squareFunc.callWithArguments([10, 20]).toString() - print(result3) // 输出30 + let result3 = squareFunc?.call(withArguments: [10, 20]).toString() + print(result3 as AnyObject) // 输出30 } @@ -105,8 +105,8 @@ class ViewController: UIViewController, UIWebViewDelegate { self.webView.scalesPageToFit = true // 加载本地Html页面 - let url = NSBundle.mainBundle().URLForResource("demo", withExtension: "html") - let request = NSURLRequest(URL: url!) + let url = Bundle.main.url(forResource: "demo", withExtension: "html") + let request = URLRequest(url: url!) // 加载网络Html页面 请设置允许Http请求 //let url = NSURL(string: "http://www.mayanlong.com"); @@ -116,20 +116,20 @@ class ViewController: UIViewController, UIWebViewDelegate { } - func webViewDidFinishLoad(webView: UIWebView) { + func webViewDidFinishLoad(_ webView: UIWebView) { - self.jsContext = webView.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as! JSContext + self.jsContext = webView.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as! JSContext let model = SwiftJavaScriptModel() model.controller = self model.jsContext = self.jsContext // 这一步是将SwiftJavaScriptModel模型注入到JS中,在JS就可以通过WebViewJavascriptBridge调用我们暴露的方法了。 - self.jsContext.setObject(model, forKeyedSubscript: "WebViewJavascriptBridge") + self.jsContext.setObject(model, forKeyedSubscript: "WebViewJavascriptBridge" as NSCopying & NSObjectProtocol) // 注册到本地的Html页面中 - let url = NSBundle.mainBundle().URLForResource("demo", withExtension: "html") - self.jsContext.evaluateScript(try? String(contentsOfURL: url!, encoding: NSUTF8StringEncoding)) + let url = Bundle.main.url(forResource: "demo", withExtension: "html") + self.jsContext.evaluateScript(try? String(contentsOf: url!, encoding: String.Encoding.utf8)) // 注册到网络Html页面 请设置允许Http请求 //let url = "http://www.mayanlong.com"; @@ -137,7 +137,7 @@ class ViewController: UIViewController, UIWebViewDelegate { //self.jsContext.evaluateScript(try? String(contentsOfURL: NSURL(string: url)!, encoding: NSUTF8StringEncoding)) self.jsContext.exceptionHandler = { (context, exception) in - print("exception:", exception) + print("exception:", exception as Any) } }

AltStyle によって変換されたページ (->オリジナル) /