Post not yet marked as solved
Good morning,
I come to you for a question:
When I install my application on my iPhone for the first time, and I install the watch application from the native "Watch" application, the Watch Connectivity function does not work, I have to do the installation from Xcode to the watch for this function to work.
Is this normal? if yes, the problem will not arise during a publication?
I have the same problem when using watch and iPhone simulators, WatchConnectivity does not work.
I am this error code in Xcode:
-[WCSession handleIncomingUserInfoWithPairingID:]_block_invoke delegate (null) does not implement session:didReceiveUserInfo:, discarding incoming content
Here is the code for the iPhone and the watch:
In my iPhone app:
import WatchConnectivity
let userDefaultsDataVenantWatch = UserDefaults.standard
class PhoneDataModel : NSObject, WCSessionDelegate, ObservableObject {
static let shared = PhoneDataModel()
let session = WCSession.default
@Published var TableauSynchroIphoneVersWatch : [String:String] = ["0":"0"]
@Published var dataWatchVersIphone: [String:String] = ["":""]
override init() {
super.init()
if WCSession.isSupported() {
session.delegate = self
session.activate()
} else {
print("ERROR: Watch session not supported")
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("session activation failed with error: \(error.localizedDescription)")
return
}
}
func sessionDidBecomeInactive(_ session: WCSession) {
session.activate()
}
func sessionDidDeactivate(_ session: WCSession) {
session.activate()
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any]) {
guard let newCount = userInfo["TableauSynchroIphoneWatch"] as? [String:String] else {
print("ERROR: unknown data received from Watch TableauSynchroIphoneWatch")
return
}
DispatchQueue.main.async {
print(newCount)
}
}
}
In my Watch app:
import WatchConnectivity
let userDefaultsDataVenantIphone = UserDefaults.standard
var TableauVenantIphone:[String:String] = ["":""]
class WatchDataModel : NSObject, WCSessionDelegate, ObservableObject {
static let shared = WatchDataModel()
let session = WCSession.default
@Published var TableauSynchroIphoneWatch : [String:String] = ["0":"0"]
override init() {
super.init()
if WCSession.isSupported() {
session.delegate = self
session.activate()
} else {
print("ERROR: Watch session not supported")
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("session activation failed with error: \(error.localizedDescription)")
return
}
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any]) {
guard let newCount = userInfo["TableauSynchroIphoneVersWatch"] as? [String:String] else {
print("ERROR: unknown data received from Watch TableauSynchroIphoneWatch")
return
}
DispatchQueue.main.async {
print(newCount)
}
}
}
Thank for your answers !
Hello everyone,
I need your help to convert my view in order to share it as an image:
On one of my "VStack" views, I would like to convert this view into an Image and share it via the "ShareLink" function
How can I convert a simple view to an Image?
For example:
ZStack {
Rectangle()
.fill(Color.blue.opacity(0.70))
.cornerRadius(16)
VStack {
Text("Share Text")
}
}
Thank You for your help !
Post not yet marked as solved
Hello World !
I need your help to perform a button rotation on my "VStack" view
My "Foreach" loop defines a multitude of buttons for me.
I would like it to rotate when I press a button.
When I put a common variable, the code does not give me an error but all the buttons rotate.
So I used a dictionary to make the buttons standalone but the code gave me an error.
How can I make my buttons autonomous in the rotation while keeping my "foreach" loop?
Thank you !
//Variable
@State private var TableauRotationEffect:[String:Double] = ["":0.0]
//Main Code in my view
ForEach(1..<Int(nombreSerie)!+1, id: \.self) { numSerie in
HStack(spacing: 20) {
Text ("\(numSerie) // \(nombreSerie):").frame(width:200).font(.system(size: 13, weight: .regular)).foregroundColor(.gray).offset(x: 60, y: 0)
Button {
TableauRotationEffect["Ser:\(numSerie)"] = 90
print("Bouton anime 1 \(DateDebutSeance) // \(numeroExercice) // \(numeroSerie)")
} label: {
Label("Graph", systemImage: "chevron.right.circle")
.labelStyle(.iconOnly)
.imageScale(.large)
.rotationEffect(.degrees(TableauRotationEffect["Ser:\(numSerie)"]))
.padding()
.animation(.easeInOut, value: showDetail)
.frame(height:10)
}.onAppear() {
TableauRotationEffect["Ser:\(numSerie)"] = 0
}
}
}
Post not yet marked as solved
Hello everyone,
I'm coming to you because I'm having a little trouble finding the code to know the movement of the Apple Watch.
I try to know exactly when the arm movement is up, down, left or right.
I can define it thanks to “attitude.pitch”
I can define it using "attitude", but it doesn't work correctly when the wrist is rotating at the same time as an arm movement. I no longer know how to move forward to know each movement.
My code is as follows:
(attitudeAvant11 is the attitude to the forward scan)
(attitude11 is the attitude.pitch on the AppleWatch)
Thank for your help
func knowMove (attitude11:Double, attitudeAvant11:Double) -> (Sens2:String, attitudeAvant12:Double) {
var attitude1 = attitude11
attitude1 = attitude1 * 100
let pourcentage = attitude1 - attitudeAvant
if attitudeAvant != 0 {
if pourcentage >= 0.5 {
sensPrint = "Sens: Move down // ➡️\(pourcentage) ⬅️ // \(attitude1) // \(attitudeAvant)"
sens1 = "1"
} else {
if pourcentage < -1.2 {
sensPrint = "Sens: Move Up // ➡️\(pourcentage)⬅️ // \(attitude1) // \(attitudeAvant)"
sens1 = "2"
} else {
sensPrint = "Sens: No Move // ➡️\(pourcentage)⬅️ // \(attitude1) // \(attitudeAvant)"
sens1 = "0"
}
}
} else {
sensPrint = "Sens: No Move Initialisation // ➡️\(pourcentage)⬅️ // \(attitude1) // \(attitudeAvant) "
}
let DateMaintenant = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
let dateString = formatter.string(from: DateMaintenant)
if sens1 != "Nul" {
TableauAttitude["\(dateString)"] = "\(sens1)"
//print(TableauAttitude)
}
attitudeAvant = attitude1
print("sensPrint: \(sensPrint)")
return (sens1,attitudeAvant)
}
Post not yet marked as solved
Hello,
For my first watchOS app, I want use a navigationLink for switch to a new view, but the button does not work. Can you help me ?
import SwiftUI
struct ContentView: View {
@State var timerVal = 1
@State var secondScreenShow = false
@State private var selectedColor = "30s"
var body: some View {
VStack {
NavigationLink (
destination: SecondView1()
, isActive: $secondScreenShow, label: {
Text("GO")
})
}
}
}
struct SecondView1: View {
var body: some View {
VStack {
Text ("second view1")
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Thank you
Post not yet marked as solved
Hello,I'm coming to you because I have a problem when I try to start a timer with an integrated variable. This sends me a compilation error, I don't know how to send a variable in my timer function. I have two "swift" files and I want to keep two different files for my functions.my error:"Instance member 'AttaqueCanon' cannot be used on type 'ExtensionTimer'; did you mean to use a value of this type instead?"Thank for your help !In my "main.swift"let ExtensionTimer = Fichier2()
gameTimerAttaqueCanon = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(ExtensionTimer.AttaqueCanon(tt:Sprite1)), userInfo: nil, repeats: true)in my "Fichier2.swift"import Foundation
import SpriteKit
class ExtensionTimer {
@objcfunc AttaqueCanon(tt:SKSpriteNode) {
let Canon1 = tt.childNode(withName: "Canon")
}
}
Post not yet marked as solved
Hi,I am asking you for a problem of contact between two objects.I am currently using PNG images and the contact is on the outline of the image and not the image itself. I would like that the transparent part is not taken into account for the contacts between the objects, I do not find my error. here is my code:my SKSpriteNode:let EnnemisOrdinateur1Category:UInt32 = 0b0100 //4
let PouvoirPlayer1Category:UInt32 = 0b01000 //8
func ennemis1() {
let EnnemisOrdinateur11 = SKSpriteNode()
EnnemisOrdinateur11.name = "EnnemisOrdinateur1"
EnnemisOrdinateur11.size.width = WidthDelEnnemis //Largeur
EnnemisOrdinateur11.size.height = EnnemisOrdinateur11.frame.width //Hauteur
EnnemisOrdinateur11.position = positionDepartEnnemisOrdinateur
EnnemisOrdinateur11.physicsBody = SKPhysicsBody(rectangleOf: EnnemisOrdinateur11.size)
EnnemisOrdinateur11.physicsBody?.isDynamic = true
EnnemisOrdinateur11.physicsBody?.categoryBitMask = EnnemisOrdinateur1Category
EnnemisOrdinateur11.physicsBody?.contactTestBitMask = MurOUobjetCategory | PouvoirPlayer1Category
EnnemisOrdinateur11.physicsBody?.collisionBitMask = MurOUobjetCategory | PouvoirPlayer1Category
EnnemisOrdinateur11.physicsBody?.usesPreciseCollisionDetection = true
Vueprincipale.addChild(EnnemisOrdinateur11)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if (touchedNode.name?.contains(« EnnemisOrdinateur1 »))! {
let CategoryContactAttaque1 = EnnemisOrdinateur1Category
DeployerPouvoir(Texture1: TexturePouvoirBouleDeFeu, Child1: BouleDeFeu1, Parent: Vueprincipale, TimeInter: 0.1, NombreDImage: 4, NameImage: "BouleDeFeu", NameDuPouvoir1: "BouleDeFeu", Taille1:"1", PositionDoigt2:PositionDoigt1, PositionDeDepart1: player1.position, CategoryContactAttaque:CategoryContactAttaque1, CategoryDuChild:PouvoirPlayer1Category)
}
}
}
func DeployerPouvoir(Texture1:[SKTexture], Child1:SKSpriteNode, Parent:SKSpriteNode, TimeInter:Double, NombreDImage: Int, NameImage:String, NameDuPouvoir1:String, Taille1:String, PositionDoigt2:CGPoint, PositionDeDepart1:CGPoint, CategoryContactAttaque:UInt32, CategoryDuChild:UInt32) {
var texture1 = [SKTexture]()
var Child = Child1
var Taille:CGFloat = Parent.frame.height/10
if Taille1 == "1" {
}
func Texture() {
for i in 1...NombreDImage {
let textureName = "\(NameImage)\(i)"
texture1.append(SKTexture(imageNamed: textureName))
}
if (texture1.count>1) {
Child = SKSpriteNode(imageNamed: "\(NameImage)1")
Child.position = PositionDeDepart1
Child.size.width = Taille
Child.size.height = Taille
Child.zPosition = 99
}
}
Texture()
let ImageCase1 = Child
ImageCase1.name = "\(NameDuPouvoir1)"
ImageCase1.size.width = Taille
ImageCase1.size.height = Taille
ImageCase1.physicsBody = SKPhysicsBody(circleOfRadius: ImageCase1.size.width / 2)
ImageCase1.physicsBody?.categoryBitMask = CategoryDuChild
ImageCase1.physicsBody?.isDynamic = true
ImageCase1.physicsBody?.contactTestBitMask = CategoryContactAttaque
ImageCase1.physicsBody?.collisionBitMask = CategoryContactAttaque
ImageCase1.physicsBody?.usesPreciseCollisionDetection = true
Parent.addChild(ImageCase1)
let titi = SKAction.repeatForever((SKAction.animate(with: texture1, timePerFrame: TimeInterval(TimeInter))))
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: PositionDoigt2.x, y: PositionDoigt2.y), duration: 1))
ImageCase1.run(titi)
actionArray.append(SKAction.removeFromParent())
ImageCase1.run(SKAction.sequence(actionArray))
var angle:CGFloat = 0
angle = atan2(PositionDoigt2.x - PositionDeDepart1.x , PositionDoigt2.y - PositionDeDepart1.y)
ImageCase1.zRotation = -angle
}contact:func didBegin(_ contact: SKPhysicsContact) {
let bodyA = contact.bodyA.categoryBitMask
//let bodyB = contact.bodyB.categoryBitMask il nest pas utilise
let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
print("BodyA:\(contact.bodyA.categoryBitMask) //BodyB \(contact.bodyB.categoryBitMask) // Colision:\(collision)")
if collision == EnnemisOrdinateur1Category | PouvoirPlayer1Category {
if bodyA == 0b100 {
print(«contact1»)
}
}
}thank you for your help !Max
Post not yet marked as solved
Hello, I come to you because I am in front of a problem that I do not understand. I would like to perform an action of a "player" if one clicks on the left and do another action on one clicks on the right. But when I click once on the right and once on the left, the two actions are done continuously without stopping (a left shot and a right shot), I do not understand why while I stop the actions at the beginning of the functionthank you override func touchesMoved(_ touches: Set, with event: UIEvent?) {
for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
ChangementTexture()
}
func ChangementTexture () {
let Frontiere11 = self.Vueprincipale.childNode(withName: "Frontiere1") as? SKSpriteNode
if PositionDoigt.x<player1.position.x && PositionDoigt.y<Frontiere11!.frame.minY {
GestionAnimationPlayer1(Direction: "Gauche")
print("Gauche")
}
if PositionDoigt.x>player1.position.x && PositionDoigt.y<Frontiere11!.frame.minY {
GestionAnimationPlayer1(Direction: "Droite")
print("Droite")
}
}
func GestionAnimationPlayer1(Direction:String) {
//Suppression de tous les child de player
if let child = self.Vueprincipale.childNode(withName: "player1") as? SKSpriteNode {
positionDuPlayer = child.position
child.removeFromParent()
child.removeAllChildren()
child.removeAllActions()
child.removeAction(forKey: "MarcherGaucheAction1")
child.removeAction(forKey: "MarcherDroiteAction1")
}
//Texture
for i in 1...4 {
let textureName = "Marcher\(Direction)\(i)"
TexturePlayer1.append(SKTexture(imageNamed: textureName))
}
/*if (TexturePlayer1.count>1) {
player1 = SKSpriteNode(imageNamed: "Marcher\(Direction)1")
}*/
let action1 = (SKAction.repeatForever(SKAction.animate(with: TexturePlayer1, timePerFrame: 0.2)))
player1.name = "player1"
player1.run(action1, withKey: "Marcher\(Direction)Action1")
Vueprincipale.addChild(player1)
if let child = self.Vueprincipale.childNode(withName: "player1") as? SKSpriteNode {
child.position = positionDuPlayer
}
//Calcul du temps de placement du player pour que ce soit lineaire
let calculPythagorePourBougerPlayer1Lineairement = sqrt(abs(PositionDoigt.x-player1.position.x)+abs(PositionDoigt.y-player1.position.y))
let calculPythagorePourBougerPlayer1Lineairement1 = sqrt((view!.frame.width)+(view!.frame.height))
var calculPourTempsPlayer = 2*((calculPythagorePourBougerPlayer1Lineairement)/calculPythagorePourBougerPlayer1Lineairement1)
if calculPourTempsPlayer<0 { calculPourTempsPlayer = calculPourTempsPlayer*(-1) }
print("\(calculPourTempsPlayer)")
let tempsDeDeplacementDuPlayer:TimeInterval = TimeInterval(calculPourTempsPlayer)
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: PositionDoigt, duration: tempsDeDeplacementDuPlayer))
player1.run(SKAction.sequence(actionArray))
self.run(SKAction.wait(forDuration: tempsDeDeplacementDuPlayer)) {
if let child = self.Vueprincipale.childNode(withName: "player1") as? SKSpriteNode {
child.removeAllActions()
child.removeAction(forKey: "MarcherGaucheAction1")
child.removeAction(forKey: "MarcherDroiteAction1")
self.player1.texture = SKTexture(imageNamed:"Repos1")
}
}
}