Has anyone been able to successfully create a connection between watchOS and macOS, such as using an Apple Watch to unlock a Mac? I know there's WC Session between iOS and WatchOS and then there's nearby interaction between two iOS devices. However, I want to know if any of you have been successfully able to do any kind fo interaction between watchOS and macOS.
For instance, here it says that Apple uses Wifi technology to approximate the distance between the mac and the watch: https://security.stackexchange.com/questions/158100/how-does-unlock-your-mac-with-apple-watch-work-what-should-i-consider-in-the
But I cannot find any concrete SwiftUI approach on it. Would appreciate if you know if any kits or methods to achieve any sort of interaction between the watch and the computer.
Post
Replies
Boosts
Views
Activity
So I'll keep it short. AS the title says I have an a web view embedded in my SwiftUI application for macOS and there's a link on the website which downloads a .doc file. However WKWebView doesn't really let you know download stuff from the internet like that. I looked around and tried the WKDownloadDelegate solution. However it gives me and error that Loading Error (Frame load interrupted). Does anyone know how can I simply download a normal text file from a website onto the Mac using WKWebView in SwiftUI?
Here's my WebView code:
import SwiftUI
import WebKit
import Combine
class WebViewData: ObservableObject {
@Published var loading: Bool = false
@Published var url: URL?;
var popupWebView: WKWebView?
init (url: URL) {
self.url = url
}
}
@available(OSX 11.0, *)
struct WebView: NSViewRepresentable {
@ObservedObject var data: WebViewData
func makeNSView(context: Context) -> WKWebView {
return context.coordinator.webView
}
func updateNSView(_ nsView: WKWebView, context: Context) {
guard context.coordinator.loadedUrl != data.url else { return }
context.coordinator.loadedUrl = data.url
if let url = data.url {
DispatchQueue.main.async {
let request = URLRequest(url: url)
nsView.load(request)
}
}
context.coordinator.data.url = data.url
}
func makeCoordinator() -> WebViewCoordinator {
return WebViewCoordinator(data: data)
}
}
@available(OSX 11.0, *)
class WebViewCoordinator: NSObject, WKNavigationDelegate {
@ObservedObject var data: WebViewData
var webView: WKWebView = WKWebView()
var loadedUrl: URL? = nil
init(data: WebViewData) {
self.data = data
super.init()
webView.navigationDelegate = self
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DispatchQueue.main.async {
self.data.loading = false
}
}
// early decision per URL
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.request.url?.pathExtension == ".doc" {
decisionHandler(.download)
} else {
decisionHandler(.allow)
}
}
// when known response
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
if navigationResponse.response.mimeType == "application/word" {
decisionHandler(.download)
} else {
decisionHandler(.allow)
}
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
DispatchQueue.main.async { self.data.loading = true }
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
showError(title: "Navigation Error", message: error.localizedDescription)
DispatchQueue.main.async { self.data.loading = false }
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
showError(title: "Loading Error", message: error.localizedDescription)
DispatchQueue.main.async { self.data.loading = false }
}
func showError(title: String, message: String) {
#if os(macOS)
let alert: NSAlert = NSAlert()
alert.messageText = title
alert.informativeText = message
alert.alertStyle = .warning
alert.runModal()
#else
print("\(title): \(message)")
#endif
}
}
Here's my Content View
@available(OSX 11.0, *)
struct ContentView: View {
private var url: URL? = URL(string: "https://mywebsite.com")
init() {
print("Hello World")
}
var body: some View {
WebView(data: WebViewData(url: self.url!))
.frame(minWidth: 1400, idealWidth: 1500, minHeight: 850, idealHeight: 850)
}
}
Also here's how my website downloads the files using JS in case anyone's interested:
var sourceHTML = document.getElementById("notes").innerHTML;
var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
var fileDownload = document.createElement("a");
document.body.appendChild(fileDownload);
fileDownload.href = source;
var lines = $('#notes').val().split('.');//gives all lines
const firstLine=lines[0];
console.log(firstLine)
fileDownload.download = firstLine + ".doc";
fileDownload.click();
document.body.removeChild(fileDownload);
}
Hey all. So I'll keep it short. I registered by App Service ID and Key and everything. I even enabled Apple Sign in from Firebase and got sign in with Apple to work on my Swift iOS app. Now I want it to work on my web app via Vanilla Javascript. When I run the following code on my frontend after initiating firebase, I don't get anything. Like absolutely no error in console or any kind of pop up. I'd appreciate if someone could tell me what I'm doing wrong. Thanks in advance!
const provider = new firebase.auth.OAuthProvider('apple.com');
firebase.auth().signInWithPopup(provider).then((result) => {
/** @type {firebase.auth.OAuthCredential} */
var credential = result.credential;
// The signed-in user info.
var user = result.user;
// You can also get the Apple OAuth Access and ID Tokens.
var accessToken = credential.accessToken;
var idToken = credential.idToken;
// ...
})
.catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
// Result from Redirect auth flow.
firebase.auth().getRedirectResult().then((result) => {
if (result.credential) {
/** @type {firebase.auth.OAuthCredential} */
var credential = result.credential;
// You can get the Apple OAuth Access and ID Tokens.
var accessToken = credential.accessToken;
var idToken = credential.idToken;
// ...
}
// The signed-in user info.
var user = result.user;
})
.catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
}
Hi there, I'm in the process of sending a private beta for my app. It's essentially a note taking app that uses Firebase as back end and uses CryptoKit for 'Sign In With Apple'. Since I'm using these two things, do I need to answer Yes to all the questions? I'm really confused as to what's the best approach to this.
So I have a ViewA which returns a value called idd after defining a function addNote() and I wish to use that idd value in ViewB. I'm able to define idd in ViewB as viewA.addNote() however I'm not able to get the value of idd passed to viewB. And even Xcode says that my result from addNote() us unused. I'm new to swiftUI and learning everyday so I'd appreciate if you could let me know how to approach this problem. Thanks!
viewA Code:
class viewA: ObservableObject{
@Published var idd: String = ""
func addNote()-> String{
// some Code
return idd
}
}
viewB Code:
struct ViewB: View {
@StateObject var viewTwo = viewTwo()
@State var idd = String()
var body: some View {
NavigationView {
ZStack{
.navigationTitle("Notes")
.navigationBarItems(
trailing: Button (action: {},
label: { NavigationLink(destination: NoteView(newNote: "", idd: viewA.addNote() ) )
{Image(systemName: "plus")} }
))
.listStyle(.plain)
.buttonStyle(PlainButtonStyle())
}
}
I have a Function addNote() that returns a value called 'idd' in the form of string
Now I have a view called ListView where there is a navigationLink that essentially needs that returned 'idd' value to be passed to this view called NoteView which is that navigationLink's destination. However I'm not able to pass that value down to NoteView.
I've been struggling with this for days now, I'd appreciate if someone could give me their two cents! Thanks a ton.
here’s the listview code:
`
structure ListView: View {
@StateObject var dataManager = DataManager()
@State var isPresented = false
var body: some View {
NavigationView {
ZStack{
List(dataManager.notes) { note in
NavigationLink(destination: NoteView(newNote: note.content, idd: note.id)) {
EmptyView()
Text(note.content)}.buttonStyle(PlainButtonStyle())
.frame(height: 0)
.navigationTitle("Notes")
.navigationBarItems(
trailing: Button (action: {},
label: {
NavigationLink(destination: NoteView(newNote: "", idd: "") )
{Image(systemName: "plus")}
.simultaneousGesture(TapGesture().onEnded{
dataManager.addNote()
})}
))
.listStyle(.plain)
.buttonStyle(PlainButtonStyle()) ‘
here’s the idd that addNote() returns:
`class DataManager : ObservableObject {
@Published var notes: [Note] = []
@Published var idd = ""
func addNote()-> String{
let user = Auth.auth().currentUser
let uid = Auth.auth().currentUser!.uid
let db = Firestore.firestore()
var ref: DocumentReference? = nil
ref = db.collection("userslist").document(uid).collection("actualnotes").addDocument(data: ["content":"New Note"])
{error in
if let error = error{
print(error.localizedDescription)
}
else {
print("Document added with ID: \(ref!.documentID)")
}
}
let idd = ref!.documentID
return idd
}`