使用swift开发Cordova插件

傷城~ 2022-07-28 11:49 315阅读 0赞

最近研究了用swift开发cordova插件的问题,事实证明用swift开发cordova插件是完全可行的,不要再去折腾烦人的oc代码了!主要参考了一个地理围栏插件 https://github.com/cowbell/cordova-plugin-geofence ,然后自己根据需求开发了百度地图标注和带扫描效果的二维码扫描iOS cordova插件,官方的那个实在太差了。

用swift开发插件主要是在项目的 Bridging-Header.h中加入Cordova和插件本身用到的头文件,然后插件类定义要以

@objc(HWPXXXXPlugin) class 开头,其它和oc插件基本一样了。示意代码如下

  1. //
  2. // BaiduMapMarkPlugin.swift
  3. // cordova-BaiduMapMarkPlugin
  4. //
  5. // Created by zxt on 2016/04/08.
  6. //
  7. //
  8. import Foundation
  9. import WebKit
  10. @available(iOS 8.0, *)
  11. @objc(HWPBaiduMapMarkPlugin) class BaiduMapMarkPlugin : CDVPlugin {
  12. func initialize(command: CDVInvokedUrlCommand) {
  13. print("BaiduMapMarkPlugin initialization")
  14. }
  15. func location(command: CDVInvokedUrlCommand) {
  16. print("location")
  17. var pointUser = PointUser()
  18. if command.arguments != nil && command.arguments.count > 0 {
  19. let geoInfo = command.arguments[0] as! String
  20. print(geoInfo)
  21. let point = convertStringToDictionary(geoInfo)
  22. print(convertStringToDictionary(geoInfo))
  23. pointUser.storeName = point!["storeName"]!
  24. pointUser.pro = point!["pro"]!
  25. pointUser.city = point!["city"]!
  26. pointUser.dist = point!["dist"]!
  27. pointUser.address = point!["address"]!
  28. pointUser.latitude = Double(point!["latitude"]!)
  29. pointUser.longitude = Double(point!["longitude"]!)
  30. print(pointUser)
  31. }
  32. let mapVc = BaiduMapViewController()
  33. mapVc.isAnon = true
  34. mapVc.pointUser = pointUser
  35. mapVc.callBackId = command.callbackId
  36. mapVc.baiduMapMarkPlugin = self
  37. self.viewController?.presentViewController(mapVc, animated: true,completion: nil)
  38. }
  39. func convertStringToDictionary(text: String) -> [String:String]? {
  40. if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
  41. do {
  42. return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:String]
  43. } catch let error as NSError {
  44. print(error)
  45. }
  46. }
  47. return nil
  48. }
  49. }

百度地图标注cordova插件项目地址:

https://github.com/offbye/cordova-plugin-qianmi-baidumapmark

发表评论

表情:
评论列表 (有 0 条评论,315人围观)

还没有评论,来说两句吧...

相关阅读

    相关 使用swift开发Cordova

    最近研究了用swift开发cordova插件的问题,事实证明用swift开发cordova插件是完全可行的,不要再去折腾烦人的oc代码了!主要参考了一个地理围栏插件 https