Skip to content

Instantly share code, notes, and snippets.

@ismt7
Last active December 28, 2022 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismt7/e328ae1a2f6c05a2024d2ca5a31af119 to your computer and use it in GitHub Desktop.
Save ismt7/e328ae1a2f6c05a2024d2ca5a31af119 to your computer and use it in GitHub Desktop.
iPhoneとApple Watch連携のサンプルコード(iPhone側)
import SwiftUI
import WatchConnectivity
struct ContentView: View {
@ObservedObject private var connector = WatchConnector()
init() {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.backgroundColor = UIColor(
red: 30/255,
green: 150/255,
blue: 234/255,
alpha: 1.0
)
navigationBarAppearance.titleTextAttributes = [
.foregroundColor: UIColor.white,
.font : UIFont.systemFont(ofSize: 30, weight: .bold)
]
navigationBarAppearance.largeTitleTextAttributes = [
.foregroundColor: UIColor.white,
.font : UIFont.systemFont(ofSize: 30, weight: .bold)
]
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
}
var body: some View {
NavigationStack{
VStack {
VStack{
Text("You have pushed the button this many times:")
Text(String(self.connector.count))
.font(.largeTitle)
.foregroundColor(Color.gray)
Text("\(self.connector.receivedMessage)")
}
.frame(maxHeight: .infinity)
HStack{
Spacer()
Button(action: {
self.connector.send()
}, label: {
Image(systemName: "plus")
.frame(width: 50, height: 50)
})
.padding()
.accentColor(Color.white)
.background(Color(red: 30/255, green: 150/255, blue: 234/255))
.font(.system(size: 30))
.cornerRadius(50)
}
.padding()
}
.frame(maxHeight: .infinity)
.navigationTitle("Sample App")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
class WatchConnector: NSObject, ObservableObject, WCSessionDelegate {
@Published var receivedMessage = "WATCH: 未受信"
@Published var count = 0
override init() {
super.init()
if WCSession.isSupported() {
WCSession.default.delegate = self
WCSession.default.activate()
}
}
func session(
_ session: WCSession,
activationDidCompleteWith activationState: WCSessionActivationState,
error: Error?
) {
print("activationDidCompleteWith state= \(activationState.rawValue)")
}
func sessionDidBecomeInactive(_ session: WCSession) {
print("sessionDidBecomeInactive")
}
func sessionDidDeactivate(_ session: WCSession) {
print("sessionDidDeactivate")
}
func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
print("didReceiveMessage: \(message)")
DispatchQueue.main.async {
self.receivedMessage = "WATCH : \(message["WATCH_COUNT"] as! Int)"
self.count = message["WATCH_COUNT"] as! Int
}
}
func send() {
if WCSession.default.isReachable {
count += 1
WCSession.default.sendMessage(["PHONE_COUNT" : count], replyHandler: nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment