try & catch in iPhone? - iphone

The try{}catch construct is common to C++, Java & related languages. In the iOS SDK is there and any functionality like this?

#try {
// Try something
}
#catch (NSException * e) {
NSLog(#"Exception: %#", e);
}
#finally {
// Added to show finally works as well
}

Related

How to implement mute functionality in a PJSIP call on iOS

I wanted to implement Mute button in my call. I am working on a VOIP application for iPhone. Now when a call comes and user picks up, I want to display a Mute button so the user can mute the call or conference. I did the same through the PJSIP API.
-(int) mutethecall
{
pj_status_t status = pjsua_conf_adjust_rx_level (0,0);
status = pjsua_conf_adjust_tx_level (0,0);
return (PJ_SUCCESS == status);
}
-(int) unmutethecall
{
pj_status_t status = pjsua_conf_adjust_rx_level (0,1);
status = pjsua_conf_adjust_tx_level (0,1);
return (PJ_SUCCESS == status);
}
The problem is that while this code is working for one to one call, it's not working for conference scenarios.
I wonder if I could turn off the mic directly: could I implement the same using iOS bypassing the PJSIP API?
Is this possible?
You can completely disconnect the microphone from the conference using pjsua_conf_disconnect and pjsua_conf_connect when you want to unmute.
Here's some Objective-C code that does the trick:
+(void)muteMicrophone
{
#try {
if( pjsipConfAudioId != 0 ) {
NSLog(#"WC_SIPServer microphone disconnected from call");
pjsua_conf_disconnect(0, pjsipConfAudioId);
}
}
#catch (NSException *exception) {
NSLog(#"Unable to mute microphone: %#", exception);
}
}
+(void)unmuteMicrophone
{
#try {
if( pjsipConfAudioId != 0 ) {
NSLog(#"WC_SIPServer microphone reconnected to call");
pjsua_conf_connect(0,pjsipConfAudioId);
}
}
#catch (NSException *exception) {
NSLog(#"Unable to un-mute microphone: %#", exception);
}
}
Note that the pjsipConfAudioID was retrieved when the call was established, again in Objective-C...
static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
pjsua_call_info ci;
PJ_UNUSED_ARG(e);
pjsua_call_get_info(call_id, &ci);
pjsipConfAudioId = ci.conf_slot;
...
}
Hope that helps!

well coded exception handling - #try #catch or without it

I want to know the global mechanism for exception handling.
Like I do have 20 different classes and I want to put try catch in all , then It may be some what time consuming as well as may can create issues.
I found some code like
-(void)ExceptionOccure:(NSException *)exception
{
NSArray *stack = [exception callStackReturnAddresses];
NSLog(#"%#", stack);
}
But I am not able to call this method , using
-(void) applicationDidFinishLaunching: (UIApplication *) application
{
NSSetUncaughtExceptionHandler (&ExceptionOccure);
}
I came to know this way but not able to call any exception.
I want to create global mechanism for this exception handling. so that in any class if any exception occurs it will call the above written function and handle the exception as well as can store the stack in the database as I needed.
Can any one suggest me or provide me sample code for handling exception globally..
thanks in advance
your handler function is a C function so you have to implement it like this :
static void uncaughtExceptionHandler(NSException *exception)
{
NSArray *stack = [exception callStackReturnAddresses];
NSLog(#"%#", stack);
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
}

#catch not catching

In the code below I'm trying to catch an exception that is being thrown because the delegate doesn't implement the boolEntryCellViewControllerSegmentChanged method. However, the exception doesn't seem to be getting caught, the program is still crashing with an "unrecognized selector sent to instance" error. I'd be grateful for any pointers as to what I'm doing wrong.
#try
{
[delegate boolEntryCellViewControllerSegmentChanged:self];
}
#catch (NSException *exception)
{
NSLog(#"why doesn't this work?");
}
#finally
{
NSLog(#"why, why why?");
}
This is probably due to a known bug: http://www.openradar.me/8081169
In most cases, this only happens on the simulator. On the device itself, it usually works. But YMMV.

iphone NSCondition - App is crashing when "wait" is called

I am working on iphone app.
I created a custom class that derives from "NSCondition".
The following piece of code is working fine with iPhone SDK 3.1.3
- (bool)waitForFinish {
#try {
[self lock];
while (!done) {
[self wait];
}
}
#finally {
[self unlock];
}
return success;
}
The app is crashing on iPhone SDK 4.0 at line "[self wait]"
What might be the reason for crash in iPhone 4.0 SDK?
I noticed that the loop "while(!done)" is running for 4 times and then its crashing.
//Adding more info
-(void)releaseUse {
[self lock];
#try {
done = 1;
[self broadcast];
}
#finally {
[self unlock];
}
[self notifyComplete];
}
The above function is called when a file is downloaded so that it will send a broadcast message to come out of wait state.
Apple says that in iPhone OS 4, it fixed a bug related to NSCondition. (I don't know what's the bug). I think it might be causing error in iPhone SDK 4 and not in iPhone SDK 3.1

Iphone : Application crash because of old encodeObject after updating

Currently, my Iphone application is not released yet. When I worked with the simulator/device and I modify my application to add more cache into the encodeWithCode: and initWithCoder: . The problem is that when the application is loaded, I tried to use some of the encoded object which is not existing before. For example:
In the previous application version (e.g 1.2), I have this encode:
- (void)encodeWithCoder:(NSCoder*)coder {
[coder encodeObject:myArray forKey:NCITEMTABLE_ARCHIVE_HOME_ITEMS_KEY];
}
But with new version (e.g 1.3), I use this init:
- (id)initWithCoder:(NSCoder*)coder {
if (self = [super initWithCoder:coder]) {
myArray = [[coder decodeObjectForKey:NCITEMTABLE_ARCHIVE_HOME_ITEMS_KEY] retain];
myArray2 = [[coder decodeObjectForKey:NCITEMTABLE_ARCHIVE_HOME_ITEMS_2_KEY] retain];
}
return self;
}
and then the application will crash because it cannot find myArray2.
In the simulator or testing, I can just delete the old version and install from fresh. However, I am afraid that when it is released, I cannot tell my user to delete the old app and install the new fresh one. Have anyone experienced about this problem?
In your initWithCoder you should be able to just call containsValueForKey to see if the key exists before trying to call decodeObjectForKey
I tried to use try catch to get the exception. It may not be the best answer but it works now
. The problem is that it may have low performance when it has to do try catch exception, which is not recommended by Apple
if (archive) {
#try {
[self unarchiveInitializingWithData:archive];
}
#catch (NSException * e) {
NCLog (#"Cannot unarchive");
[self normalInitializing];
}
} else {
NCLog (#"Normal init");
// normal init
[self normalInitializing];
}