Post

Replies

Boosts

Views

Activity

MultipeerConnectivity in SwiftUI?
I tried now two different approaches (MCAdvertiserAssistant / MCBrowserViewController and MCNearbyServiceAdvertiser / MCNearbyServiceBrowser) to implement this in my first Swift app but always got the result that it stays at "Connecting" for a few seconds and than just declines the request. In a video from Hacking with Swift there was a popup if the connection should be accepted - this never comes up on my devices. So maybe this is the error already? As for the browser part, I use this ViewController wrapper to make it available: import MultipeerConnectivity import SwiftUI struct MultipeerConnectivityBrowserView: UIViewControllerRepresentable {   func makeUIViewController(context: UIViewControllerRepresentableContext<MultipeerConnectivityBrowserView>) -> MCBrowserViewController {     let service = MultipeerConnectivityService.instance()     guard let session = service.session else {       print("Failed to load MCSession for Browser")       return MCBrowserViewController()     }           let viewController = MCBrowserViewController(serviceType: service.serviceType, session: session)     viewController.delegate = service           return viewController   }   func updateUIViewController(_ uiViewController: MCBrowserViewController, context: UIViewControllerRepresentableContext<MultipeerConnectivityBrowserView>) {   } } struct MultipeerConnectivityBrowserView_Previews: PreviewProvider {   static var previews: some View {     MultipeerConnectivityBrowserView()   } } I can see my 2nd device in the upcoming dialog but when I click on it, as described above, it just hangs at "Connecting" until it, as it seems to me, timed out. This is currently the main implementation for the MC in my app: import MultipeerConnectivity class MultipeerConnectivityService: NSObject, MCSessionDelegate, MCBrowserViewControllerDelegate, MCAdvertiserAssistantDelegate { 		private static var internalInstance: MultipeerConnectivityService? = nil 		 		let serviceType = "sn-dueladviser" 		let peerID = MCPeerID(displayName: UIDevice.current.name) 		 		var session: MCSession? 		var advertiserAssistant: MCAdvertiserAssistant? 		 		static func instance() -> MultipeerConnectivityService { 				if let instance = internalInstance { 						return instance 				} 				 				internalInstance = MultipeerConnectivityService() 				return internalInstance! 		} 		 		private override init() { 				super.init() 				 				session = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .required) 				session?.delegate = self 		} 		 		func startHosting() { 				guard let session = session else { return } 				advertiserAssistant = MCAdvertiserAssistant(serviceType: serviceType, discoveryInfo: nil, session: session) 				advertiserAssistant?.delegate = self 				 				advertiserAssistant?.start() 		} 		 		func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) { 				 		} 		 		func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) { 				 		} 		 		func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) { 				 		} 		 		func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) { 				browserViewController.dismiss(animated: true, completion: { }) 		} 		 		func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) { 				browserViewController.dismiss(animated: true, completion: { }) 		} 		 		func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) { 				switch state { 				case .connected: 						print("Connected: \(peerID.displayName)") 				case .connecting: 						print("Connecting: \(peerID.displayName)") 				case .notConnected: 						print("Not Connected: \(peerID.displayName)") 						 				@unknown default: 						print("Unknown state received: \(peerID.displayName)") 				} 		} 		 		func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { 				//todo: Receive data 		} } I tried to add NSLocalNetworkUsageDescription and NSBonjourServices without any change (even if both are not mentioned in any guides except the official documentation). Sadly it didn't change anything + it seems like my XCode does not know these keys or at least do not replace the key name with another text like it did with other keys like camera permission.
4
0
3.1k
Jul ’20
How to execute code without the app open?
I want to create a app that has to load data from my API on a regular basis. The data is changed every 15 minutes so I would like to use a similar interval to load my data to update my widget and the data in the app. But what is the proper way of implementing such behavior? I mean the app would need to update the data even when the app is closed in the app switcher to update the widget and so on. Background fetch seems to be the wrong way to me, and I'm also not sure about BGTaskScheduler. As far as I understand this, both needs to be started from my app which may not be opened after a device reboot. I'm sure there must be a way to implement the wanted behavior because mail apps need todo the very same on servers that do not support push.
0
0
617
Sep ’20
Using UNNotificationServiceExtension with FCM not getting called
I don't know what could be wrong but my UNNotificationServiceExtension is never getting called by any push from FCM. I call the FCM from a Python3 script and so far it works (I get a push notification but always with the default text and not the modified one). firebaseHeaders = { "Content-Type":"application/json", "Authorization":"key=MYKEY" } firebaseBody = { "to":devicetoken, "priority":"high", "mutable-content":True, "apns-priority":5, "notification": { "titlelockey":"push.goalReachedTitle", "bodylockey":"push.goalReachedContentRemote", "bodylocargs":[str(entry['value']), entry['region']], "sound":"default", "badge":1 }, "data": { "values":data, "region":entry['region'], "value":entry['value'] } } firebaseResult = requests.post("https://fcm.googleapis.com/fcm/send", data=None, json=firebaseBody, headers=firebaseHeaders) My Extension is also pretty basic (for testing) and I already tried to remove it and add it again to my main app project without success. class NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) if let bestAttemptContent = bestAttemptContent { bestAttemptContent.title = bestAttemptContent.title + " [TEST 123]" contentHandler(bestAttemptContent) } } override func serviceExtensionTimeWillExpire() { // Called just before the extension will be terminated by the system. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } } Anyone has a idea what I still can check, test or miss?
7
2
7.8k
Oct ’20
UIViewControllerRepresentable getting size?
Is there a way to get the size inside a UIViewControllerRepresentable? I try to include a UIKit control that requires me to put the size in it's constructor. This is my code so far: final class ChartVC: UIViewControllerRepresentable {       func makeUIViewController(context: Context) -> UIViewController {     let viewController = UIViewController()           let chart = Chart(frame: CGRect(x: 0, y: 0, width: 300, height: 200)) //todo: don't use fixed values here...           viewController.view.addSubview(chart)     return viewController   }       func updateUIViewController(_ uiViewController: UIViewController, context: Context) { } } And this is, how I currently use this: ChartVC()   .frame(minWidth: 180, idealWidth: 280, maxWidth: 420, minHeight: 300, alignment: .center)   .padding()   .background(Color.contentBackgroundColor)   .cornerRadius(8) Question now is, how do I get the size that SwiftUI will apply to my frame?
0
0
1.6k
Oct ’20
Ignoring CKSubscription for record changes from own save
Is there any way to prevent the CKSubscription from getting invoked if the change that invoked it comes from this app instance? My app updates the CloudKit after every change but if the user does this changes quickly, there is a high chance that the subscription get back to me with outdated data even tho this change came from this very app instance. I still want this update to happen if these changes are done from a different app instance on another device or what so ever.
0
0
536
Jun ’23
Xcode won't download manual provisioning profles
Since a few days now, I'm using Xcode 16 and I try to install a newly made provisioning profile for my app. On the development profile I always get the message "Failed to install one or more provisioning profiles on the device". But no message at all for the distribution profile. I even tried setting up completely new certificates as I updated to macOS 15 and Xcode 16. No success. When checking the installed profiles via Finder under open ~/Library/MobileDevice/Provisioning\ Profiles I only see profiles from my last project from june this year. It does not matter how often I press "Download Manual Profiles" in Xcode nothing new lands here. Is there any other way to install newly created profiles? As my project is done with .NET and not a native Xcode project I do not have the option to let Xcode it generate for me (if this would even work).
1
0
1.5k
Sep ’24