How to receive image dragged from Safari webpage with SwiftUI `.onDrop()` - swift

I'm not able to receive image dragged from Safari webpage in my SwiftUI app on macOS. I suppose Safari uses different mechanics, because it works properly when it comes to Chrome and Firefox. I use .onDrop() but it results empty providers no matter what kind of UTType used. Below is my code but output no provider available.
So, what UTType it is when dragging image from Safari webpage, and how can I implement it in .onDrop()
.onDrop(of: [.url, .fileURL], isTargeted: $isTarget) { providers -> Bool in
if let provider = providers.first,
let identifier = provider.registeredTypeIdentifiers.first {
print(identifier)
provider.loadItem(forTypeIdentifier: identifier) { data, error in
// Do something here
}
} else {
print("no provider available")
}
return true
}

Related

How to implement SwiftUI’s .onDrag modifier with NSImage (macOS)

I am building a sandboxed app for macOS with SwiftUI. I have a NSImage that is displayed as Image(nsImage: myNSImage). I want that View to support drag and drop, meaning that it can be dragged to any location that can receive image files.
Here’s my approach:
Image(nsImage: myNSImage)
.onDrag({
let itemProvider = NSItemProvider()
itemProvider.suggestedName = "image.png"
itemProvider.registerDataRepresentation(for: UTType.png) {
loadHandler in
loadHandler(nsImage.pngRepresentation, nil)
print("loadHandler completed") // Never prints !!
return nil
}
return itemProvider
})
This way I can drag the View. But it seems like it isn’t able to provide the image.
The “drop”, i.e. on the Desktop, is simply not working. I would expect that it saves an "image.png" in the destination URL, but it doesn’t.
How can I implement .onDrag so that it provides a image file based on NSImage?
Edit: I already have tried different UTTypes, for example .tiff in combination with nsImage.tiffRepresentation without luck.

turn-by-turn navigation with google maps url scheme swift

How can I enable turn-by-turn navigation with google maps urlscheme?. I used below url in swift
comgooglemaps://?saddr=%f,%f&daddr=%f,%f&directionsmode=driving
but it only creates a route, the start navigation button is not present when the google maps app is activated.
am i missing something?
let kGoogleMapsSchema = "comgooglemaps://"
let path = "\(kGoogleMapsSchema)?saddr=%f,%f&daddr=%f,%f&directionsmode=driving"
guard let finalURLString = String(format: path, currentUserCoordinates!.latitude, currentUserCoordinates!.longitude, destinationCoordinates!.latitude, destinationCoordinates!.longitude, currentUserCoordinates!.latitude, currentUserCoordinates!.longitude).addingPercentEncoding(
withAllowedCharacters: .urlQueryAllowed) else {
return
}
guard let url = URL(string: finalURLString) else {
return
}
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
am i missing something?
I found the answer as only preview button was available instead of start navigation. Because I was giving start coordinates not of my current location. So in case it is my current coordinates it will work.
Google Maps, No Option for Starting the Navigation, Only Preview is there

How to create apple watchOS5 complication?

I've never worked in WatchOS5 and want to develop a horizontal complication (Modular large) for AppleWatch, like "Heart Rate". The idea is that I would display heart rate data in a different way. Right now I want to deploy the complication on development watch.
I have created a new project with a checkbox for "complication" added. I see that this added a complications controller with timeline configuration placeholders.
There is also an storyboard with a bunch of empty screens. I'm not sure as to how much effort I need to put into an apple watch app before I can deploy it. I see this Apple doc, but it does not describe how to layout my complication. Some section seem to have missing links.
Can I provide one style of complication only (large horizontal - modular large)
Do I need to provide any iPhone app content beyond managing the
complication logic, or can I get away without having a view controller?
Do I control the appearance of my complication by adding something to the assets folder (it has a bunch of graphic slots)?
Sorry for a complete beginner project, I have not seen a project focusing specifically on the horizontal complication for watch OS 5
You should be able to deploy it immediately, though it won't do anything. Have a look at the wwdc video explaining how to create a complication: video
You can't layout the complication yourself, you can chose from a set of templates that you fill with data. The screens you are seeing are for your watch app, not the complication.
You don't have to support all complication styles.
The complication logic is part of your WatchKit Extension, so technically you don't need anything in the iOS companion app, I'm not sure how much functionality you have to provide to get past the app review though.
Adding your graphics to the asset catalog won't do anything, you have to reference them when configuring the templates.
Here's an example by Apple of how to communicate with the apple watch app. You need to painstakingly read the readme about 25 times to get all the app group identifiers changed in that project.
Your main phone app assets are not visible to the watch app
Your watch storyboard assets go in WatchKit target
Your programmatically accessed assets go into the watch extension target
Original answers:
Can I provide one style of complication only (large horizontal -
modular large) - YES
Do I need to provide any iPhone app content beyond
managing the complication logic, or can I get away without having a
view controller? YES - watch apps have computation limits imposed on them
Do I control the appearance of my complication by
adding something to the assets folder (it has a bunch of graphic
slots)? See below - it's both assets folder and placeholders
Modify the example above to create a placeholder image displayed on the watch (when you are selecting a complication while modifying the screen layout)
func getPlaceholderTemplate(for complication: CLKComplication, withHandler handler: #escaping (CLKComplicationTemplate?) -> Void) {
// Pass the template to ClockKit.
if complication.family == .graphicRectangular {
// Display a random number string on the body.
let template = CLKComplicationTemplateGraphicRectangularLargeImage()
template.textProvider = CLKSimpleTextProvider(text: "---")
let image = UIImage(named: "imageFromWatchExtensionAssets") ?? UIImage()
template.imageProvider = CLKFullColorImageProvider(fullColorImage: image)
// Pass the entry to ClockKit.
handler(template)
}else {
handler(nil);
return
}
}
sending small packets to the watch (will not send images!)
func updateHeartRate(with sample: HKQuantitySample){
let context: [String: Any] = ["title": "String from phone"]
do {
try WCSession.default.updateApplicationContext(context)
} catch {
print("Failed to transmit app context")
}
}
Transferring images and files:
func uploadImage(_ image: UIImage, name: String, title: String = "") {
let data: Data? = UIImagePNGRepresentation(image)
do {
let fileManager = FileManager.default
let documentDirectory = try fileManager.url(for: .cachesDirectory,
in: .userDomainMask,
appropriateFor:nil,
create:true)
let fileURL = try FileManager.fileURL("\(name).png")
if fileManager.fileExists(atPath: fileURL.path) {
try fileManager.removeItem(at: fileURL)
try data?.write(to: fileURL, options: Data.WritingOptions.atomic)
} else {
try data?.write(to: fileURL, options: Data.WritingOptions.atomic)
}
if WCSession.default.activationState != .activated {
print("session not activated")
}
fileTransfer = WCSession.default.transferFile(fileURL, metadata: ["name":name, "title": title])
}
catch {
print(error)
}
print("Completed transfer \(name)")
}

Disable the loading of Stylesheets in UIWebView and/or WKWebView

I have absolutely tried everything with this one. I've read every apple article and no where can I find how to disable the loading of CSS (Styling) in the legacy UIWebView or the new WKWebView. I don't mind what web view I use just as long as it can accomplish this.
I've tried WKPreferences() and WKWebViewConfiguration and both have no member userStyleSheetEnabled.
I've referred myself to this apple article https://developer.apple.com/documentation/webkit/webpreferences/1536396-userstylesheetenabled?
Does anyone know the answer and how to achieve this on Swift 4?
The WebView class you referenced is very old and has been deprecated. If you need to add a webview to your app, use WKWebView instead. This answer works with iOS >= 11 and macOS >= 10.13.
What you need is adding WKContentRuleList to your WKWebView's configuration. They are similar to Safari content blockers (i.e. ad-blockers) that you may already have installed on your phone:
// This should ideally be in a file but we're using string for convenience
let jsonRuleList = """
[{
"trigger": {
"url-filter": ".*",
"resource-type": ["style-sheet"]
},
"action": {
"type": "block"
}
}]
"""
// Compile the content-blocking list
WKContentRuleListStore.default().compileContentRuleList(forIdentifier: "blockStyleSheet", encodedContentRuleList: jsonRuleList) { list, error in
guard error == nil else { print(error!.localizedDescription); return }
guard let list = list else { return }
// Add the stylesheet-blocker to your webview's configuration
let configuration = WKWebViewConfiguration()
configuration.userContentController.add(list)
// Adding the webview to the view. Nothing to see here
self.webView = WKWebView(frame: self.view.bounds, configuration: configuration)
self.view.addSubview(self.webView)
// Let's try Apple's website without CSS
let request = URLRequest(url: URL(string: "https://www.apple.com")!)
self.webView.load(request)
}
Result:
References
Customized Loading in WKWebView (WWDC video)
Creating Safari Content-Blocking Rules

How to load google street view in a UIWebView of iphone app?

I am trying to use the google map street view in my app, so i am trying to load the html file in my Webview. but it is need the flash player to show the street view.
Is adobe flash player is required for this?
Anyone know how to use the street view in our iphone app?
Regards,
Yes, it is possible to load Google StreetView in UIWebView. Here is a sample code example to load Google StreetView -
let urlAddress = "http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=bran+castle&sll=44.439972,26.096894&sspn=0.000869,0.003664&ie=UTF8&hq=Bran+Castle&hnear=Bran+Castle,+Strada+General+Traian+Mo%C8%99oiu+nr.+28+E574,+Bran+507025,+Romania&t=k&layer=c&cbll=45.516381,25.368123&panoid=0d7jjq7vdWGTxFyonEKnBQ&cbp=12,209.81,,1,-18.52&ll=45.516381,25.368123&spn=0,0.008444&z=17";
let request = NSURLRequest(URL: NSURL(string: urlAddress)!)
glWebView.loadRequest(request)
After loading this streetView. Google will give you an alert that this URL is belongs to google map only. To hide that alert message just add this below javascript query in your WebViewDidFinish method -
webView.stringByEvaluatingJavaScriptFromString("document.getElementsByClassName('ml-unsupported-link-dialog-container')[0].style.display='none'")
Well technically it is not possible, but if you can get the panorama image id for that specific latitude and longitude, then it is possible. In my previous answer, I said it is possible as because I know the panorama id for that specific location. Using that code you can open steetview in your UIWebview, but it will show a warning. By touching anywhere in your webview will disappear that warning alert.
Bu Google introduce there SDK to load streetview in your iOS app, instead of leaving your app. All you need to do is add 'GoogleMaps' sdk using cocoa pods and run this below source code to display google Streetview for specific latitude and longitude.
let panoSvc = GMSPanoramaService()
panoSvc.requestPanoramaNearCoordinate(CLLocationCoordinate2D(latitude: 32.847683, longitude: -96.771638), radius:100, callback: { (panorama, error) in
if error == nil {
DispatchQueue.main.async(execute: {
let panoView = GMSPanoramaView(frame: .zero)
self.view = panoView
panoView.moveNearCoordinate((panorama?.coordinate)!, radius: 200)
print("Panorama ID: ", panorama?.panoramaID)
print("Panorama Co-ordinate: ", panorama?.coordinate)
})
}
else {
Utility.showCustomMsg(self.view, label: "Not Found", detailslbl:"Street View is not available for this location.", isSuccessImg: false, duration: 3)
{
self.navigationController?.popViewController(animated: true)
}
}
})
Cool huh...... Thanks goes to Google for their cool staff.