feat(ios): files sending
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,18 @@
|
||||
//
|
||||
// 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"
|
||||
@@ -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,39 @@
|
||||
import Foundation
|
||||
import MultipeerConnectivity
|
||||
|
||||
|
||||
class MyDeviceDataGenerator {
|
||||
static func generate(name: String?) -> NearbyDevice {
|
||||
return NearbyDevice(
|
||||
peerID: getPeerID(),
|
||||
name: getNameArchived(or: name),
|
||||
deviceType: UIDevice.current.model,
|
||||
os: UIDevice.current.systemName,
|
||||
osVersion: UIDevice.current.systemVersion
|
||||
)
|
||||
}
|
||||
static private func getPeerID() -> MCPeerID {
|
||||
if let archivedPeerID = Archiver.getPeerID() {
|
||||
return archivedPeerID
|
||||
} else {
|
||||
let peerID = MCPeerID(
|
||||
displayName: UIDevice.current.name.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
|
||||
}
|
||||
return archivedName ?? UIDevice.current.name
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user