I am using ABAddressBookRegisterExternalChangeCallback to get the external changes in AddressbookBook of the user. I am using the following code to register the callback:
ABAddressBookRef ntificationaddressbook = ABAddressBookCreate();
ABAddressBookRegisterExternalChangeCallback(ntificationaddressbook, MyAddressBookExternalChangeCallback, self);
and when this callback is called then MyAddressBookExternalChangeCallback is called successfully
void MyAddressBookExternalChangeCallback (ABAddressBookRef ntificationaddressbook,CFDictionaryRef info,void *context)
{
NSLog(#"Changed Detected......");
}
I have the following questions:
How can i detect which contacts was changed, and which action (ADD,Update,Delete) was performed on that Contact. I need to get the recordID of that contact. Is it Possible ?
Unfortunately if thats not possible then how the apps like viber, tango, watsapp gets the change information ?
I gets the callback method called only when the app is in background if the app is terminated is there any way to get the changes notification.
Please help. Thanks in advance.
I found the solution to find what was changed in the addressbook. I am not sure if its a precise one, But some how that works.
Create a Callback Notification for Addressbook:-
ABAddressBookRef ntificationaddressbook = ABAddressBookCreate();
ABAddressBookRegisterExternalChangeCallback(ntificationaddressbook, MyAddressBookExternalChangeCallback, self);
Than Add the following code when notification Occurs:-
void MyAddressBookExternalChangeCallback (ABAddressBookRef ntificationaddressbook,CFDictionaryRef info,void *context)
{
NSTimeInterval timeStampone = [[NSDate date] timeIntervalSince1970];
NSNumber *timeStamponeobj= [NSNumber numberWithDouble: timeStampone];
NSLog(#"Start Process");
NSUserDefaults *userdefs = [[NSUserDefaults alloc]init];
NSMutableArray *arraytemp = [[NSMutableArray alloc]init];
CFArrayRef peopleRefs = ABAddressBookCopyArrayOfAllPeopleInSource(ntificationaddressbook, kABSourceTypeLocal);
NSMutableArray *changedrecords = [[NSMutableArray alloc]init];
ABAddressBookRevert(ntificationaddressbook);
CFIndex count = CFArrayGetCount(peopleRefs);
for (CFIndex i=0; i < count; i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(peopleRefs, i);
NSDate* datemod = ( NSDate *)(ABRecordCopyValue(ref, kABPersonModificationDateProperty));
NSDate *lastopened = [userdefs valueForKey:#"LastOpenedDate"];
NSTimeInterval datemodifiedtime =[datemod timeIntervalSince1970];
NSNumber *modifieddatenumber= [NSNumber numberWithDouble: datemodifiedtime];
NSTimeInterval lastopeddate = [lastopened timeIntervalSince1970];
NSNumber *lastopenednumber= [NSNumber numberWithDouble: lastopeddate];
if ([modifieddatenumber intValue]>[lastopenednumber intValue]) {
ABRecordRef aSource = CFArrayGetValueAtIndex(peopleRefs,i);
int recordid = ABRecordGetRecordID(aSource);
[changedrecords addObject:[NSString stringWithFormat:#"%d",recordid]];
}
}
NSString *arraystring= [changedrecords componentsJoinedByString:#","];
[userdefs setValue:arraystring forKey:#"ArrayOfChangedPeopleString"];
[userdefs setValue:changedrecords forKey:#"ArrayOfChangedPeople"];
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ([arraystring isEqualToString:#"(null)"]) {
arraystring = [arraystring stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
}
NSLog(#"arraychangedstring= %# and length = %d",arraystring,arraystring.length);
NSLog(#"Must send data to the server here for this changed people array= %#",changedrecords);
if (arraystring.length>2) {
NSLog(#"inside if");
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allSources = ABAddressBookCopyArrayOfAllPeople( addressBook );
[userdefs setValue:#"" forKey:#"ArrayOfChangedPeopleString"];
[userdefs setValue:#"" forKey:#"ArrayOfChangedPeople"];
for (int i = 0; i<[changedrecords count]; i++) {
NSString *recordid= [changedrecords objectAtIndex:i];
int nPeople = [recordid intValue];
[IntoochUtil current_function_name:#"Inside getcontactdetails - Start"];
if(nPeople != 0){
// for (CFIndex i = nPeople_start; i < (nPeople_end); i++)
// {
// NSString *reocrdid = [NSString stringWithFormat: #"Record %ld",i];
// NSLog(#"Recordid= %#",reocrdid);
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
//common field
NSString *first_name =nil;
NSString *mid_name = nil ;
NSString *last_name =nil ;
NSString *basic_email =nil ;
NSString *basic_mobile =nil ;
NSString *other_phone=nil;
NSString *pager_phone= nil;
//personal filed
NSString *Home_email =nil ;
NSString*home_mobile =nil ;
NSString*home_address =nil;
//business filed
NSString *work_email =nil ;
NSString *company_name =nil ;
NSString *job_title =nil ;
NSString*work_mobile =nil ;
NSString *iphone = nil;
NSString *main = nil;
NSString *work_address =nil ;
NSString *home_fax, *work_fax, *other_fax =nil;
NSString *birthday = nil;
// NSString *blogs = nil;
NSString *record_id;
ABRecordRef aSource = ABAddressBookGetPersonWithRecordID(addressBook, nPeople);
// Fetch all groups included in the current source
// Getting Record id from address book
int recordid = ABRecordGetRecordID(aSource);
record_id = [NSString stringWithFormat:#"%d",recordid];
// Getting Names from address book
// CFStringRef firstName = ABRecordCopyValue(aSource, kABPersonFirstNameProperty);
first_name= ABRecordCopyValue(aSource, kABPersonFirstNameProperty);
// first_name=[NSString stringWithFormat:#"%#",firstName];
// CFRelease(firstName);
// NSLog(#"First name= %#",first_name);
NSMutableDictionary *dict_name = [[NSMutableDictionary alloc]init];
if (!([first_name isEqualToString:#"(null)"]|| first_name == nil || first_name.length ==0))
{
[dict_name setValue:first_name forKey:#"FirstName"];
}
[first_name release];
mid_name= ABRecordCopyValue(aSource, kABPersonMiddleNameProperty);
// NSLog(#"mid name = %#",mid_name);
// CFRelease(midname);
if (!([mid_name isEqualToString:#"(null)"]|| mid_name == nil || mid_name.length ==0))
{
[dict_name setValue:mid_name forKey:#"MiddleName"];
}
[mid_name release];
last_name = ABRecordCopyValue(aSource, kABPersonLastNameProperty);
// NSLog(#"Last Name= %#",last_name);
// CFRelease(lastName);
if (!([last_name isEqualToString:#"(null)"]|| last_name == nil || last_name.length ==0))
{
[dict_name setValue:last_name forKey:#"LastName"];
}
[last_name release];
NSString *dictNamestring = [NSString stringWithFormat:#"%#",dict_name];
if (!([dictNamestring isEqualToString:#"(null)"]|| dictNamestring == nil || dictNamestring.length <4))
{
[dict setValue:dict_name forKey:#"Names"];
}
// [dict_name release];
// Getting Phone numbers from address book
NSMutableDictionary *dict_phone = [[NSMutableDictionary alloc]init];
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty);
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
home_mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i) ;
[dict_phone setValue:home_mobile forKey:#"Mobile"];
}
else if ([mobileLabel isEqualToString:(NSString*)kABHomeLabel])
{
basic_mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[dict_phone setValue:basic_mobile forKey:#"Home"];
}
else if([mobileLabel isEqualToString:(NSString *)kABWorkLabel])
{
work_mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[dict_phone setValue:work_mobile forKey:#"Work"];
}
else if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel])
{
iphone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[dict_phone setValue:iphone forKey:#"iPhone"];
}
else if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMainLabel])
{
main = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[dict_phone setValue:main forKey:#"Main"];
}
else if ([mobileLabel isEqualToString:(NSString*)kABOtherLabel])
{
other_phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[dict_phone setValue:other_phone forKey:#"OtherPhone"];
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel])
{
pager_phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
[dict_phone setValue:pager_phone forKey:#"Pager"];
}
else
{
NSLog(#"%# label is not processed",mobileLabel);
}
}
CFRelease(phones);
NSString *dictphonestring = [NSString stringWithFormat:#"%#",dict_phone];
if (!([dictphonestring isEqualToString:#"(null)"]|| dictphonestring == nil || dictphonestring.length <4))
{
[dict setValue:dict_phone forKey:#"PhoneNos"];
}
// [dict_phone release];
// Getting Fax numbers from address book
NSMutableDictionary *dict_fax = [[NSMutableDictionary alloc]init];
ABMultiValueRef faxes =(NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty);
NSString* FaxLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(faxes); i++) {
FaxLabel = (NSString*)ABMultiValueCopyLabelAtIndex(faxes, i);
if([FaxLabel isEqualToString:(NSString *)kABPersonPhoneHomeFAXLabel])
{
home_fax = (NSString*)ABMultiValueCopyValueAtIndex(faxes, i) ;
[dict_fax setValue:home_fax forKey:#"Home"];
}
if ([FaxLabel isEqualToString:(NSString*)kABPersonPhoneWorkFAXLabel])
{
work_fax = (NSString*)ABMultiValueCopyValueAtIndex(faxes, i);
[dict_fax setValue:work_fax forKey:#"Work"];
}
if([FaxLabel isEqualToString:(NSString *)kABPersonPhoneOtherFAXLabel])
{
other_fax = (NSString*)ABMultiValueCopyValueAtIndex(faxes, i);
[dict_fax setValue:other_fax forKey:#"Other"];
}
}
CFRelease(faxes);
NSString *dictfaxstring = [NSString stringWithFormat:#"%#",dict_fax];
if (!([dictfaxstring isEqualToString:#"(null)"]|| dictfaxstring == nil || dictfaxstring.length <4))
{
[dict setValue:dict_fax forKey:#"FaxNos"];
}
[dict_fax release];
// Getting emails from address book
ABMultiValueRef email = ABRecordCopyValue(aSource, kABPersonEmailProperty);
NSMutableDictionary *dict_email = [[NSMutableDictionary alloc]init];
NSString* email_lbl;
for(CFIndex i = 0; i < ABMultiValueGetCount(email); i++) {
email_lbl = (NSString*)ABMultiValueCopyLabelAtIndex(email, i);
if([email_lbl isEqualToString:(NSString *)kABHomeLabel])
{
Home_email = [(NSString*)ABMultiValueCopyValueAtIndex(email, i)retain];
[dict_email setValue:Home_email forKey:#"Home"];
}
else if([email_lbl isEqualToString:(NSString *)kABWorkLabel])
{
work_email = [(NSString*)ABMultiValueCopyValueAtIndex(email, i)retain];
[dict_email setValue:work_email forKey:#"Work"];
}
else if([email_lbl isEqualToString:(NSString *)kABOtherLabel])
{
basic_email = [(NSString*)ABMultiValueCopyValueAtIndex(email, i)retain];
[dict_email setValue:basic_email forKey:#"Other"];
}
else
{
NSLog(#"%# label is processed as other",email_lbl);
basic_email = [(NSString*)ABMultiValueCopyValueAtIndex(email, i)retain];
[dict_email setValue:basic_email forKey:#"Other"];
}
}
CFRelease(email);
NSString *dictemailstring = [NSString stringWithFormat:#"%#",dict_email];
if (!([dictemailstring isEqualToString:#"(null)"]|| dictemailstring == nil || dictemailstring.length <4))
{
[dict setValue:dict_email forKey:#"Emails"];
}
// [dict_email release];
company_name = ABRecordCopyValue(aSource, kABPersonOrganizationProperty);
// NSLog(#"Company name= %#",company_name);
job_title = ABRecordCopyValue(aSource, kABPersonJobTitleProperty);
// NSLog(#"Job title= %#",job_title);
NSDate *birthdate = ABRecordCopyValue(aSource, kABPersonBirthdayProperty);
NSString *birthdate_string = [NSString stringWithFormat:#"%#",birthdate];
if (!([birthdate_string isEqualToString:#"(null)"]|| birthdate_string == nil || birthdate_string.length ==0)) {
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:birthdate];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
birthday = [NSString stringWithFormat:#"%d-%d-%d",day,month,year];
NSLog(#"birthday= %#",birthday);
}
[birthdate release];
// Getting addressess from address book
ABMultiValueRef address =(NSString*)ABRecordCopyValue(aSource, kABPersonAddressProperty);
NSDictionary *dict_address = [[NSMutableDictionary alloc]init];
NSString* addLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(address); i++) {
addLabel=(NSString*)ABMultiValueCopyLabelAtIndex(address, i);
if ([addLabel isEqualToString:(NSString*)kABWorkLabel]) {
NSMutableDictionary *dict_work_add = [(NSMutableDictionary *)ABMultiValueCopyValueAtIndex(address, i)retain];
work_address=[NSString stringWithFormat:#"%# %# %# %# %#",[dict_work_add objectForKey:#"Street"],[dict_work_add objectForKey:#"City"],[dict_work_add objectForKey:#"State"],[dict_work_add objectForKey:#"Country"],[dict_work_add objectForKey:#"ZIP"]] ;
work_address = [work_address stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
work_address = [work_address stringByReplacingOccurrencesOfString:#"\n" withString:#""];
// NSLog(#"work address = [%#] and length= %d",work_address,work_address.length);
if (!([work_address isEqualToString:#"(null)"]|| work_address == nil || work_address.length ==0))
{
[dict_address setValue:work_address forKey:#"Work"];
}
}
if ([addLabel isEqualToString:(NSString*)kABHomeLabel])
{
NSMutableDictionary *dict_home_add = [(NSMutableDictionary *)ABMultiValueCopyValueAtIndex(address, i)retain];
home_address = [NSString stringWithFormat:#"%# %# %# %# %#",[dict_home_add objectForKey:#"Street"],[dict_home_add objectForKey:#"City"],[dict_home_add objectForKey:#"State"],[dict_home_add objectForKey:#"Country"],[dict_home_add objectForKey:#"ZIP"]];
home_address = [home_address stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
home_address = [home_address stringByReplacingOccurrencesOfString:#"\n" withString:#""];
// NSLog(#"home address = [%#] and length= %d",home_address,home_address.length);
if (!([home_address isEqualToString:#"(null)"]|| home_address == nil || home_address.length ==0))
{
[dict_address setValue:home_address forKey:#"Home"];
}
}
}
CFRelease(address);
NSString *dictaddrssstring = [NSString stringWithFormat:#"%#",dict_address];
if (!([dictaddrssstring isEqualToString:#"(null)"]|| dictaddrssstring == nil || dictaddrssstring.length <4))
{
[dict setValue:dict_address forKey:#"Addresses"];
}
[dict_address release];
// Getting Social Networks from address book
ABMultiValueRef social =(NSString*)ABRecordCopyValue(aSource, kABPersonSocialProfileProperty);
NSMutableDictionary *dict_social = [[NSMutableDictionary alloc]init];
if (social) {
for (int i = 0 ; i < ABMultiValueGetCount(social); i++) {
NSDictionary *socialDict = (NSDictionary *)ABMultiValueCopyValueAtIndex(social, i);
NSString *twitterid;
if ([socialDict[#"service"] isEqualToString:(NSString*)kABPersonSocialProfileServiceTwitter]) {
twitterid = (NSString*)socialDict[#"username"];
[dict_social setValue:twitterid forKey:#"Twitter"];
}
NSString *facebookid;
if ([socialDict[#"service"] isEqualToString:(NSString*)kABPersonSocialProfileServiceFacebook]) {
facebookid = (NSString*)socialDict[#"username"];
[dict_social setValue:facebookid forKey:#"Facebook"];
}
NSString *linkedinid;
if ([socialDict[#"service"] isEqualToString:(NSString*)kABPersonSocialProfileServiceLinkedIn]) {
linkedinid = (NSString*)socialDict[#"username"];
[dict_social setValue:linkedinid forKey:#"LinkedIn"];
}
NSString *gamecenterid;
if ([socialDict[#"service"] isEqualToString:(NSString*)kABPersonSocialProfileServiceGameCenter]) {
gamecenterid = (NSString*)socialDict[#"username"];
[dict_social setValue:gamecenterid forKey:#"GameCenter"];
}
NSString *myspaceid;
if ([socialDict[#"service"] isEqualToString:(NSString*)kABPersonSocialProfileServiceMyspace]) {
myspaceid = (NSString*)socialDict[#"username"];
[dict_social setValue:myspaceid forKey:#"MySpace"];
}
NSString *flickerid;
if ([socialDict[#"service"] isEqualToString:(NSString*)kABPersonSocialProfileServiceFlickr]) {
flickerid = (NSString*)socialDict[#"username"];
[dict_social setValue:flickerid forKey:#"Flicker"];
}
[socialDict release];
}
CFRelease(social);
}
NSString *dictsocialstring = [NSString stringWithFormat:#"%#",dict_social];
if (!([dictsocialstring isEqualToString:#"(null)"]|| dictsocialstring == nil || dictsocialstring.length <4))
{
[dict setValue:dict_social forKey:#"SocialNetworks"];
}
[dict_social release];
// Getting Other Fields from address book
NSMutableDictionary *dict_others = [[NSMutableDictionary alloc]init];
if (!([company_name isEqualToString:#"(null)"]||company_name == nil || company_name.length ==0)) {
[dict_others setValue:company_name forKey:#"CompanyName"];
}
if (!([job_title isEqualToString:#"(null)"]||job_title == nil || job_title.length ==0)) {
[dict_others setValue:job_title forKey:#"JobTitle"];
}
if (!([birthday isEqualToString:#"1-1-2001"]||[birthday isEqualToString:#"(null)"]||birthday == nil || birthday.length ==0)) {
[dict_others setValue:birthday forKey:#"BirthDay"];
}
NSString *dictothersstring = [NSString stringWithFormat:#"%#",dict_others];
if (!([dictothersstring isEqualToString:#"(null)"]|| dictothersstring == nil || dictothersstring.length <4))
{
[dict setValue:dict_others forKey:#"OtherInfo"];
}
[dict_others release];
[dict setValue:record_id forKey:#"RecordID"];
//This NSLog(#"Value for Dict is========= %# =========",dict);
NSString *namedictionstring = [NSString stringWithFormat:#"%#",dict_name];
NSString *emaildictstring = [NSString stringWithFormat:#"%#",dict_email];
NSString *phonedictstring = [NSString stringWithFormat:#"%#",dict_phone];
// NSLog(#"name dict length= %d email dict length= %d phone dict length = %d",namedictionstring.length,emaildictstring.length,phonedictstring.length);
// [array_addressbook addObject:dict];
if (phonedictstring.length >3) {
[arraytemp addObject:dict];
}
else
{
if (emaildictstring.length>3 && namedictionstring.length) {
[arraytemp addObject:dict] ;
}
else
{
NSLog(#"Contact is not valid= %#",dict_name);
}
}
[dict_name release];
[dict_phone release];
[dict_email release];
[dict release];
[company_name release];
[job_title release];
/* CFRelease(firstName);
CFRelease(midname);
CFRelease(lastName);
CFRelease(jobtitle);
CFRelease(birthday_c);
CFRelease(companyname);*/
// }
}
}
NSString *json_array_string = [arraytemp JSONRepresentation];
[IntoochUtil current_function_name:#"Inside getcontactdetails - End"];
NSLog(#"Json String To send= %#",json_array_string);
if (allSources) {
CFRelease(allSources);
}
if (addressBook) {
CFRelease(addressBook);
}
}
else
{
NSLog(#"This is empty.... nothing to perform");
NSMutableArray *arrayofdeletedpeoples= [[NSMutableArray alloc]init];
ABAddressBookRef addressbook = ABAddressBookCreate();
CFArrayRef allPeoples= ABAddressBookCopyArrayOfAllPeople(addressbook);
NSMutableArray *arrayofrecordIDCallback = [[NSMutableArray alloc]init];
int count = CFArrayGetCount(allPeoples);
for (int i =0; i<count; i++) {
ABRecordRef aSource = CFArrayGetValueAtIndex(allPeoples,i);
NSString * recordid = [NSString stringWithFormat:#"%d",ABRecordGetRecordID(aSource)];
[arrayofrecordIDCallback addObject:recordid];
}
NSLog(#"arrayofrecordIDCallback = %# and count= %d",arrayofrecordIDCallback,[arrayofrecordIDCallback count]);
NSMutableArray *arrayofallrecordids = [userdefs valueForKey:#"AllRecordIDSBackGround"];
NSLog(#"arrayofallrecordids= %# and count= %d",arrayofallrecordids,[arrayofallrecordids count]);
for (int i=0 ; i< [arrayofallrecordids count]; i++) {
if ([arrayofrecordIDCallback containsObject:[arrayofallrecordids objectAtIndex:i]]) {
}
else
{
[arrayofdeletedpeoples addObject:[arrayofallrecordids objectAtIndex:i]];
}
}
NSLog(#"Array of deleted peoples= %#",arrayofdeletedpeoples);
[arrayofdeletedpeoples release];
[arrayofallrecordids release];
[arrayofrecordIDCallback release];
if (allPeoples) {
CFRelease(allPeoples);
}
if (addressbook) {
CFRelease(addressbook);
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[changedrecords release];
[userdefs release];
[arraytemp release];
NSLog(#"Ends Process");
NSTimeInterval timeStamptwo = [[NSDate date] timeIntervalSince1970];
NSNumber *timeStamptwoObj = [NSNumber numberWithDouble: timeStamptwo];
NSLog(#"Time Difference= %d",[timeStamptwoObj intValue]-[timeStamponeobj intValue]);
}
Yes thats true that you can not get specific notification on Phonebook changes. You will need to synch your data when callback notification fire.
void MyAddressBookExternalChangeCallback (ABAddressBookRef ntificationaddressbook,CFDictionaryRef info,void *context)
{
NSLog(#"Changed Detected......");
// synch data
}
The apps like viber, tango, watsapp also does the same thing but in Background thread.
If the app is terminated, then you will need to synch data when app gets started like in didFinishLaunchingWithOptions.
As far I know, you can't tell what are the difference - I bumped to this problem myself.
My solution was:
I had a local Core Data based address book, and every time I got this notification, I ran a background thread that synchronised the data, and notify in the end all the changes.
During the first time, I am dumping all the data in local database using sqlite, then on external change I am dumping new data from contacts database in dummy db in sqlite, then firing big long compare query based on what has changed. In our case, main focus was on phone numbers and name. Once you get change update that in your contacts db in sqlite.
I think even whatsapp is doing something similar. I checked the time taken to reflect the changes by whatsapp and our app almost are similar. Actually, my app is 2 secs faster than whatsapp in terms of reflecting changes.
Hope this will help.
In my application i have got data in Mutable array like this,
MutableArray1:(
"22.298166 , 73.165809",
"22.300598 , 73.167183",
"22.298101 , 73.166188",
"22.298128 , 73.166194"
"22.298130 , 73.166194"
)
I want to compare a NSString with data "22.298130 , 73.166194" with last three data of MutableArray1.
Please suggest me how can i do that?
if([Array1 count]>3)
{
for (int i = [Array1 count] - 4; i < [Array1 count]; i++) {
if ([[Array1 objectAtIndex:i] isEqualToString:#"22.298130 , 73.166194"]) {
NSLog (#"True");
//Write your Code Here
}
}
}
NSArray *array = #[ #"22.298166 , 73.165809",
#"22.300598 , 73.167183",
#"22.298101 , 73.166188",
#"22.298128 , 73.166194",
#"22.298130 , 73.166194"];
If you just need to know, if it is inside the array:
NSString *searchString = #"22.298130 , 73.166194";
BOOL found = [[array subarrayWithRange:NSMakeRange([array count]-3, 3)] containsObject:searchString];
NSLog(#"%#", (found) ? #"YES" : #"NO");
if you need to know the index, you can do
[[array subarrayWithRange:NSMakeRange([array count]-3, 3)] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
if ([obj isEqualToString:searchString]) {
*stop = YES; //avoid further loops, if we had a positive hit.
NSLog(#"%lu %lu", idx, [array count]-3+idx); //index in subarray and in original array
};
}];
I have an array which contains strings like frame_10#3x.png , frame_5#3x.png,frame_19#3x.png etc.
So I want to sort this array according to the number after the underscore i.e. the correct sequence will be frame_5#3x.png,frame_10#3x.png,frame_19#3x.png.
I tried to use the following method but no result:
NSInteger firstNumSort(id str1, id str2, void *context) {
int num1 = [str1 integerValue];
int num2 = [str2 integerValue];
if (num1 < num2)
return NSOrderedAscending;
else if (num1 > num2)
return NSOrderedDescending;
return NSOrderedSame;
}
Please suggest how to do this sorting for array.
NSArray *sry_img = [[NSArray alloc] initWithObjects:#"frame_18#3x.png",#"frame_17#3x.png",#"frame_1222#3x.png",#"frame_10#3x.png",#"frame_3#3x.png",#"frame_4#3x.png",#"frame_4#3x.png",#"frame_1#3x.png",#"frame_4#3x.png",#"frame_4#3x.png",nil];
NSArray *sortedStrings = [sry_img sortedArrayUsingSelector:#selector(localizedStandardCompare:)];
NSLog(#"%#",sortedStrings);
Enjy .......
But
localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.
you want to do something like:
NSArray *components1 = [str1 componentsSeparatedByString:#"_"];
NSArray *components2 = [str2 componentsSeparatedByString:#"_"];
NSString *number1String = [components1 objectAtIndex:([components1 count] - 1])];
NSString *number2String = [components2 objectAtIndex:([components2 count] - 1])];
return [number1String compare:number2String];
I am not sure if my solution is the best possible approach but it can solve your problem for the time being :) .
1) First I have written a function to get the numbers before # character in your string and then I implemented simple SELECTION SORT algo to sort the array using this functions.
- (NSString*)getSubStringForString:(NSString*)value {
// First we will cut the frame_ string
NSMutableString *trimmedString = [NSMutableString stringWithString:[value substringWithRange:NSMakeRange(6, [value length]-6)]];
// New String to contain the numbers
NSMutableString *newString = [[NSMutableString alloc] init];
for (int i = 0; i < [trimmedString length] ; i++) {
NSString *singleChar = [trimmedString substringWithRange:NSMakeRange(i, 1)];
if (![singleChar isEqualToString:#"#"]) {
[newString appendString:singleChar];
} else {
break;
}
}
return newString;
}
This is the selection Implementation of the algo for sorting. The main logic is in the for loop. You can copy the code in viewDidLoad method to test.
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"frame_10#3x.png",#"frame_5#3x.png",
#"frame_3#3x.png", #"frame_19#3x.png",
nil];
NSLog(#"Values before Sort: %#", array);
int iPos;
int iMin;
for (iPos = 0; iPos < [array count]; iPos++)
{
iMin = iPos;
for (int i = iPos+1; i < [array count]; i++)
{
if ([[self getSubStringForString:[array objectAtIndex:i]] intValue] >
[[self getSubStringForString:[array objectAtIndex:iMin]] intValue]) {
iMin = i;
}
}
if ( iMin != iPos )
{
NSString *tempValue = [array objectAtIndex:iPos];
[array replaceObjectAtIndex:iPos withObject:[array objectAtIndex:iMin]];
[array replaceObjectAtIndex:iMin withObject:tempValue];
}
}
NSLog(#"Sorted Values: %#", array);
I hope that it can atleast keep you going. :)
You can try this-
NSString *str1 = [[[[str1 componentsSeparatedByString:#"frame_"] objectAtIndex:1] componentsSeparatedByString:#"#3x.png"] objectAtIndex:0];
int num1 = [str1 integerValue];
I am having trouble trapping an empty result set from FMDB.
The code is below. I am getting NSLog's from the database opening and closing and NSLog "1" but none of the ones in the If statement!
If I have data in the database its fine, but I want to trap and edit result if the database is empty.
[self openDatabase];
NSNumberFormatter *nfcurrency = [[NSNumberFormatter alloc]init];
[nfcurrency setNumberStyle:NSNumberFormatterCurrencyStyle];
[nfcurrency setLocale:[NSLocale currentLocale]];
FMResultSet *result = [[self getDatabase]executeQuery:#"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
//FMResultSet *result = [[self getDatabase]executeQuery:#"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
NSLog(#"1");
if (result == NULL) {
NSLog(#"Last BFNeeded Result = nil");
} else {
while ([result next]) {
NSLog(#"HERE");
NSString *lastBFNeeded = [nfcurrency stringFromNumber:[NSNumber numberWithDouble:[result doubleForColumn:#"BFNeeded"]]];
NSLog(#"lastBFNeeded=%#",lastBFNeeded);
}
}
NSLog(#"ClosingDB");
[self closeDatabase];
Continuing after getting first reply:
I can't get hasAnotherRow to work as expected.
I have this code:
FMResultSet *result = [[self getDatabase]executeQuery:#"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 0,1;"];
if (result == nil) {
NSLog(#"Last BFNeeded Result = nil");
}
else {
NSLog(#"has results1: %#", [result hasAnotherRow] ? #"YES" : #"NO");
while ([result next]) {
NSLog(#"has results2: %#", [result hasAnotherRow] ? #"YES" : #"NO");
}
}
With a database that returns a result, I get result1 NO, result2 YES so I assume the hasAnotherRow must go inside the while ([result next]) loop.
However with an empty database, I get result1 NO and it doesn't even get to result2!
the "result" will never be nil for a query that produces 0 rows.
also, you shouldn't compare object pointers to NULL -- compare to nil. See this question: NULL vs nil in Objective-C
try this:
FMResultSet *result = [[self getDatabase]executeQuery:#"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
NSLog ( #"has results: %#", [result hasAnotherRow] ? #"YES" : #"NO" );
NSInteger count;
query=[NSString stringWithFormat:#"select Count(*) from %# where name = 'brandon' ",dbName];
results = [database executeQuery:query ];
while([results next]) {
count = [results intForColumnIndex:0];
NSLog(#"count:%d",count);
}
will set count equal to zero if there is no entry
I'm developing a Twitter app and I'm having problems with adding tweets to an NSArray. The problem is I get the tweets, and all the content ok, but the last query i got is just for control, so it doesn't contains the object "tweet" (which is the tweet content). I have tried stopping it if it detects the control query but no luck. This is the normal search query:
See dictionary: {
"created_at" = 1305363612;
"from_user" = IamDeShayDenise;
"from_user_id" = 123375840;
"from_user_id_str" = 123375840;
geo = "";
id = 69326131068813312;
"id_str" = 69326131068813312;
"iso_language_code" = en;
metadata = {
"result_type" = recent;
};
"profile_image_url" = "http://a3.twimg.com/profile_images/1336852311/110302-003005_normal.jpg";
source = "web";
"source_api_request_type" = 33;
text = "#LondonBlackk Yeah....and Yes I was With My Mommy and Nana!!! && Still Pulling All The Guys...LoL";
"to_user" = LondonBlackk;
"to_user_id" = 172117314;
"to_user_id_str" = 172117314;
}
In that one I get the text object ok and I can read it. The problem comes when I get this:
See dictionary: {
"source_api_request_type" = 33;
}
Or this:
See dictionary: {
"completed_in" = "0.027116";
"max_id" = 69324964586725376;
"max_id_str" = 69324964586725376;
"next_page" = "?page=2&max_id=69324964586725376&q=lol";
page = 1;
query = lol;
"refresh_url" = "?since_id=69324964586725376&q=lol";
"results_per_page" = 15;
"since_id" = 1;
"since_id_str" = 1;
"source_api_request_type" = 33;
warning = "since_id removed for pagination.";
}
And this is the code I use for filling the arrays:
tweets = [[NSMutableArray alloc] init];
usernames = [[NSMutableArray alloc] init];
for(NSDictionary *d in searchResults) {
NSLog(#"See dictionary: %#", d);
Tweet *author = [d objectForKey:#"from_user"];
Tweet *tweet = [d objectForKey:#"text"];
if ([d objectForKey:#"from_user"] == nil || [d objectForKey:#"text"] == nil){
NSLog(#"end");
}
[usernames addObject:author];
[tweets addObject:tweet];
[author release];
[tweet release];
}
I don't know what can be wrong :(
Thanks in advance :D
*UPDATE:*Got it working in this way
if ([d objectForKey:#"from_user"] == nil || [d objectForKey:#"text"] == nil){
NSLog(#"end");
} else {
[usernames addObject:author];
[tweets addObject:tweet];
}
instead of:
if ([d objectForKey:#"from_user"] == nil || [d objectForKey:#"text"] == nil){
NSLog(#"end");
}
[usernames addObject:author];
[tweets addObject:tweet];
Then the other problem was also solved. Thanks y'all!!
The way of solving it is this:
if ([d objectForKey:#"from_user"] == nil || [d objectForKey:#"text"] == nil){
NSLog(#"end");
} else {
[usernames addObject:author];
[tweets addObject:tweet];
}
instead of:
if ([d objectForKey:#"from_user"] == nil || [d objectForKey:#"text"] == nil){
NSLog(#"end");
}
[usernames addObject:author];
[tweets addObject:tweet];