Ios Swift 4-通过UIActivityViewController共享位置
我正在尝试构建一个应用程序,我可以在任何导航应用程序(谷歌地图、苹果地图、waze等)中共享地址并打开它。 下面是我目前拥有的代码(在浏览了谷歌搜索结果页面,包括几十个stackoverflow问题之后) 当我在模拟器上运行应用程序时,我得到以下结果: 单击“共享”按钮后Ios Swift 4-通过UIActivityViewController共享位置,ios,swift,Ios,Swift,我正在尝试构建一个应用程序,我可以在任何导航应用程序(谷歌地图、苹果地图、waze等)中共享地址并打开它。 下面是我目前拥有的代码(在浏览了谷歌搜索结果页面,包括几十个stackoverflow问题之后) 当我在模拟器上运行应用程序时,我得到以下结果: 单击“共享”按钮后 为什么我得不到苹果地图或谷歌地图之类的应用建议?我也不明白为什么它建议我把它复制到contacts 提前谢谢。试试这个 // if the device has google maps installed in it if
为什么我得不到苹果地图或谷歌地图之类的应用建议?我也不明白为什么它建议我把它复制到contacts 提前谢谢。试试这个
// if the device has google maps installed in it
if (UIApplication.shared.canOpenURL(NSURL(string:"comgooglemaps://")! as URL)) {
UIApplication.shared.openURL(NSURL(string:
"comgooglemaps://?saddr=&daddr=\(myLatitude),\(myLongitude)&directionsmode=driving")! as URL)
}
// if google maps is not installed, try apple map
else if (UIApplication.shared.canOpenURL(NSURL(string:"http://maps.apple.com/maps")! as URL)) {
// apple map
let url = "http://maps.apple.com/maps?saddr=\(from.latitude),\(from.longitude)&daddr=\(to.latitude),\(to.longitude)"
UIApplication.shared.openURL(URL(string:url)!)
}
// if apple map is also not there, it will show an appStore link to download apple map application.
使用UIActivityViewController、Swift 5共享位置
func activityItems(latitude: Double, longitude: Double) -> [AnyObject]? {
var items = [AnyObject]()
let URLString = "https://maps.apple.com?ll=\(latitude),\(longitude)"
if let url = NSURL(string: URLString) {
items.append(url)
}
let locationVCardString = [
"BEGIN:VCARD",
"VERSION:3.0",
"PRODID:-//Joseph Duffy//Blog Post Example//EN",
"N:;Shared Location;;;",
"FN:Shared Location",
"item1.URL;type=pref:\(URLString)",
"item1.X-ABLabel:map url",
"END:VCARD"
].joined(separator: "\n")
guard let vCardData : NSSecureCoding = locationVCardString.data(using: .utf8) as NSSecureCoding? else {
return nil
}
let vCardActivity = NSItemProvider(item: vCardData, typeIdentifier: kUTTypeVCard as String)
items.append(vCardActivity)
return items
}
如何使用?
if let shareObject = self.activityItems(latitude: 52.033809, longitude: 6.882286) {
//open UIActivityViewController
}
参考链接:第三方应用程序将不会显示在模拟器中(不包括安装在模拟器上的应用程序)。试着在真正的设备上运行你的代码。我在自己的iPhone上运行了它,但仍然不推荐像Apple Maps、Google Maps或Waze这样的导航应用程序。。有什么建议吗@你找到解决方案了吗?你从未将任何数据(vcard字符串)写入URL。
if let shareObject = self.activityItems(latitude: 52.033809, longitude: 6.882286) {
//open UIActivityViewController
}