Migrating app from Swift 2.2 to 3.0 with WCSessionDelegate

My app was working correctly with swift 2.2 in xCode 7 but after upgrade my xCode to 8 version and migrate swift to 3.0, I founded this error in my WCSeesionDelegate


class WatchSessionManager: NSObject, WCSessionDelegate {
     @available(watchOS 2.2, *)
     public function session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?)
     {
     }


Type "WatchSessionManager" does not conform to protocol "WCSessionDelegate"


After that error I added the mandatory functions and then I founded this errors


class WatchSessionManager: NSObject, WCSessionDelegate {
     @available(iOS 9.3, *)
     public func sessionDidBecomeInactive(_ session: WCSession)
     {
     }
Cannot override "sessionDidBecomeInactive" which has been marked unavailable

     @available(iOS 9.3, *)
     public func sessionDidDeactivate(_ session: WCSession)
     {
     }
Cannot override "sessionDidDeactivate" which has been marked unavailable

     @available(watchOS 2.2, *)
     public function session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?)
     {
     }


Any idea?

Accepted Reply

My deployment target is iOS10 and WatchOS3

OK. I’m going to ignore the watchOS deployment target because you said earlier than this is failing on the iOS side of the build.

Here’s what I did:

  1. I created a new project from the Single View Application template in Xcode 8. This defaults to the iOS deployment target being set to iOS 10.0.

  2. In that project I added a new Swift file called

    WatchSessionManager.swift
    .
  3. I pasted in the code from my 20 Sep post, above.

  4. I built for the simulator.

It built just fine. Please try this at your end and let me know what you see.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

The following works fine when I paste it into a newly created iOS project.

import WatchConnectivity

class WatchSessionManager: NSObject, WCSessionDelegate { 
    public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) 
    { 
    }

    func sessionDidBecomeInactive(_ session: WCSession) {
    }

    func sessionDidDeactivate(_ session: WCSession) {
        session.activate()
    }
}

I suspect you’re tripping over availability problems. Do you get these errors when building the iOS side or the watchOS side?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

This code is part of the iOS side.


Thank you

This code is part of the iOS side.

Cool; that’s what I was testing.

Why did you include watchOS availability annotations?

What’s your iOS deployment target?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Eskimo

Yes, you're right, this availability is wrong, but if I remove this part, the errors doesn't change.


My deployment target is iOS10 and WatchOS3


Thank a lot for your help

My deployment target is iOS10 and WatchOS3

OK. I’m going to ignore the watchOS deployment target because you said earlier than this is failing on the iOS side of the build.

Here’s what I did:

  1. I created a new project from the Single View Application template in Xcode 8. This defaults to the iOS deployment target being set to iOS 10.0.

  2. In that project I added a new Swift file called

    WatchSessionManager.swift
    .
  3. I pasted in the code from my 20 Sep post, above.

  4. I built for the simulator.

It built just fine. Please try this at your end and let me know what you see.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Big mistery. I delete my old file and copy/paste, then it works.


Thanks a lot, I was desperate.