Add macOS support (#23)
* Add macOS support * Update README * Update to v0.2.0 * Document breaking changes * Delete widget tests
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// ArchivedData.swift
|
||||
// nearby_service
|
||||
//
|
||||
// Created by Kseniia Nikitina on 4/2/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import MultipeerConnectivity
|
||||
|
||||
|
||||
class Archiver {
|
||||
static func getPeerID() -> MCPeerID? {
|
||||
guard let savedData = UserDefaults.standard.data(forKey: PEER_ID),
|
||||
let unarchivedPeerID = try? NSKeyedUnarchiver.unarchivedObject(ofClass: MCPeerID.self, from: savedData)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
return unarchivedPeerID
|
||||
}
|
||||
|
||||
static func savePeerID(for peerID: MCPeerID) {
|
||||
|
||||
do {
|
||||
UserDefaults.standard.set(
|
||||
try NSKeyedArchiver.archivedData(withRootObject: peerID, requiringSecureCoding: false),
|
||||
forKey: PEER_ID
|
||||
)
|
||||
} catch let e {
|
||||
Logger.error(message: e.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static func getName() -> String? {
|
||||
guard let savedData = UserDefaults.standard.data(forKey: DEVICE_NAME),
|
||||
let unarchivedName = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSData.self, from: savedData)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
return String(data: unarchivedName as Data, encoding: .utf8)
|
||||
}
|
||||
|
||||
static func saveName(for name: String) {
|
||||
if let data = name.data(using: .utf8) {
|
||||
do {
|
||||
UserDefaults.standard.set(
|
||||
try NSKeyedArchiver.archivedData(withRootObject: data, requiringSecureCoding: false),
|
||||
forKey: DEVICE_NAME
|
||||
)
|
||||
} catch let e {
|
||||
Logger.error(message: e.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Constants.swift
|
||||
// nearby_service
|
||||
//
|
||||
// Created by Kseniia Nikitina on 4/2/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
let SERVICE_TYPE = "mp-connection"
|
||||
let PEER_ID = "PEER-ID"
|
||||
let DEVICE_NAME = "DEVICE-NAME"
|
||||
let ON_MESSAGE_RECEIVED = Notification.Name("NearbySessionOnMessageReceived")
|
||||
let ON_RESOURCE_RECEIVED = Notification.Name("NearbySessionOnResourceReceived")
|
||||
|
||||
let DART_COMMAND_MESSAGE_RECEIVED = "invoke_nearby_service_message_received"
|
||||
let DART_COMMAND_RESOURCES_RECEIVED = "invoke_nearby_service_resources_received"
|
||||
|
||||
let ERROR_NO_INITIALIZATION = "NO_INITIALIZATION"
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Logger.swift
|
||||
// nearby_service
|
||||
//
|
||||
// Created by Kseniia Nikitina on 26/1/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
class Logger {
|
||||
static func error(message: String) {
|
||||
NSLog("NearbyServicePluginError -- %@", message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import Foundation
|
||||
import MultipeerConnectivity
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
#elseif os(macOS)
|
||||
import AppKit
|
||||
import Foundation
|
||||
#endif
|
||||
|
||||
|
||||
class MyDeviceDataGenerator {
|
||||
static func generate(name: String?) -> NearbyDevice {
|
||||
return NearbyDevice(
|
||||
peerID: getPeerID(),
|
||||
name: getNameArchived(or: name),
|
||||
deviceType: getDeviceType(),
|
||||
os: getOSName(),
|
||||
osVersion: getOSVersion()
|
||||
)
|
||||
}
|
||||
|
||||
static private func getDeviceType() -> String {
|
||||
#if os(iOS)
|
||||
return UIDevice.current.model
|
||||
#elseif os(macOS)
|
||||
return "Mac"
|
||||
#endif
|
||||
}
|
||||
|
||||
static private func getOSName() -> String {
|
||||
#if os(iOS)
|
||||
return UIDevice.current.systemName
|
||||
#elseif os(macOS)
|
||||
return "macOS"
|
||||
#endif
|
||||
}
|
||||
|
||||
static private func getOSVersion() -> String {
|
||||
#if os(iOS)
|
||||
return UIDevice.current.systemVersion
|
||||
#elseif os(macOS)
|
||||
return ProcessInfo.processInfo.operatingSystemVersionString
|
||||
#endif
|
||||
}
|
||||
static private func getPeerID() -> MCPeerID {
|
||||
if let archivedPeerID = Archiver.getPeerID() {
|
||||
return archivedPeerID
|
||||
} else {
|
||||
let deviceName: String
|
||||
#if os(iOS)
|
||||
deviceName = UIDevice.current.name
|
||||
#elseif os(macOS)
|
||||
deviceName = Host.current().localizedName ?? "Mac"
|
||||
#endif
|
||||
|
||||
let peerID = MCPeerID(
|
||||
displayName: deviceName.replacingOccurrences(of: " ", with: "_")
|
||||
+ "_"
|
||||
+ String(Int.random(in: 1000..<50000))
|
||||
)
|
||||
Archiver.savePeerID(for: peerID)
|
||||
return peerID
|
||||
}
|
||||
}
|
||||
static private func getNameArchived(or name: String?) -> String {
|
||||
let archivedName = Archiver.getName()
|
||||
if let newName = name {
|
||||
if (archivedName != newName) {
|
||||
Archiver.saveName(for: newName)
|
||||
}
|
||||
return newName
|
||||
}
|
||||
#if os(iOS)
|
||||
return archivedName ?? UIDevice.current.name
|
||||
#elseif os(macOS)
|
||||
return archivedName ?? (Host.current().localizedName ?? "Mac")
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user