I'm trying to use Multipeer Connectivity to connect iPhone Simulator on my mac with my iPhone. The app will get to the MCBrowserViewController, where I'll tap the iPhone Simulator and send an invitation to it. Then, I'll accept the invitation from my Mac and my phone will show me a "connecting..." However, the connection never succeeds, even though my mac and phone are less than 4 inches apart. This also doesn't work in reverse (i.e. when I send the invite from my mac)
I've tried the suggestions in this link but neither having separate advertiser and browser sessions nor commenting out the didReceiveCertificate seems to be working.
- (void)setupPeerWithDisplayName:(NSString *)displayName {
self.peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
}
- (void)setupSession {
self.advertiseSession = [[MCSession alloc] initWithPeer:self.peerID];
self.advertiseSession.delegate = self;
self.browserSession = [[MCSession alloc] initWithPeer:self.peerID];
self.browserSession.delegate = self;
}
- (void)setupBrowser {
self.browser = [[MCBrowserViewController alloc] initWithServiceType:#"m117" session:self.browserSession];
}
- (void)advertiseSelf:(BOOL)advertise {
if (advertise) {
self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:#"m117" discoveryInfo:nil session:self.advertiseSession];
[self.advertiser start];
} else {
[self.advertiser stop];
self.advertiser = nil;
}
}
...
-(void)session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL))certificateHandler {
certificateHandler(YES);
}
Related
My App was rejected by Apple because it can't connect to other device running iOS 10.1.1 on Wi-Fi connected to an IPv6 network.
When I tap on connect, the app continues to search for invitees and no further user action is produced.
I use Multi-peer Connectivity and I never tested my App being connected to an IPv6(It's my first release). But the App run very fine without having any connection or being connected to IPv4 network.
I don't know why the App is running and connecting fine using the IPv4 and doesn't connect to peer if the iPad is connected to an IPv6 network.
So my Question: Is it possible to use Multi-peer Connectivity with IPv6 so that Apple can approve the App or how should I handle this Issue ?
Here is my Code, maybe it is something wrong there.
#interface ConnectionManageriOS7 () <MCSessionDelegate, MCBrowserViewControllerDelegate>
{
UILocalNotification *_expireNotification;
UIBackgroundTaskIdentifier _taskId;
}
#property (nonatomic, strong) MCSession *session;
#property (nonatomic, strong) MCPeerID *localPeerID;
#property (nonatomic, strong) MCBrowserViewController *browserVC;
#property (nonatomic, strong) MCAdvertiserAssistant *advertiser;
#end
static ConnectionManageriOS7 *_manager = nil;
#implementation ConnectionManageriOS7
+ (ConnectionManageriOS7 *)connectManager {
#synchronized([ConnectionManageriOS7 class]){
if (_manager == nil) {
_manager = [[ConnectionManageriOS7 alloc] init];
}
return _manager;
}
return nil;
}
- (id)init {
self = [super init];
if (self) {
[self setupSessionAndAdvertiser];
}
return self;
}
- (void)setupSessionAndAdvertiser {
_localPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];;
_session = [[MCSession alloc] initWithPeer:_localPeerID];
_session.delegate = self;
}
- (void)connectWithDelegate:(id)delegate {
_delegate = delegate;
if (_session.connectedPeers.count) {
if ([_delegate respondsToSelector:#selector(didConntectedWithManager:)]) {
[_delegate didConntectedWithManager:self];
}
} else {
if (_advertiser == nil) {
_advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:VISUS_Service
discoveryInfo:nil
session:_session];
_isConnected = NO;
[_advertiser start];
}
if (_browserVC == nil) {
_browserVC = [[MCBrowserViewController alloc] initWithServiceType:VISUS_Service session:_session];
_browserVC.delegate = self;
}
[(UIViewController *)delegate presentViewController:_browserVC
animated:YES completion:nil];
}
}
- (void)sendMessage:(NSString *)message {
NSData *textData = [message dataUsingEncoding:NSASCIIStringEncoding];
NSLog(#"Send Data: %#", message);
NSError *error = nil;
[_session sendData:textData
toPeers:_session.connectedPeers
withMode:MCSessionSendDataReliable
error:&error];
if (error) {
//
[self session:_session peer:nil didChangeState:MCSessionStateNotConnected];
NSLog(#"error %#", error.userInfo);
}
}
- (void)stopService {
NSLog(#"Stop Service");
[_advertiser stop];
_advertiser = nil;
_browserVC = nil;
}
#pragma marks -
#pragma marks MCBrowserViewControllerDelegate
- (void) dismissBrowserVC{
[_browserVC dismissViewControllerAnimated:YES completion:nil];
}
// Notifies the delegate, when the user taps the done button
- (void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController {
if ([_delegate respondsToSelector:#selector(didConntectedWithManager:)]) {
[_delegate didConntectedWithManager:self];
}
[self dismissBrowserVC];
}
// Notifies delegate that the user taps the cancel button.
- (void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController{
if (_browserVC == nil) {
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}else {
[self dismissBrowserVC];
}
}
#pragma marks -
#pragma marks MCBrowserViewControllerDelegate
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
if (state != MCSessionStateConnecting) {
if (state == MCSessionStateConnected) {
_isConnected = true;
if ([_delegate respondsToSelector:#selector(willConntectedWithManager:)]) {
[_delegate willConntectedWithManager:self];
}
}
else {
_isConnected = false;
[self stopService];
if ([_delegate respondsToSelector:#selector(didDisconntectedWithManager:)]) {
[_delegate didDisconntectedWithManager:self];
}
}
}
}
// Received data from remote peer
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
// Decode data back to NSString
NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Receive Data: %#", message);
// append message to text box:
dispatch_async(dispatch_get_main_queue(), ^{
if ([_delegate respondsToSelector:#selector(connectionManager:receivedString:)]) {
[_delegate connectionManager:self receivedString:message];
}
});
}
- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error {
_isConnected = false;
[self stopService];
NSLog(#"----- Error ----- %#", error.localizedDescription);
}
// Received a byte stream from remote peer
- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID {
}
// Start receiving a resource from remote peer
- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress {
}
- (void) session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler
{
certificateHandler(YES);
}
- (void) createExpireNotification
{
[self killExpireNotification];
if (_session.connectedPeers.count != 0) // if peers connected, setup kill switch
{
NSTimeInterval gracePeriod = 20.0f;
// create notification that will get the user back into the app when the background process time is about to expire
NSTimeInterval msgTime = UIApplication.sharedApplication.backgroundTimeRemaining - gracePeriod;
UILocalNotification* n = [[UILocalNotification alloc] init];
_expireNotification = n;
_expireNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:msgTime];
_expireNotification.alertBody = #"Bluetooth Connectivity is about to disconnect. Open the app to resume Test";
_expireNotification.soundName = UILocalNotificationDefaultSoundName;
_expireNotification.applicationIconBadgeNumber = 1;
[UIApplication.sharedApplication scheduleLocalNotification:_expireNotification];
}
}
- (void) killExpireNotification
{
if (_expireNotification != nil)
{
[UIApplication.sharedApplication cancelLocalNotification:_expireNotification];
_expireNotification = nil;
}
}
- (void)bacgroundHandling {
_taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^
{
[self stopService];
[[UIApplication sharedApplication] endBackgroundTask:_taskId];
_taskId = UIBackgroundTaskInvalid;
}];
[self createExpireNotification];
}
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
// http://down.vcnc.co.kr/WWDC_2013/Video/708.pdf -- wwdc tutorial, this part is towards the end (p119)
// self.arrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]];
// ask the user
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:peerID.displayName
message:#"Would like to create a session with you"
delegate:self
cancelButtonTitle:#"Decline" otherButtonTitles:#"Accept", nil];
[alertView show];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// retrieve the invitationHandler and check whether the user accepted or declined the invitation...
BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO;
// respond
if(accept) {
// void (^invitationHandler)(BOOL, MCSession *) = [self.arrayInvitationHandler objectAtIndex:0];
// invitationHandler(accept, self.mySession);
}
else
{
NSLog(#"Session disallowed");
}
}
- (void)terminate {
[self killExpireNotification];
[self stopService];
}
#end
I have solved the problem. For everybody with simular problem:
It wasn't a problem with IPv6, it is a matter of how to use Multipeer connectivity. In my Case I tryied to the the IPv6 connection with a iPad and a Simulator. And I used my Macbook for creating a nat64 network. And the reason why the simulator and iPad never saw each other the fact that they where not connected to same wifi network.
Solution:
Just take for testing two iPads and use your mac as nat64 network accesspoint.
There is a similar question to this here:Multipeer Connectivity Framework - Lost Peer stays in Session
But the answers have unfortunately not helped, or I have misunderstood them.
I have my multipeer connectivity app, and have also tested Apples own MultipeerGroupChat app.
When a peer disconnects, sometimes they stay in the session. This results in failed connections when the peer tries to reconnect.
I have tried [session disconnect] in applicationWillTerminate, but it still occurs.
How can I have the MCBrowserViewController update itself with who is connected and who isn't?
-(id)init{
self = [super init];
if (self) {
_peerID = nil;
_session = nil;
_browser = nil;
_advertiser = nil;
}
_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
-(void)setupMCBrowser{
_browser = [[MCBrowserViewController alloc] initWithServiceType:#"session" session:_session];
_browser.maximumNumberOfPeers = 8;
}
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
NSDictionary *dict = #{#"peerID": peerID,
#"state" : [NSNumber numberWithInt:state]
};
[[NSNotificationCenter defaultCenter] postNotificationName:#"MCDidChangeStateNotification"
object:nil
userInfo:dict];
switch (state)
{
case MCSessionStateConnecting:
{
_statusID = #"Connecting";
break;
}
case MCSessionStateConnected:
{
_statusID = #"Connected";
if (_firstPeer == NULL){
_firstPeer = peerID.displayName;
}
else if (_secondPeer == NULL){
_secondPeer = peerID.displayName;
}
else if (_thirdPeer == NULL){
_thirdPeer = peerID.displayName;
}
break;
}
case MCSessionStateNotConnected:
{
if ([peerID isEqual:_firstPeer]){
_firstPeer = NULL;
}
else if ([peerID isEqual:_secondPeer]){
_secondPeer = NULL;
}
else if ([peerID isEqual:_thirdPeer]){
_thirdPeer = NULL;
}
_statusID = #"Connection Lost";
break;
}
}
}
I know this question has been asked many times, but after reading each of them more than a few times, I still can't get my Multipeer Connectivity to work. I am sending but not receiving the invitation. Here is the code:
#implementation MPCManager
- (id)init {
self = [super init];
if (self) {
_myPeerID = nil;
_session = nil;
_browser = nil;
_advertiser = nil;
}
return self;
}
- (void)automaticBrowseAndAdvertiseWithName:(NSString *)displayName {
_myPeerID = [[MCPeerID alloc] initWithDisplayName:displayName];
_session = [[MCSession alloc] initWithPeer:_myPeerID];
_session.delegate = self;
_advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:_myPeerID
discoveryInfo:nil
serviceType:#"trm-s"];
_advertiser.delegate = self;
[_advertiser startAdvertisingPeer];
_browser = [[MCNearbyServiceBrowser alloc] initWithPeer:_myPeerID
serviceType:#"trm-s"];
_browser.delegate = self;
[_browser startBrowsingForPeers];
}
- (void)session:(MCSession *)session
didReceiveCertificate:(NSArray *)certificate
fromPeer:(MCPeerID *)peerID
certificateHandler:(void (^)(BOOL accept))certificateHandler {
certificateHandler(YES);
}
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
didReceiveInvitationFromPeer:(MCPeerID *)peerID
withContext:(NSData *)context
invitationHandler:(void (^)(BOOL,
MCSession *))invitationHandler {
NSLog(#"This is NOT getting called");
}
- (void)browser:(MCNearbyServiceBrowser *)browser
didNotStartBrowsingForPeers:(NSError *)error {
NSLog(#"%#", [error localizedDescription]);
}
- (void)browser:(MCNearbyServiceBrowser *)browser
foundPeer:(MCPeerID *)peerID
withDiscoveryInfo:(NSDictionary *)info {
NSLog(#"This IS getting called");
}
- (void)invitePeer:(MCPeerID *)peerID {
NSLog(#"This IS getting called");
[_browser invitePeer:peerID toSession:_session withContext:nil timeout:30];
}
I am running it on two simulators, and it was working for some time, but stopped suddenly. Any ideas on how or where to look for the problem?
Make sure that you are serializing and reusing your MCPeerID objects whenever possible. Each time you call - (instancetype)initWithDisplayName:(NSString *)myDisplayName it returns a unique instance.
What often happens in a dev environment is that you end up with a flood of advertisers and browsers and a ton of ghost duplicates in the Bonjour advertising space. This can cause everything to just go wonky.
If you are using simulators then resetting them may help. On hardware you can restart or toggle airplane mode.
Take a look at this year's WWDC session on Multipeer named "Cross Platform Nearby Networking". It has some good best practices to follow that will help immensely.
I am getting duplicate peername if I connect and disconnect bluetooth multiple time in my both ios device.
Is there any way to get single name for unique peer in gkpeerpickercontroller for bluetooth chat application.
Also attached the screenshot for it.
I am using below code to show GKPeerPickerController.
-(IBAction)btnConnectClicked:(id)sender
{
[self openPeerPickerController];
}
-(IBAction)btnDisconnectClicked:(id)sender
{
[currentSession disconnectFromAllPeers];
}
-(void)openPeerPickerController
{
if (!currentSession)
{
GKPeerPickerController *peerPicker2 = [[GKPeerPickerController alloc] init];
peerPicker2.delegate = self;
peerPicker2.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[peerPicker2 show];
}
}
-(void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *) session
{
NSLog(#"Peer session connected");
//set session delegate and dismiss the picker
session.delegate = self;
currentSession = session;
picker.delegate = nil;
[picker dismiss];
}
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
{
//create ID for session
NSString *sessionIDString = #"MTBluetoothSessionID";
//create GKSession object
GKSession *session = [[GKSession alloc] initWithSessionID:sessionIDString displayName:nil sessionMode:GKSessionModePeer];
return session;
}
-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
NSLog(#"Peer cancelled");
[currentSession disconnectFromAllPeers];
currentSession=nil;
picker.delegate = nil;
}
-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
switch (state)
{
case GKPeerStateAvailable:
{
// not connected to session, but available for connectToPeer:withTimeout:
}
break;
case GKPeerStateUnavailable:
{
// no longer available
}
break;
case GKPeerStateConnected:
{
// connected to the session
[currentSession setDataReceiveHandler:self withContext:nil]; //set ViewController to receive data
}
break;
case GKPeerStateDisconnected:
{
// disconnected from the session
currentSession.delegate = nil;
currentSession = nil; //allow session to reconnect if it gets disconnected
}
break;
case GKPeerStateConnecting:
{
// waiting for accept, or deny response
}
break;
default:
break;
}
}
You GKPeerPickerControllerDelegate method
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
Returns a new session every time. In your case its being called twice and hence two sessions are created.
From the documentation:
When the peer picker needs a session, it calls this method. Your
application can either create a new session or return a previously
created session to the peer picker.
So you can declare the session as a property and write a getter and just return the session property in the delegate which will avoid creation of multiple sessions
#property (nonatomic, string) GKSession *session;
#define sessionIDString #"MTBluetoothSessionID"
- (GKSession) session {
if(!_session) {
//create GKSession object
_session = [[GKSession alloc] initWithSessionID:sessionIDString displayName:nil sessionMode:GKSessionModePeer];
}
return _session;
}
And change the delegate method to :
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {
return self.session;
}
Make sure to nullify the session when you're done.
I can reliably crash the simulator with this when releasing the GKSession after calling displayNameForPeer for another peer (not self), and I'm not sure if it's something I'm doing wrong or if it's a bug with Apple's Gamekit framework (and whether I need to worry about it, since I only see the crash under 4.0 and 4.1, not 4.2+).
The output is:
found available peer; checking name and ID... m4, 26176566
*** -[GKSessionInternal lock]: message sent to deallocated instance 0x7508900
Here's the minimal reproducible code set -- note that another GKSession must be visible on the network (so that there's an available peer found to call displayNameForPeer on) to trigger the crash. Running this same code on another device but without the makeUnavailable and killSession calls is adequate.
- (void)viewDidLoad
{
[self createSession];
[self makeAvailable];
peerListAvailable = [[NSMutableArray alloc] initWithArray:[currentSession peersWithConnectionState:GKPeerStateAvailable]];
for (NSString *peer in peerListAvailable)
{
// this method guarantees the crash on session release
NSLog(#"found available peer; checking name and ID... %#, %#",[currentSession displayNameForPeer:peer], peer);
}
[peerListAvailable release];
peerListAvailable = nil;
[self makeUnavailable];
[self killSession];
[super viewDidLoad];
}
- (void) createSession
{
if (!currentSession)
{
currentSession = [[GKSession alloc] initWithSessionID:#"GKTester" displayName:nil sessionMode:GKSessionModePeer];
currentSession.delegate = self;
currentSession.disconnectTimeout = 30;
[currentSession setDataReceiveHandler: self withContext:nil];
}
}
-(void) killSession
{
if (currentSession)
{
[currentSession disconnectFromAllPeers];
[currentSession setDelegate:nil];
[currentSession setDataReceiveHandler:nil withContext:nil];
[currentSession release]; // crash occurs after this
currentSession = nil;
}
}
-(void) makeAvailable
{
while (currentSession && !currentSession.available)
{
[currentSession setAvailable:YES];
[NSThread sleepForTimeInterval:.5];
}
}
-(void) makeUnavailable
{
while (currentSession && currentSession.available)
{
[NSThread sleepForTimeInterval:.5];
[currentSession setAvailable:NO];
}
}
You have an over-release in your code:
[currentSession disconnectFromAllPeers];
[currentSession setDelegate:nil];
[currentSession setDataReceiveHandler:nil withContext:nil];
[currentSession release]; // This is an over-release
currentSession = nil; // You are trying to access a variable after it's been released
You should release the currentSession member variable in dealloc only, like so:
- (void)dealloc
{
[currentSession release];
[super dealloc];
}