使用swift开发Cordova插件
2016-04-15 18:13
525 查看
最近研究了用swift开发cordova插件的问题,事实证明用swift开发cordova插件是完全可行的,不要再去折腾烦人的oc代码了!主要参考了一个地理围栏插件 https://github.com/cowbell/cordova-plugin-geofence ,然后自己根据需求开发了百度地图标注和带扫描效果的二维码扫描iOS cordova插件,官方的那个实在太差了。
用swift开发插件主要是在项目的 Bridging-Header.h中加入Cordova和插件本身用到的头文件,然后插件类定义要以
@objc(HWPXXXXPlugin) class 开头,其它和oc插件基本一样了。示意代码如下`
//
// BaiduMapMarkPlugin.swift
// cordova-BaiduMapMarkPlugin
//
// Created by zxt on 2016/04/08.
//
//
百度地图标注cordova插件项目地址:
https://github.com/offbye/cordova-plugin-qianmi-baidumapmark
原文转载自:/article/11828558.html
用swift开发插件主要是在项目的 Bridging-Header.h中加入Cordova和插件本身用到的头文件,然后插件类定义要以
@objc(HWPXXXXPlugin) class 开头,其它和oc插件基本一样了。示意代码如下`
//
// BaiduMapMarkPlugin.swift
// cordova-BaiduMapMarkPlugin
//
// Created by zxt on 2016/04/08.
//
//
import Foundation import WebKit @available(iOS 8.0, *) @objc(HWPBaiduMapMarkPlugin) class BaiduMapMarkPlugin : CDVPlugin { func initialize(command: CDVInvokedUrlCommand) { print("BaiduMapMarkPlugin initialization") } func location(command: CDVInvokedUrlCommand) { print("location") var pointUser = PointUser() if command.arguments != nil && command.arguments.count > 0 { let geoInfo = command.arguments[0] as! String print(geoInfo) let point = convertStringToDictionary(geoInfo) print(convertStringToDictionary(geoInfo)) pointUser.storeName = point!["storeName"]! pointUser.pro = point!["pro"]! pointUser.city = point!["city"]! pointUser.dist = point!["dist"]! pointUser.address = point!["address"]! pointUser.latitude = Double(point!["latitude"]!) pointUser.longitude = Double(point!["longitude"]!) print(pointUser) } let mapVc = BaiduMapViewController() mapVc.isAnon = true mapVc.pointUser = pointUser mapVc.callBackId = command.callbackId mapVc.baiduMapMarkPlugin = self self.viewController?.presentViewController(mapVc, animated: true,completion: nil) } func convertStringToDictionary(text: String) -> [String:String]? { if let data = text.dataUsingEncoding(NSUTF8StringEncoding) { do { return try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:String] } catch let error as NSError { print(error) } } return nil } } `
百度地图标注cordova插件项目地址:
https://github.com/offbye/cordova-plugin-qianmi-baidumapmark
原文转载自:/article/11828558.html
相关文章推荐
- Ubuntu上配置Swift的开发环境,基于JetBrains的Clion编辑器
- Swift中闭包实现OC的block传值
- [Swift 开发] UICollectionView的用法
- Swift笔记:类和结构体
- swift语言的Block
- swift和oc的混编
- Swift笔记:枚举
- 窥探 Swift 之别具一格的 Struct 和 Class
- 使用swift开发Cordova插件
- Swift笔记:闭包
- Swift类和结构体详解
- swift使用xib绘制UIView
- Swift - 按钮(UIButton)的用法
- Swift写的超级简单的五子棋
- Swift写的超级简单的五子棋
- Swift:playground
- Swift 取子字符串
- Swift中文教程(一)基础数据类型
- Swift笔记:函数
- Swift笔记:For-In、While 循环、if条件语句