I'm modifying the "Chatty" app from here: http://mobileorchard.com/tutorial-networking-and-bonjour-on-iphone/
I'm trying to create a server which quickly receives information from multiple clients, but does not really send anything back. There are both Wifi and Bluetooth used at the same time, so I would like to maximize the network throughput. I think that UDP protocol might be faster, because there's no handshaking/packet sequence verification.
Is changing the "_chatty._tcp." to "_chatty._udp." enough in this example? I checked the documentation for NSNetService, but don't see anything about UDP there.
- (BOOL) publishService {
// come up with a name for our chat room
NSString* chatRoomName = [NSString stringWithFormat:#"%#'chat room", [[AppConfig getInstance] name]];
// create new instance of netService
self.netService = [[NSNetService alloc]
initWithDomain:#"" type:#"_chatty._tcp."
name:chatRoomName port:self.port];
if (self.netService == nil)
return NO;
// Add service to current run loop
[self.netService scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];
// NetService will let us know about what's happening via delegate methods
[self.netService setDelegate:self];
// Publish the service
[self.netService publish];
return YES;
}
// Start browsing for servers
- (BOOL)start {
// Restarting?
if ( netServiceBrowser != nil ) {
[self stop];
}
netServiceBrowser = [[NSNetServiceBrowser alloc] init];
if( !netServiceBrowser ) {
return NO;
}
netServiceBrowser.delegate = self;
[netServiceBrowser searchForServicesOfType:#"_chatty._tcp." inDomain:#""];
return YES;
}
It appears that the app is working fine after changing "_chatty._tcp." to "_chatty._udp."
Related
I am doing VoIP call Application. There i can make call through my application. I have integrated SiriKit setup and added following code.
#pragma mark -Start Audio Call
- (void)resolveContactsForStartAudioCall:(INStartAudioCallIntent *)intent
withCompletion:(void (^)(NSArray<INPersonResolutionResult *> *resolutionResults))completion{
NSLog(#"maytest resolveContactsForStartAudioCall");
NSArray<INPerson *> *recipients = intent.contacts;
NSMutableArray<INPersonResolutionResult *> *resolutionResults = [NSMutableArray array];
if (recipients.count == 0) {
completion(#[[INPersonResolutionResult needsValue]]);
return;
}else if(recipients.count==1){
[resolutionResults addObject:[INPersonResolutionResult successWithResolvedPerson:recipients.firstObject]];
}else if(recipients.count>1){
[resolutionResults addObject:[INPersonResolutionResult disambiguationWithPeopleToDisambiguate:recipients]];
}else{
[resolutionResults addObject:[INPersonResolutionResult unsupported]];
}
completion(resolutionResults);
}
- (void)confirmStartAudioCall:(INStartAudioCallIntent *)intent
completion:(void (^)(INStartAudioCallIntentResponse *response))completion{
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartAudioCallIntent class])];
INStartAudioCallIntentResponse *response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeReady userActivity:userActivity];
completion(response);
}
- (void)handleStartAudioCall:(INStartAudioCallIntent *)intent
completion:(void (^)(INStartAudioCallIntentResponse *response))completion{
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartAudioCallIntent class])];
INStartAudioCallIntentResponse *response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeContinueInApp userActivity:userActivity];
completion(response);
}
But, its not making calls through my app. Its making calls through native dialer and some times showing application not yet set up with siri.
Could anyone guide me and after make call, how to save call history.
Thanks!
I encountered this problem too!!!
I my case, it's because I'm calling Siri in Chinese!!!! It takes me a half day to figure it out!!
If I change Siri language to English and everything works just fine!
In Chinese Siri will only call through native dialer no matter how I specify the application.
I'm not sure if it happens on other languages too since I can only test those two.
I'm trying to make a simple application that will scan for nearby Bluetooth devices and list their names as they are discovered. I'm using CoreBluetooth in accordance with every guide I've found, including Apple's guide here
However, it never works. I put an iPhone 4S in discoverable mode next to the iPhone 5 running the app, and it never discovers it. I also tried a Bluetooth-enabled car, but I don't know if it has BLE. What am I doing wrong? Here is the essence of my code, in ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[activity stopAnimating]; isScanning = NO; //activity is a GUI activity wheel
centralManager = [[CBCentralManager alloc] initWithDelegate: self queue: nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
int state = central.state;
[self log: [NSString stringWithFormat: #"CBCentralManagerDidUpdateState: %d", state]];
//[self log] just NSLogs the message and adds it to a text view for the user to see.
if (state!=CBCentralManagerStatePoweredOn){
[self log: #"Error! Bluetooth not powered on!"]; //I never get this error.
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
[self log: [NSString stringWithFormat: #"Peripheral found with CoreBluetooth: %#", peripheral.name]];
//And I never see any of these "peripheral found" messages.
}
- (IBAction)scanButton:(id)sender {
if (!isScanning){
[activity startAnimating];
isScanning = YES;
[centralManager scanForPeripheralsWithServices:nil options:nil];
[self log: #"Scanning started."];
}
else{
[activity stopAnimating];
isScanning = NO;
[centralManager stopScan];
[self log: #"Scanning stopped."];
}
}
Thanks for any suggestions.
I found an answer here: Can't seem to get core bluetooth to work
I need an iOS device in peripheral mode or a BLE peripheral. Very annoying because basically no peripherals use BLE.
It works with my brother's iPhone 4S running a free app from the App Store called LightBlue. The app lets you put the device in peripheral mode... it's kind of the developer to put out a nice testing app like this.
I have been developing a game which allows for multiplayer matches. I had previous tested the multiplayer invitations and they had all worked. Sending a request from one device displayed a banner on the other and if the invite was accepted the game started.
Just before submitting the app, two nights ago, I tested this functionality again only to find that it has stopped working.
- (void)authenticateLocalUser:(UIViewController *)viewController :(id<GCHelperDelegate>)theDelegate
{
delegate = theDelegate;
self.presentingViewController = viewController;
if (!gameCenterAvailable) {
// Game Center is not available.
userAuthenticated = FALSE;
}
else{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
/*
The authenticateWithCompletionHandler method is like all completion handler methods and runs a block
of code after completing its task. The difference with this method is that it does not release the
completion handler after calling it. Whenever your application returns to the foreground after
running in the background, Game Kit re-authenticates the user and calls the retained completion
handler. This means the authenticateWithCompletionHandler: method only needs to be called once each
time your application is launched. This is the reason the sample authenticates in the application
delegate's application:didFinishLaunchingWithOptions: method instead of in the view controller's
viewDidLoad method.
Remember this call returns immediately, before the user is authenticated. This is because it uses
Grand Central Dispatch to call the block asynchronously once authentication completes.
*/
//ios 6
[localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
if (viewcontroller != nil){
userAuthenticated = FALSE;
[self.presentingViewController presentViewController: viewcontroller animated: YES completion:nil];
}
else if (localPlayer.isAuthenticated){
// Enable Game Center Functionality
userAuthenticated = TRUE;
[self checkForInvite:self.presentingViewController :delegate];
if (! self.currentPlayerID || ! [self.currentPlayerID isEqualToString:localPlayer.playerID]) {
// Current playerID has changed. Create/Load a game state around the new user.
self.currentPlayerID = localPlayer.playerID;
// get friends of local player
[localPlayer loadFriendsWithCompletionHandler:^(NSArray *friends, NSError *error) {
if (friends != nil)
{
[self loadPlayerData: friends];
}
}];
}
}
else{
userAuthenticated = FALSE;
}
[scoreHandler setGameCentreAvailable:userAuthenticated];
})];
}
}
- (void)checkForInvite :(UIViewController *)viewController :(id<GCHelperDelegate>)theDelegate
{
delegate = theDelegate;
self.presentingViewController = viewController;
NSLog(#"Invite handler installed");
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite){
NSLog(#"Accepted");
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
[viewController presentViewController: mmvc animated: YES completion:nil];
} else if (playersToInvite) {
NSLog(#"Match Request");
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[viewController presentViewController: mmvc animated: YES completion:nil];
}
};
}
The debug window in xcode shows the following:
2013-03-27 18:06:20.112 MyApp[791:907] Authentication changed: player authenticated.
2013-03-27 18:06:21.219 MyApp[791:907] Invite handler installed
Mar 27 18:06:21 Neils-iPhone MyApp[791] <Notice>: 18:06:21.356712 com.apple.GameKitServices: -[GKDiscoveryManager startAdvertisingLocalPlayer:discoveryInfo:]: I am [<nil>] [7989F444CF2BDA83] discoveryInfo [{
e = 2;
h = A42FD7FD;
}]
Is the "I am []..." significant in the lines above?
I have even downloaded and run the tutorial from Ray Wenderlich's site for creating a multiplayer game and tried that. That exhibits the same issues, unless it is running in the foreground on both devices. My app does not display invitation requests even if running in the foreground.
Has anyone else experienced this problem or have any ideas what is going on? authenticateLocalUser is called from applicationDidFinishLaunching
The only way I could make invites-by-name work is by going to Settings/Notifications/Game Center and making Game Center display Alerts, not Banners.
If you have GC display alerts, you get a popup box like this:
This dialog acts like a big parent. If the user hits Accept, then your [GKMatchmaker sharedMatchmaker].inviteHandler gets invoked.
If the user hits Decline, then your game never knows that he was invited to any party ever. User hitting Decline means the parent rips up the invitation and never tells his child he got invited to a game at all.
This is the only way I could get invite-by-name to work.
I use ZXing for an app, this is mainly the same code than the ZXing original code except that I allow to scan several time in a row (ie., the ZXingWidgetController is not necesseraly dismissed as soon as something is detected).
I experience a long long freeze (sometimes it never ends) when I press the dismiss button that call
- (void)cancelled {
// if (!self.isStatusBarHidden) {
// [[UIApplication sharedApplication] setStatusBarHidden:NO];
// }
[self stopCapture];
wasCancelled = YES;
if (delegate != nil) {
[delegate zxingControllerDidCancel:self];
}
}
with
- (void)stopCapture {
decoding = NO;
#if HAS_AVFF
if([captureSession isRunning])[captureSession stopRunning];
AVCaptureInput* input = [captureSession.inputs objectAtIndex:0];
[captureSession removeInput:input];
AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[captureSession.outputs objectAtIndex:0];
[captureSession removeOutput:output];
[self.prevLayer removeFromSuperlayer];
/*
// heebee jeebees here ... is iOS still writing into the layer?
if (self.prevLayer) {
layer.session = nil;
AVCaptureVideoPreviewLayer* layer = prevLayer;
[self.prevLayer retain];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 12000000000), dispatch_get_main_queue(), ^{
[layer release];
});
}
*/
self.prevLayer = nil;
self.captureSession = nil;
#endif
}
(please notice that the dismissModalViewController that remove the view is within the delegate method)
I experience the freeze only while dismissing only if I made several scans in a row, and only with an iPhone 4 (no freeze with a 4S)
Any idea ?
Cheers
Rom
According to the AV Cam View Controller Example calling startRunning or stopRunning does not return until the session completes the requested operation. Since you are sending these messages to the session on the main thread, it freezes all the UI until the requested operation completes. What I would recommend is that you wrap your calls in an Asynchronous dispatch so that the view does not lock-up.
- (void)cancelled
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self stopCapture];
});
//You might want to think about putting the following in another method
//and calling it when the stop capture method finishes
wasCancelled = YES;
if (delegate != nil) {
[delegate zxingControllerDidCancel:self];
}
}
I am developing an online multiplayer game, but I am struggling with Apple documentation. (I have tried these tutorials by Ray Wenderlichs Part 1 and part 2, but they are not working (match never starts because inviting device never receives the match acceptance).
As this topic is vast, I will be creating a single question, then moving to create another question on SO if necessary.
I want to create an online multiplayer game that will let a user to invite from 1 to 3 people. So, it would be a 2 to 4 people match. The game is not turned based. It is live and the data to be transferred between users is minimum.
Lets start with the basic stuff.
1) the first thing I do is create a notification
if (self.gameCenterAvailable) {
NSNotificationCenter *nc =
[NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(authenticationChanged)
name:GKPlayerAuthenticationDidChangeNotificationName
object:nil];
}
to let me know when the notification changes. When this happens, authenticationChanged method will fire... here it is
- (void)authenticationChanged {
GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite)
{
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
[self presentModalViewController:mmvc animated:YES];
}
else if (playersToInvite)
{
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 4;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[self presentModalViewController:mmvc animated:YES];
}
};
}
I grabbed this code from Apple. My question here is this. If Apple say to run this code after the user is authenticated why it is checking for invitation or users to invite? As far as I see, users were not invited yet. Unless the code is not executed at that time, right? It will just sit in memory waiting to be called, WHEN the invitation is done, correct?
If this is the case, I now create an invitation for a match doing
[self dismissModalViewControllerAnimated:NO];
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
request.playersToInvite = self.pendingPlayersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[self presentModalViewController:mmvc animated:YES];
A window will be present to all users I choose to invite. Suppose the first one taps ACCEPT on the invitation. Which method will be fired on my app, how do I get the user identity and how do I know if all users accepted?
thanks.
First of all please be aware that invitations on the Sandbox enviroment tend to work erratically, so I suggest you start by having all players search for an available match. The code would be something like this:
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
[[GKMatchmaker sharedMatchmaker] findMatchForRequest:request
withCompletionHandler:^(GKMatch *match, NSError *error) {
if (error || !match) {
// handle the error
}
else if (match != nil){
// match found
}}];
}
Then you have to implement the protocol GKMatchDelegate. There's a method there that will be invoked for each player that joins the match and for each player that gets disconnected from it (on this method you can find out the user identity with its playerID):
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
switch (state){
case GKPlayerStateConnected:
if (match.expectedPlayerCount == 0) {
// start the match
}
break;
case GKPlayerStateDisconnected:
// remove the player from the match, notify other players, etc
break;
}
}
Regarding your first question, the code on authenticationChanged is only registering the handlers for those methods, meaning the code that will be invoked when the notifications arrive.
EDIT: Regarding the question on your comment, if the match was started by invitations, the user that started the match has to wait until all invitations are accepted, or cancel some of them and then press Start Match (or something like that) on the Invite Screen. In this scenario the match.expectedPlayerCount == 0 condition will be satisfied once all the players that accepted the invites are connected to the match.
If the match was started by AutoMatch then Game Center does the following: once it finds minPlayers waiting to start a match it will assign them to a match and then wait a few more seconds to see if it can fill the remaining slots. At some point it will start the match with a certain number of players between minPlayers and maxPlayers. Then the condition match.expectedPlayerCount == 0 will be satisfied only once all the players have effectively join the match, but note that when the decision was taken to start a match the number of players expected for that match is already determined by Game Center.