Browse Author: vanita.ladkat

Adding Event to Native calendar in iOS

Add Eventkit framework to your project

//Code Snippet:

EKEventStore *store;

store=[[EKEventStore alloc] init];

//for iOS 7.0 and above we need to have the permission of user for using eventkit that is the calendar

//ask for permission:

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)

{

// handle access here

if(granted)

{

//configure event:

EKEvent *addNewEvent=[EKEvent eventWithEventStore:store];

addNewEvent.startDate=fromDate;

addNewEvent.endDate=toDate;

EKCalendar *defaultCalendar=[store defaultCalendarForNewEvents];

addNewEvent.calendar=defaultCalendar;

EKRecurrenceRule *recRule=[[EKRecurrenceRule alloc] init];

addNewEvent.recurrenceRules=[NSArray arrayWithObject:recRule];

 

BOOL status= [store saveEvent:addNewEvent span:EKSpanThisEvent commit:YES error:&error];

if(status)

{

NSLog(@”Successfully saved”);

}

else

{

//error in saving

//                NSLog(@”error in saving: %@”,error);

}

}

}

];

 

 

Speech to text conversion in iOS

There are lots of Speech to text conversion techniques are available but each one have their pros and cons. The Google Speech API is more convenient. I tried lot of ways for the conversion, but Google  API is more efficient and quite easy but having insufficient documentation.

I found that the API accepts flac encoded audio file, but in iOS the flac format is not supported. Finally I found the way is to use audio file encoded with linearPCM and with bit rate of 16000.

The configuration for audio is:

Channels       : 1

Sample Rate    : 16000

Precision      : 16-bit

Sample Encoding: 16-bit Signed Integer PCM

 

The settings dictionary for audio recorder is:

NSDictionary *recordSettings = [NSDictionary

dictionaryWithObjectsAndKeys:

[NSNumber numberWithInt:AVAudioQualityMax],     AVEncoderAudioQualityKey,

[NSNumber numberWithInt:16],                    AVEncoderBitRateKey,

[NSNumber numberWithInt: 1],                    AVNumberOfChannelsKey,

[NSNumber numberWithFloat:16000],               AVSampleRateKey,

[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,

nil];

 

And pass the recorded file in NSData format to api as: the most important thing in this is the content type that one use. It should match the audio file configuration

NSString *urlString = [NSString stringWithFormat:@”https://www.google.com/speech-api/v2/recognize?xjerr=1&client=chromium&lang=en-US&key=%@”,GOOGLE_API_KEY]; //GOOGLE_API_KEY: is obtained from google API Access

NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setHTTPMethod:@”POST”];

[request setHTTPBody:byteData];

[request addValue:@”audio/l16; rate=16000″ forHTTPHeaderField:@”Content-Type”];

[request setURL:url];

[request setTimeoutInterval:15];

NSURLResponse *response;

NSError *error = nil;

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //the data with trancribed result


 

Handling Push Notification in iOS

The push notification is quite complex part to handle. Get it done in only Three easy steps

1] Register for push notification

2] Get Device token

3] Implement delegate methods to handle push notification

 

1] Register for push notification

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// register for remote notification

[[UIApplication sharedApplication]

registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert )];

 

//handling push notification: when app is not in running state either in background or foreground

if ([[launchOptions allKeys] containsObject:UIApplicationLaunchOptionsRemoteNotificationKey])

{

NSLog(@”received notification: %@”,launchOptions); //handle notification here with launchOptions dictionary

}

}

2] Get Device token

Receive token in delegate method

– (void)application:(UIApplication *)app

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken

{

NSString *tokenStr = [devToken description]; //here is the device token needed for Push notification

}

 

//delegate method for getting failure error of registering device

– (void)application:(UIApplication *)app

didFailToRegisterForRemoteNotificationsWithError:(NSError *)err

{

NSLog(@”Error in registration. Error: %@”, err);

}

 

3] Implement delegate methods to handle push notification

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

//called when application is running either in foreground or background

NSLog(@”received notification: %@”,userInfo); //handle notification here with userinfo dictionary

}

 

 

 

 

 

 


Need more help?

Hi there, was your problem or query resolved? If not & need more assistance, please do reach out to us at info@nanostuffs.com, we'll be more than delighted to help. Nanostuffs has 7+ years of extensive Salesforce & iOS/Android experience.
Holler Box