Post

Replies

Boosts

Views

Activity

Fragment large size data sent and received using NSKeyedArchiver.archivedData in GameCenter
Trying to send and receive data in the GameCenter environment using the following methods: func sendData(dictionaryWithData dictionary: Dictionary<String, Any>,toPeer targetPeers: [GKPlayer]) { guard let match = self.match else { return } do { let dataToSend = try NSKeyedArchiver.archivedData(withRootObject: dictionary, requiringSecureCoding: false) try match.send(dataToSend, to: targetPeers, dataMode: .reliable) } catch { #if DEBUG print("CONNECTION MANAGER SEND DATA ERROR") #endif } } public func match(_ theMatch: GKMatch,didReceive data: Data,forRecipient recipient: GKPlayer,fromRemotePlayer player: GKPlayer) { if match != theMatch { return } DispatchQueue.main.async { do { guard let message = NSDictionary.unsecureUnarchived(from: data) as? Dictionary<String, Any> else {return} ... <CODE> ... } ///Source: https://stackoverflow.com/questions/51487622/unarchive-array-with-nskeyedunarchiver-unarchivedobjectofclassfrom static func unsecureUnarchived(from data: Data) -> Self? { do { let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data) unarchiver.requiresSecureCoding = false let obj = unarchiver.decodeObject(of: self, forKey: NSKeyedArchiveRootObjectKey) if let error = unarchiver.error { print("Error:\(error)") } return obj } catch { print("Error:\(error)") } return nil } Everything works great until the data exceeds 87K (which, I understand, is the limit for exchanging data in GameCenter). The data is not sent and gives the following error: Async message[1FCA0D11-05DE-47D0-9714-983C8023F5C1] send error: FailedToSendData: , InternalError: reliable, maxPayloadSizeExceeded Interesting enough, I do not have this problem when using MCSession, as follows, even if data exceeds 87K: func sendData(dictionaryWithData dictionary: Dictionary<String, Any>, toPeer targetPeers: [MCPeerID]) { do { let dataToSend = try NSKeyedArchiver.archivedData(withRootObject: dictionary, requiringSecureCoding: false) try session.send(dataToSend, toPeers: targetPeers, with: MCSessionSendDataMode.reliable) } catch { #if DEBUG print("CONNECTION MANAGER SEND DATA ERROR") #endif } } I have been doing research and found that I need to fragment data and send and receive it in packages. But I could not find a good explanation how to do it. Any help would be appreciated!
5
0
722
Jul ’24
How to draw an arc in MKMapView?
Hello. I got another question.I need to draw half-circle on MKMapView knowing the center coordinates, start and end angles, and radius in nautical miles.I have subclassed MKOverlayPathRenderer:import UIKit import MapKit class IGAAcarsDrawArc: MKOverlayPathRenderer { let PI = 3.14159265 let radius : CGFloat = 10.0 var startAngle: CGFloat = 0 var endAngle: CGFloat = 3.14159 var latitude = 25.96728611 var longitude = -80.453019440000006 override func createPath() { let line = MKPolyline() let arcWidth: CGFloat = 5 let path = UIBezierPath(arcCenter: CGPointMake(CGFloat(latitude), CGFloat(longitude)), radius: self.radius, startAngle: startAngle, endAngle: endAngle, clockwise: true) path.lineWidth = arcWidth path.stroke() } }Now, it is not clear how do I use this to create MKPolyline and implement in mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay). Does anyone know how to draw an arc in MKMapView?Thanks a lot!
2
0
1.7k
Jun ’16
Remove only one polyline of several
Hello.I have two polylines on the map:var polylineRoute : MKGeodesicPolyline! var polylineFlight : MKGeodesicPolyline!I assign each of them a title and add them to the map like this (in different methods):let polyline = MKGeodesicPolyline(coordinates: &routeCoordinates, count: routeCoordinates.count) polyline.title = "route" self.mapView.addOverlay(polyline) self.polylineRoute = polylineandlet polyline = MKGeodesicPolyline(coordinates: &routeCoordinates, count: routeCoordinates.count) polyline.title = "flight" self.mapView.addOverlay(polyline) self.polylineFlight = polylineNow, when a specific action is triggered, I would like to remove the flight overlay and leave the route overlay intact.This does not work at all:func removeFlightPath() { self.mapView.removeOverlay(self.polylineFlight) self.polylineFlight = nil }The following works but removes both polylines:func removeFlightPath() { var overlays = mapView.overlays mapView.removeOverlays(overlays) }Is there a working way to remove only one polyline?Thanks a lot!
2
0
2.7k
Feb ’16
NSUserDefaults with tuples
Hello. I am using NSUserDefaults to save values, e.g.let defaultsLoad = NSUserDefaults.standardUserDefaults() // Strings string1 = defaultsLoad.stringForKey("String1") // Arrays let array1 = defaultsLoad.objectForKey("Array1") as? NSData if let array1 = array1 { self.array = NSKeyedUnarchiver.unarchiveObjectWithData(array1) as! [String] }My question is: how do I save and retrieve tuples in swift? I have a tuple variable(String,String,String)and an array of these tuples:[(String,String,String)]These do not work:let defaultsLoad = NSUserDefaults.standardUserDefaults() defaultsLoad.setObject(tuple1, forKey:"Tuple1") let tupleArrayData = NSKeyedArchiver.archivedDataWithRootObject(tupleArray) defaultsLoad.setObject(tupleArrayData, forKey: "TupleArray")let defaultsLoad = NSUserDefaults.standardUserDefaults() tuple1 = defaultsLoad.objectForKey("Tuple1") // This one does not show any error. The problem is with saving let tupleArrayData = defaultsLoad.objectForKey("TupleArray") as? NSData if let tupleArrayData = tupleArrayData { tupleArray = NSKeyedUnarchiver.unarchiveObjectWithData(tupleData) as! [(String,String,String)] }Thanks a lot!
6
0
4.1k
Oct ’15
Adding gradient background to SKScene
Hi.I am trying to add background with gradient color to my SKScene:class GameScene: SKScene { override func didMove(to view: SKView) { let gradientView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)) let image = UIImage.gradientBackgroundImage(bounds: CGRect(x: 0, y: 0, width: frame.width, height: frame.height), colors: [UIColor.yellow.cgColor, UIColor.blue.cgColor]) let background = SKSpriteNode(color: UIColor(patternImage: image), size: frame.size) addChild(background) } } extension UIImage { /** http://www.riptutorial.com/ios/example/14328/gradient-image-with-colors */ static func gradientBackgroundImage(bounds: CGRect, colors: [CGColor]) -> UIImage { let gradientLayer = CAGradientLayer() gradientLayer.frame = bounds gradientLayer.colors = colors UIGraphicsBeginImageContext(gradientLayer.bounds.size) gradientLayer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } }This is not working. Is there a better way to add gradient background to the SKScene?Thanks a lot!EDIT:If I do this in didMove(to view:):self.view?.backgroundColor = UIColor.blue let backgroundDep = IGABackgroundLayer().gradientMetar() // My method to create CAGradientLayer backgroundDep.frame = self.view!.bounds self.view!.layer.insertSublayer(backgroundDep, at: 0)...background is created nicely but added SKNodes are not seen on top of it.
5
0
4.3k
Sep ’18
How to handle a disconnected player in a real-time match
Hi. Are there best practices in terms of how I should handle a disconnected player in a real-time match? In my app, once a player goes to background, he/she gets disconnected and the player is marked as temporarily unavailable on other players' devices. Is there any way that the disconnected player can return back to the same game when he/she becomes active again? It sounds a bit counterproductive to stop the whole match if one player has to disconnect quickly, for example to answer a call. Thank you!
1
0
806
Nov ’20
GameKit: 'didFind match' never called
I am trying to implement a real-time match in GameCenter using GameKit and SpriteKit. This is my scene: class IntroScene: SKScene, GameCenterManagerDelegate { &#9;var gameCenterManager: GameCenterManager? &#9;override func didMove(to view: SKView) { &#9;&#9;GameCenterManager.manager.delegate = self &#9;&#9;<other code> &#9;} &#9;override func didChangeSize(_ oldSize: CGSize) { &#9;&#9;super.didChangeSize(oldSize) &#9;&#9;GameCenterManager.manager.authenticatePlayer() &#9;} &#9;func didChangeAuthStatus(isAuthenticated: Bool) { &#9;&#9;...Enabling the Button to play in GameCenter &#9;} &#9;func presentGameCenterAuth(viewController: UIViewController?) { &#9;&#9;guard let vc = viewController else {return} &#9;&#9;self.view!.window?.rootViewController?.present(vc, animated: true) &#9;} &#9;func presentMatchmaking(viewController: UIViewController?) { &#9;&#9;guard let vc = viewController else {return} &#9;&#9;self.view!.window?.rootViewController?.present(vc, animated: true) &#9;} &#9;->> Problem here. Never gets called &#9;func presentGame(match: GKMatch) { &#9;&#9;print("....INTROSCENE: DELEGATE PRESENT_GAME CALLED......") &#9;&#9;... Presenting a new scene &#9;} } This is my GameCenterManager class: final class GameCenterManager : NSObject, GKLocalPlayerListener {   static let manager = GameCenterManager()   weak var delegate: GameCenterManagerDelegate?   var maxPlayers = 2   var match: GKMatch?   static var isAuthenticated: Bool {     return GKLocalPlayer.local.isAuthenticated   }       override init() {     super.init()   }           func authenticatePlayer() {     GKLocalPlayer.local.authenticateHandler = { gcAuthVC, error in       self.delegate?.didChangeAuthStatus(isAuthenticated: GKLocalPlayer.local.isAuthenticated)       if GKLocalPlayer.local.isAuthenticated {         GKLocalPlayer.local.register(self)       }       // If the User needs to sign to the Game Center       else if let vc = gcAuthVC {         self.delegate?.presentGameCenterAuth(viewController: vc)       }       else {         print(">>>>> Error authenticating the Player! \(error?.localizedDescription ?? "none") <<<<<")       }     }   }       func presentMatchmaker() {     guard GKLocalPlayer.local.isAuthenticated else { return }     let request = GKMatchRequest()     request.minPlayers = 2     request.maxPlayers = 6     request.inviteMessage = "Would you like to play?"     guard let vc = GKMatchmakerViewController(matchRequest: request) else { return }     vc.matchmakerDelegate = self     delegate?.presentMatchmaking(viewController: vc)   }     &#9;// THIS IS WORKING...   func player(_ player: GKPlayer, didAccept invite: GKInvite) {           print("-----player -- did accept invite-------\(player.displayName)")           guard let vc = GKMatchmakerViewController(invite: invite) else { return }     vc.matchmakerDelegate = self           self.gameCenterViewController?.present(vc, animated: true, completion: nil)   }       func player(_ player: GKPlayer, didRequestMatchWithRecipients recipientPlayers: [GKPlayer]) {     print("didRequestMatchWithRecipients")   }           func player(_ player: GKPlayer, matchEnded match: GKTurnBasedMatch) {     print("match ended")   }       func player(_ player: GKPlayer, wantsToQuitMatch match: GKTurnBasedMatch) {     print("wants to quit match")   } } extension GameCenterManager: GKMatchmakerViewControllerDelegate {     &#9;// THIS IS NOT WORKING -(   func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {           print("-----matchmakerVC did find match-------")           viewController.dismiss(animated: true)           match.delegate = self           delegate?.presentGame(match: match)   }       func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {     print("matchmakerVC was cancelled")     viewController.dismiss(animated: true)     delegate?.matchmakingCancelled()   }       func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {     viewController.dismiss(animated: true)     delegate?.matchmakingError(error: error)   } } extension GameCenterManager: GKMatchDelegate {       func match(_ match: GKMatch, didReceive data: Data, forRecipient recipient: GKPlayer, fromRemotePlayer player: GKPlayer) {     // HANDLE DATA   }       func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {     //HANDLE DATA   }           func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {     print("-----match did change state")     guard match == self.match else { return }     switch state {     case .connected where self.match != nil :       print("-----MATCH DID CHANGE STATE CONNECTED-----")     case .disconnected:       print("-----MATCH DID CHANGE STATE DISCONNECTED-----")     default:       break     }   }       func match(_ match: GKMatch, didFailWithError error: Error?) {     print("-----match did fail with error")   } } So, basically this is what happens. Player 1 starts the game. The player gets successfully authenticated. I invite Player 2 to join the game. The invitation is sent to Player 2's device. Player 2 accepts the game (printing player did accept invite). However, after that nothing happens. There is continuing spinning wheel saying "Sending" on Player 1's device, and the game scene never launches. I never receive notification that the match has been found, i.e. matchmakerViewController(_ viewController:,match:) is never called. Any help would be greatly appreciated!
2
0
992
Oct ’20
Buying IAP for a Sandbox account: Verification Failed
Hi. I have created several sandbox accounts to test in-app purchases for my application. However, every time I try to make a purchase on my iPhone I get the following error: Verification Failed. Your Apple ID or password is incorrect. This is wrong, I am pretty sure that my credentials are fine. I used to have a similar issue on iPad. What I did was reinstalling the application on my device after I had logged in as a sandbox user and cleaning all application data before installing the application. Somehow it worked. But not on iPhone; no matter what I do, the problem persists. Your help would be much appreciated! I do not want to go to production without a successful test on an iPhone. Thank you!
1
0
1.5k
Aug ’20