iphone MultipeerConnectivity lag - iphone

I am using MultipeerConnectivity framework for connecting two iOS devices in my app. I am able to connect two device using this framework but it is taking almost one minute to send an event from one device to other. I am testing this on simulator right now so I am not sure if that is causing the delay.
Should I be using a different framework for passing value of 2-3 variables from one device o another so that it is fast?

In the MultiPeerConnectivity framework by default the callback for the receiver happens on a background thread When you receive data from the session delegate, you need to do your work on the main thread:
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//do your work here
}];

Related

IPhone Device Orientation - How Main UI thread is affected?

I am developing a hello world application in iphone OS 6.0. Whenever a screen is rendered, a web service call is made to the server. But, i am making the web service call in the same main thread without starting in a seperate thread.
However, whenever i rotate the device to landscape mode, the web service call is stopped. Again when i rotate the device to portrait mode, web service call is started.
Any idea on why this happens ... Thanks in advance.
EDIT
I tested the application using iOS instruments (Network Monitor). I understand that device orientation did not stop the network operation.
The following links helped me arrive at a solution.
On iOS, can you make a synchronous network request (but not on the main thread) and still get progress callbacks (on a separate, non-main thread)?
http://tewha.net/2012/06/networking-using-nsurlconnection/
I understand that in Iphone, UI rendering happens in the Main Thread. So, i can control the application behavior with respect to device orientation similar to android OS.
Adding to this, I need not perform the network code in asynchronous mode. I can perform the synchronous network operation in a seperate thread. This solves my issue.
Thanks for all ...

Get extern [UIApplication sharedApplication]?

I am currently developing an "in device" (not simulator) monitoring system for an iPad app. The system should be able to do the following:
-User input simulation: Simulate user inputs: taps, drags, keyboard.
-Information recollection: Get text values and results after doing inputs.
This information will be later reported to an event management server. I have already taken care of the two points in a single app, by using some code from KIF software https://github.com/square/KIF and some self developed code. I use the accessibleName property of the UIView objects to get their pointer and send artificial events to the [[UIApplication sharedApplication] sendEvent:] method.
Here’s the thing: I need to send these events from app#1 in which I am, to app#2. (To keep the monitoring independent, also embedding all the code in app#2 is not an option) I use the [[UIApplication sharedApplication] openURL:] method to open the app#2.
https://developer.apple.com/library/IOs/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW18
The problem is, I cannot send events from app#1 to app#2 because I cannot seem to get the pointer to the [UIApplication sharedApplication] object of app#2 in the app#1 code.
A shared memory communication between the processes would be great, but according to the information that I found, the URL messaging scheme seems to be the only communication between apps (or processes) in iOS. The NSConnection class is also not present in iOS. I've also read a little bit about Mach ports and POSIX file descriptors, but they seem fairly complicated and I have no idea if they can help.
I even tried to send the value of the pointer of the app#2 [UIApplication sharedApplication] instance as a string parameter in the URL message, then converted the string back to a pointer in app#1, but got a EXC_BAD_ACCESS. I suppose the pointer address does not mean the same in both processes. Maybe each process has its own "offset"?
Any help would be greatly appreciated.
In first instance I wold clarify that what you want to do is not possibile without private APIs.
Each app is sandboxed and you cannot access to other apps (directly). I think that your problem is really big.
Each app running in iOS is like small, independent, eco-system that ends when the user press the 'Home' button (except if you declare some background services like VOIP, localization or audio player). Once the app has been closed the only thing that you can still run is a process that should not be longer than 10 mins. This is to prevent an excessive usage of background tasks and even here, the app isn't accessible from other apps.
I think the only way is to search for private APIs, hoping someone else already found a way to create a data streaming (directly) between 2 apps.

Get data from accelerometer when application runs in the background

So I need my iphone application to run in the background and collect data from the accelerometer. Even though I managed to do the former by playing background music, the accelerometer seems to stop sending data to the application when the app is not in theforeground.
Is there any workaround for this?
I've seen there's a trick to make it not go asleep:
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
... but my problem is that I have to run and see the GUI of another application when this other one is running.
Thanks.
P.S. I know the app store would reject my application. My project is only for demostration purposes.

GameKit bluetooth

I'm trying to understand the basic steps required for a game I'm trying to make using GameKit for bluetooth connections. I only have 1 device, so I can't test out my ideas very easily.
The game involves a maximum of 2 players over bluetooth via GameKit and the only data to send/receive is a 'shot' which includes position and direction data only.
This is what I think should happen, I really just want someone to point out any mistakes I've made:
The game starts by presenting a GKPeerPickerController with a connectionTypeMask of 'GKPeerPickerConnectionTypeNearby' and setting it's delegate to 'self'.
The GKPeerPickerController delegate method 'peerPickerController:didConnectPeer:toSession:' is implemented and sets the sessions dataReceiveHandler to self. The peerID is added to an NSMutableArray which holds all peer IDs connected. (There should only be 1 peer connected at any time).
To enforce only 1 peer connection per game the method 'session:didReceiveConnectionRequestFromPeer:' checks the 'count' property of the NSMutableArray which holds the connected peerIDs. If 'count'>0 then we respond by calling: 'denyConnectionFromPeer:' else, we call: 'acceptConnectionFromPeer:error:'
To send a shot, the game calls: 'sendData:toPeers:withDataMode:error:'
To receive shots, the game responds to: 'receiveData:fromPeer:inSession:context:'
Are there any steps missing? For example, if a user tries to connect to me before I connect to them, what will happen with my GKPeerPickerController? How do I know to dismiss it?
And without another iPhone/iPod I can't test.
First off, if you want to test Bluetooth in Gamekit you really are going to need a 2nd device. If you're serious about your app, try and buy a cheap/refurbished iPhone or iPod Touch that will support GameKit Bluetooth (the oldest devices don't do it). I actually think you do even better with two Macs for testing, but if you don't already have a second one that's an expensive proposition.
What you've posted above is pretty much correct - actually using the PeerPicker is pretty straightforward and you should be able to get through it easily using Apple's docs. The problem is that in my experience, it's a bit janky. It might be because I'm using a 3GS and a 3G to test and they have different Bluetooth inside, but a lot of times I see weird things when the scenario you outlined occurred - "if a user tries to connect to me before I connect to them." In the end it typically still works, but not without a little overhead of waiting and perhaps the user having to repeat the process a time or two.

How do I use AsyncSocket in a worker thread instead of main Thread?

I have a iPhone app that connects to an IP camera and retrieve MJPEG data using GET method. Everything seems to work find on the simulator, but on the device the UI seems to be blocking: Whenever i receive an image data, I load it into uiimageview. It seems like the only way out is to have AsyncSocket running on a background thread and set the image on the mainThread.
How do I do that for AsyncSocket?
There is a very simple asynchronous socket pattern that can be created using the NSStream object. Take a look at the WiTap sample on the iphone developer site for an example of how to implement it.
If implemented as in the WiTap example you probably wont have any need for a background thread. Should you still find a need for a background thread keep in mind you will probably have to manually process the runloop for that thread in order to keep your socket's operating properly. Take a look at the NSRunloop documentation for more information on that.