I want to create a kml file programmatically in iOS Swift4. I surfed a lot regarding offline GoogleMaps & saving data in kml file, but I haven't found any relevant material regarding this. In short, I just want to store some POI in kml & display it in offline Googlemap!!
KML is an XML document scheme. You can write it in any way you see fit. Use a dedicated XML encoder or brute force using strings.
The full spec is documented at https://developers.google.com/kml/documentation/kmlreference
You may find examining existing KML files useful to show example structures.
Here is an example brute force encoder to encode a set of labeled placemarks…
Raw strings will need to be XML escaped before writing to file.
You may find Google Toolbox for Mac useful here.
class KMLWriter {
func createKMLFormat(_ logs:[DDBLogEntry])->String {
var kml = preamble()
for log in logs {
kml += placemarkForLog(log)
}
terminate(&kml)
return kml
}
func placemarkForLog(_ log:DDBLogEntry)->String {
let name = log.userContent ?? "empty"
let time = log.dateCreated ?? Date()
let timeStamp = Date.UTCDateFormatter.string(from: time)
return "<Placemark>\n<name>\(name)</name>\n<Point><coordinates>\(log.longitude ?? 0),\(log.latitude ?? 0),\(log.elevation ?? 0)</coordinates></Point>\n<TimeStamp><when>\(timeStamp)</when></TimeStamp>\n</Placemark>\n"
}
func preamble()->String {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document>\n"
}
func terminate(_ kml:inout String) {
kml += "</Document>\n</kml>\n"
}
}
Related
Kind of new to Swift in general, but I'm trying to make a simple RAW camera app for fun. Apple's documentation says that to configure a photo output, you do
let query = photoOutput.isAppleProRAWEnabled ?
{ AVCapturePhotoOutput.isAppleProRAWPixelFormat($0) } :
{ AVCapturePhotoOutput.isBayerRAWPixelFormat($0) }
// Retrieve the RAW format, favoring Apple ProRAW when enabled.
guard let rawFormat =
photoOutput.availableRawPhotoPixelFormatTypes.first(where: query) else {
fatalError("No RAW format found.")
}
but I've been getting an error with the first let statement which says "'isAppleProRAWEnabled' is only available in iOS 14.3 or newer." Is there any way to force it to check for ProRaw, even not on iOS 14.3? I'm not even interested in using ProRaw, but I can't figure out how to get rid of the check and just select the classic RAW format (which I think is the bayer format). If anyone knows a workaround, that would be great!
You can query for the Bayer RAW format as below:
let rawFormatQuery = {AVCapturePhotoOutput.isBayerRAWPixelFormat($0)}
guard let rawFormat = photoOutput.availableRawPhotoPixelFormatTypes.first(where: rawFormatQuery) else {
fatalError("No RAW format found.")
}
Then you set your photo settings using the raw format:
let photoSettings = AVCapturePhotoSettings(rawPixelFormatType: rawFormat,
processedFormat: processedFormat)
Finally, you call your capture delegate as described in the Apple documentation (which I think is where you got the code above).
https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/capturing_photos_in_raw_and_apple_proraw_formats
I have an Action Extension to which I'm trying to share PDF-files.
I'm using the boilerplate code for ActionRequestHandler.swift that was autogenerated for me:
func beginRequest(with context: NSExtensionContext) {
// Do not call super in an Action extension with no user interface
self.extensionContext = context
for item in context.inputItems as! [NSExtensionItem] {
if let attachments = item.attachments {
for itemProvider in attachments {
...
...
}
}
}
}
Working from other apps
When exporting from every application except Safari, this is what I get:
This is all ok, I can verify that it's an pdf by checking the com.adobe.pdf and then I use the public.file-url to fetch the shared file.
Failing from Safari
But when exporting from Safari (doesn't matter if I choose "Automatic" or "Pdf" for file type), I instead only get com.apple.property-list:
Further info
Both dropbox and OneDrive works, so it's doable in some sort of way.
Also I realised that sharing an PDF from a url that's protected by some sort of login doesn't work with "Public.file-url" since that URL wont be accessible from inside swift-code.
That leads me to think that the java-script preprocessor might be the way to go? Fetch the pdf-contents with JS and pass it on to code?
Question
How do I use the com.apple.property-list to fetch the file?
Or is some config I did faulty, since I get this property-list instead of the pdf/url combo?
While I didn't manage to figure out a solution to the original question, I did manage to solve the problem.
When adding an Action Extension, one gets to choose Action type:
Presents user interface
No user interface
I choosed No user interfacesince that was what I wanted.
That gave me an Action.js file and ActionRequestHandler.swift:
class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
...
}
These files seem to work around a system where the Action.js is supposed to fetch/manipulate the source page and then send information to the backing Swift code. As stated in my original question, when sharing a PDF from Safari, no PDF-URL gets attached.
A working solution
If I instead choose Presents user interface, I got another setup, ActionViewController.swift:
class ActionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Get the item[s] we're handling from the extension context.
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
for provider in item.attachments! {
if provider.hasItemConformingToTypeIdentifier(kUTTypePDF as String) {
provider.loadItem(forTypeIdentifier: kUTTypePDF as String, options: nil, completionHandler: { (pdfUrl, error) in
OperationQueue.main.addOperation {
if let pdfUrl = pdfUrl as? URL {
// pdfUrl now contains the path to the shared pdf data
}
}
}
}
}
This file / solution works as expected, the extensionContext gets populated with one attachment that conforms to kUTTypePDF as expected.
Why this works, while the "no gui"-approach doesn't, I have no idea. Bug or feature?
I have not found any documentation of how/why this is supposed to work in Apple's developer section, the "share extension" documentation is very light.
I am trying to store 30k users details in to core data. To achieve this I search and came up with a solution to have all the data in a file in CSV format.
I am able to download and read CSV file to using following code:
func readCsvFile () {
if let path = Bundle.main.path(forResource: "users", ofType: "csv") {
if FileManager.default.fileExists(atPath: path){
if let fileHandler = FileHandle(forReadingAtPath: path){
if let data = fileHandler.readDataToEndOfFile() as? Data{
if let dataStr = String(data: data, encoding: .utf8){
print(dataStr)
}
}
}
}
}
}
but now the problem is that when i read entire data from file it causes memory issue. I need to read some portion and process core data storing. Again get back and continue data read from where I left and go on.
My file has data as following:
Firstname,Lastname,Email,Phone,Title
Tanja,van Vlissingen-ten Brink,1,,Vulr(st)er
Berdien,Huismn Noornnen,2,,Filanager
Ailma,Ankit,3,,Vulr(st)er
Rzita,Salmani Samani,4,,Vulr(st)er
DeEora,Levaart,5,,Eerste Vulr(st)er
Kirsten,Veroor,6,,Vulr(st)er
Tristan,Haenbol,7,,Vulr(st)er
Manon,Bland,8,,Aankomend Vulrer
Naomi,Ruman,9,,Aankomend Vulrer
So I found that using NSFileHandler I can move or seek my file cursor to specific location in file, but it needs offset:
fileHandler = FileHandle(forReadingAtPath: path)
fileHandler.seek(toFileOffset: 10)
but I don't know how can I identify that I have read some specific number of lines, and now get back to next set of lines.
Also NSStream is the way to read File but I haven't explored it.
I'm creating a reading list app, and I'd like to pass the read time of a user added link to a table cell in their reading list - and the only way to get that number is from that page's word count. I've found a few solutions, namely Parsehub, Parse and Mercury but they seem to be geared more towards use cases that need more advanced things to be scraped from a url. Is there a simpler way in Swift to calculate word count of a url?
First of all, you need to parse the HTML. HTML can only be parsed reliably with dedicated HTML parser. Please don't use Regular Expressions or any other search method to parse HTML. You may read it why from this link. If you are using swift, you may try Fuzi or Kanna. After you get the body text with any one of the library, you have to remove extra white spaces and count the words. I have written some basic code with Fuzi library for you to get started.
import Fuzi
// Trim
func trim(src:String) -> String {
return src.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
// Remove Extra double spaces and new lines
func clean(src:String) ->String {
return src.replacingOccurrences(
of: "\\s+",
with: " ",
options: .regularExpression)
}
let htmlUrl = URL(fileURLWithPath: ((#file as NSString).deletingLastPathComponent as NSString).appendingPathComponent("test.html"))
do {
let data = try Data(contentsOf: htmlUrl)
let document = try HTMLDocument(data: data)
// get body of text
if let body = document.xpath("//body").first?.stringValue {
let cleanBody = clean(src: body)
let trimmedBody = trim(src:cleanBody)
print(trimmedBody.components(separatedBy: " ").count)
}
} catch {
print(error)
}
If you are fancy, you may change my global functions to String extension or you can combine them in a single function. I wrote it for clarity.
I'm working on a iOS project with Swift on Xcode, and i need to show a map with some routes drawn on it. I created the map view using Google Maps SDK for iOS and it works well. I use KML.swift to parse the KML file.
The problem is that i want to load the routes for the map from a KML and i don't see how to do it. On Android, the SDK allows you to add a layer from a KML and add that layer to the map easy (more or less), but i can't find the way to do something like that on the iOS SDK.
Is there any way to do it?
Thanks in advance.
You can now do so with GoogleMapsUtils. The KML file can be parsed using GMUKMLParser and rendered with GMSMapView.
Create instances of GMSMapView and GMUKMLParser
let mapView: GMSMapView!
let kmlParser: GMUKMLParser!
func renderKml() {
guard let path = Bundle.main.path(forResource: "uae_map", ofType: "kml") else {
print("Invalid path")
return
}
let url = URL(fileURLWithPath: path)
kmlParser = GMUKMLParser(url: url)
kmlParser.parse()
let renderer = GMUGeometryRenderer(
map: mapView,
geometries: kmlParser.placemarks,
styles: kmlParser.styles,
styleMaps: kmlParser.styleMaps
)
renderer.render()
}