Skip to content

Commit

Permalink
Fix issue on iOS devices
Browse files Browse the repository at this point in the history
  • Loading branch information
vietdq2701 committed Nov 25, 2020
1 parent c595ee7 commit 1b6f6d5
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 79 deletions.
53 changes: 28 additions & 25 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -76,7 +76,6 @@
EE2828077045705EB4BA404D /* Pods-Runner.release.xcconfig */,
C9F844878FCCBEAFD77B42DD /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -253,27 +252,12 @@
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/../Flutter/Flutter.framework",
"${BUILT_PRODUCTS_DIR}/SAMKeychain/SAMKeychain.framework",
"${BUILT_PRODUCTS_DIR}/SwiftyJSON/SwiftyJSON.framework",
"${BUILT_PRODUCTS_DIR}/Toast/Toast.framework",
"${BUILT_PRODUCTS_DIR}/device_info/device_info.framework",
"${BUILT_PRODUCTS_DIR}/flutter_nearby_connections/flutter_nearby_connections.framework",
"${BUILT_PRODUCTS_DIR}/flutter_udid/flutter_udid.framework",
"${BUILT_PRODUCTS_DIR}/fluttertoast/fluttertoast.framework",
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SAMKeychain.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftyJSON.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Toast.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/device_info.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_nearby_connections.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_udid.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fluttertoast.framework",
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -370,20 +354,26 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = com.nankai.flutterNearbyConnectionsExample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down Expand Up @@ -490,7 +480,8 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand All @@ -502,20 +493,26 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = com.nankai.flutterNearbyConnectionsExample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
Expand All @@ -529,20 +526,26 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = com.nankai.flutterNearbyConnectionsExample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down
15 changes: 8 additions & 7 deletions example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
23 changes: 14 additions & 9 deletions ios/Classes/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@ class Device: NSObject {

func createSession() {
if self.session != nil { return }

self.session = MCSession(peer: MPCManager.instance.localPeerID, securityIdentity: nil, encryptionPreference: .required)
self.session?.delegate = self
}

func disconnect() {
self.session?.disconnect()
self.session = nil
NotificationCenter.default.post(name: MPCManager.Notifications.deviceDidChangeState, object: self)
}

func invite(with browser: MCNearbyServiceBrowser) {
self.createSession()
browser.invitePeer(self.peerID, to: self.session!, withContext: nil, timeout: 10)
if (self.state == MCSessionState.notConnected) {
self.createSession()
if let session = session {
browser.invitePeer(self.peerID, to: session, withContext: nil, timeout: 10)
}
}
}

}

extension Device: MCSessionDelegate {
public func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
self.state = state
NotificationCenter.default.post(name: MPCManager.Notifications.deviceDidChangeState, object: self)
if(self.state != state) {
self.state = state
NotificationCenter.default.post(name: MPCManager.Notifications.deviceDidChangeState, object: self)
}
}

public func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
Expand All @@ -48,7 +53,7 @@ extension Device: MCSessionDelegate {
public func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) { }

public func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) { }

public func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) { }

}
66 changes: 38 additions & 28 deletions ios/Classes/MPCManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ class MPCManager: NSObject {

var deviceDidChange: (() -> Void)?

override init() {
if let data = UserDefaults.standard.data(forKey: "peerID"), let id = NSKeyedUnarchiver.unarchiveObject(with: data) as? MCPeerID {
// override init() {
// if let data = UserDefaults.standard.data(forKey: "peerID"), let id = NSKeyedUnarchiver.unarchiveObject(with: data) as? MCPeerID {
// self.localPeerID = id
} else {
// } else {
// let peerID = MCPeerID(displayName: UIDevice.current.name)
// let data = NSKeyedArchiver.archivedData(withRootObject: peerID)
// UserDefaults.standard.set(data, forKey: "peerID")
// self.localPeerID = peerID
}
super.init()
}
// }
// super.init()
// }

deinit{
if(enterbackgroundNotification != nil){
Expand Down Expand Up @@ -97,39 +97,44 @@ class MPCManager: NSObject {
}

func invitePeer(deviceID: String) {
self.devices.forEach { (element) in
element.disconnect()
do {
let device = MPCManager.instance.findDevice(for: deviceID)
if(device?.state == MCSessionState.notConnected){
device?.invite(with: self.browser)
}
} catch let error {
print(error.localizedDescription)
}


let device = MPCManager.instance.device(for: deviceID)
device?.invite(with: self.browser)
}

func disconnectPeer(deviceID: String){
self.devices.forEach { (element) in
if(element.peerID.displayName == deviceID){
element.disconnect()
}
let device = MPCManager.instance.findDevice(for: deviceID)
device?.disconnect()
}

func device(for id: MCPeerID) -> Device {
if let device = devices.first(where: {$0.peerID == id}) {
return device
} else {
let device = Device(peerID: id)
self.devices.append(device)
return device
}
}

func device(for deviceId: String) -> Device? {
func findDevice(for deviceId: String) -> Device? {
for device in self.devices {
if device.peerID.displayName == deviceId { return device }
}

return nil
}

func device(for id: MCPeerID) -> Device {
func findDevice(for id: MCPeerID) -> Device? {
if let device = devices.first(where: {$0.peerID == id}) {
return device
} else {
let device = Device(peerID: id)
self.devices.append(device)
return device
}
return nil
}

@objc func enteredBackground() {
Expand All @@ -141,7 +146,6 @@ class MPCManager: NSObject {

extension MPCManager: MCNearbyServiceAdvertiserDelegate {
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {

let device = self.device(for: peerID)
device.createSession()
invitationHandler(true, device.session)
Expand All @@ -151,15 +155,21 @@ extension MPCManager: MCNearbyServiceAdvertiserDelegate {

extension MPCManager: MCNearbyServiceBrowserDelegate {
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
// found peer, create a device with this peerID
self.device(for: peerID)

}

func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
let device = self.device(for: peerID)
device.disconnect()
devices.removeAll { (device) -> Bool in
device.peerID == peerID
// lost peer, disconnect and remove the device with this peerID
let device = self.findDevice(for: peerID)
device?.disconnect()
do {
try devices.removeAll { (device) -> Bool in
device.peerID == peerID
}
} catch let error {
print(error.localizedDescription)
}
}

}
35 changes: 25 additions & 10 deletions ios/Classes/SwiftFlutterNearbyConnectionsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ public class SwiftFlutterNearbyConnectionsPlugin: NSObject, FlutterPlugin {
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch MethodCall(rawValue: call.method) {
case .initNearbyService:
let data = call.arguments as! Dictionary<String, AnyObject>
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
result(false)
return

}
let serviceType:String = data["serviceType"] as? String ?? SERVICE_TYPE
var deviceName:String = data["deviceName"] as? String ?? ""
if (deviceName.isEmpty){
Expand All @@ -124,16 +128,24 @@ public class SwiftFlutterNearbyConnectionsPlugin: NSObject, FlutterPlugin {
MPCManager.instance.stopBrowsingForPeers()
result(true)
case .invitePeer:
let data = call.arguments as! Dictionary<String, AnyObject>
let deviceId:String? = data["deviceId"] as? String ?? nil
if (deviceId != nil) {
MPCManager.instance.invitePeer(deviceID: deviceId!)
result(true)
} else {
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
result(false)
return

}
guard let deviceId: String = data["deviceId"] as? String else {
result(false)
return
}
MPCManager.instance.invitePeer(deviceID: deviceId)
result(true)

case .disconnectPeer:
let data = call.arguments as! Dictionary<String, AnyObject>
guard let data = call.arguments as? Dictionary<String, AnyObject> else {
result(false)
return

}
let deviceId:String? = data["deviceId"] as? String ?? nil
if (deviceId != nil) {
MPCManager.instance.disconnectPeer(deviceID: deviceId!)
Expand All @@ -142,10 +154,13 @@ public class SwiftFlutterNearbyConnectionsPlugin: NSObject, FlutterPlugin {
result(false)
}
case .sendMessage:
let dict = call.arguments as! Dictionary<String, AnyObject>
guard let dict = call.arguments as? Dictionary<String, AnyObject> else {
result(false)
return
}
do {
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
if let device = MPCManager.instance.device(for: dict["deviceId"] as! String) {
if let device = MPCManager.instance.findDevice(for: dict["deviceId"] as! String) {
currentReceivedDevice = device
try device.send(data: jsonData)
result(true)
Expand Down

0 comments on commit 1b6f6d5

Please sign in to comment.