ios - MultiPeer Framework Delegates Not Firing/Running (No UI) -
i've been stuck on problem while. i'm trying test multi peer framework. , i'm trying without ui. in other words, i'm inviting other peers manually through code. want able app come up, , peers automatically start looking each other , connect.
to set class named sidewaysmanager so:
/** sidewaysmanager.m */ #import "sidewaysmanager.h" #import "epbmyconfig.h" @implementation sidewaysmanager { nsstring *databasepath; mcsession *currentsession; mcnearbyservicebrowser *nearbybrowser; mcnearbyserviceadvertiser *nearbyadvertiser; nsstring *servicetype; } - (id) init { self = [super init]; if(self) { epbmyconfig *configinfo = [epbmyconfig instance]; nsstring *assetid = [configinfo getassetid]; nsstring *useagelocation = [configinfo getuseagelocation]; nsstring *peerdisplayname = [@"pctouchpad-" stringbyappendingstring:assetid]; if(![useagelocation isequaltostring:@""]) { peerdisplayname = [peerdisplayname stringbyappendingstring: [nsstring stringwithformat:@"-%@", useagelocation]]; } mcpeerid *localpeerid = [[mcpeerid alloc] initwithdisplayname:peerdisplayname]; currentsession = [[mcsession alloc] initwithpeer:localpeerid]; currentsession.delegate = self; servicetype = @"epb-session"; nearbybrowser = [[mcnearbyservicebrowser alloc] initwithpeer:localpeerid servicetype:servicetype]; nearbybrowser.delegate = self; nearbyadvertiser = [[mcnearbyserviceadvertiser alloc] initwithpeer:localpeerid discoveryinfo:nil servicetype:servicetype]; nearbyadvertiser.delegate = self; nsarray *documentpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentdir = [documentpaths objectatindex:0]; databasepath = [documentdir stringbyappendingstring:@"/voterdb_prod"]; [nearbyadvertiser startadvertisingpeer]; [nearbybrowser startbrowsingforpeers]; return self; } return self; } /** session delegates */ // remote peer changed state - (void)session:(mcsession *)session peer:(mcpeerid *)peerid didchangestate:(mcsessionstate)state { if(state == mcsessionstateconnected) { [nearbyadvertiser stopadvertisingpeer]; [nearbybrowser stopbrowsingforpeers]; nslog(@"peer connected session"); } else if(state == mcsessionstatenotconnected) { nslog(@"peer not connected"); } } // received data remote peer - (void)session:(mcsession *)session didreceivedata:(nsdata *)data frompeer:(mcpeerid *)peerid { } // start receiving resource remote peer - (void)session:(mcsession *)session didstartreceivingresourcewithname:(nsstring *)resourcename frompeer:(mcpeerid *)peerid withprogress:(nsprogress *)progress { } // finished receiving resource remote peer , saved content in temporary location - app responsible moving file permanent location within sandbox - (void)session:(mcsession *)session didfinishreceivingresourcewithname:(nsstring *)resourcename frompeer:(mcpeerid *)peerid aturl:(nsurl *)localurl witherror:(nserror *)error { } - (void)session:(mcsession *)session didreceivecertificate:(nsarray *)certificate frompeer:(mcpeerid *)peerid certificatehandler:(void(^)(bool accept))certificatehandler { nslog(@"received certificate"); certificatehandler(yes); } - (void)session:(mcsession *)session didreceivestream:(nsinputstream *)stream withname:(nsstring *)streamname frompeer:(mcpeerid *)peerid { stream.delegate = self; [stream scheduleinrunloop:[nsrunloop mainrunloop] formode:nsdefaultrunloopmode]; [stream open]; } /** mcbrowser delegates */ // found nearby advertising peer - (void)browser:(mcnearbyservicebrowser *)browser foundpeer:(mcpeerid *)peerid withdiscoveryinfo:(nsdictionary *)info { nslog(@"found peer"); [browser invitepeer:peerid tosession:currentsession withcontext:nil timeout:0]; } // nearby peer has stopped advertising - (void)browser:(mcnearbyservicebrowser *)browser lostpeer:(mcpeerid *)peerid { nslog(@"lost peer"); } /**advertising delegates */ - (void)advertiser:(mcnearbyserviceadvertiser *)advertiser didreceiveinvitationfrompeer:(mcpeerid *)peerid withcontext:(nsdata *)context invitationhandler:(void(^)(bool accept, mcsession *session))invitationhandler { invitationhandler(yes, currentsession); } // advertising did not start due error - (void)advertiser:(mcnearbyserviceadvertiser *)advertiser didnotstartadvertisingpeer:(nserror *)error { nslog(@"did not start advertising"); } @end
and here's sidewaysmanager.h file:
#import <foundation/foundation.h> #import <multipeerconnectivity/multipeerconnectivity.h> @interface sidewaysmanager : nsobject <mcnearbyserviceadvertiserdelegate, mcnearbyservicebrowserdelegate, mcsessiondelegate, nsstreamdelegate> @end
, initialize class in appdelegate class. but, reason none of delegate methods above run. can 1 familiar framework me out or have idea why nothing happening in class? appreciate it.
i have created project based on single view application template. added sidewaysmanager code project. make compile removed epbmyconfig import , fixed compilation errors shown below:
// epbmyconfig *configinfo = [epbmyconfig instance]; // nsstring *assetid = [configinfo getassetid]; // nsstring *useagelocation = [configinfo getuseagelocation]; nsstring *peerdisplayname = @"pctouchpad-test"; // if(![useagelocation isequaltostring:@""]) // { // peerdisplayname = [peerdisplayname stringbyappendingstring: [nsstring stringwithformat:@"-%@", useagelocation]]; // } mcpeerid *localpeerid = [[mcpeerid alloc] initwithdisplayname:peerdisplayname];
running code on iphone , simulator delegates methods called , session created. seems setup of multipeer connectivity objects ok.
how create sidewaysmanager in appdelegate?
this how did it:
@interface pfappdelegate () @property (strong, nonatomic) sidewaysmanager *manager; @end @implementation pfappdelegate - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { self.manager = [[sidewaysmanager alloc]init]; return yes; }
it important appdelegate class holds reference manager, otherwise de-alloced after didfinishlaunchingwithoptions returns. explain nothing happens in code.
alternatively, might problem in creation of peer display name (basically stuff commented out).
Comments
Post a Comment