我有一个应用程序,其中我在相机胶卷中获得图像的CLLocation
纬度和经度。我可以得到那些坐标的地址。有没有办法将该地址与我的联系人地址进行比较,如果匹配,请创建一个名称与该联系地址对应的NSString
。
//首先获取CLLocation
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
if (asset == nil) return;
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
NSLog(@"lat;%f",location.coordinate.latitude);
NSLog(@"long;%f",location.coordinate.longitude);
//然后找到地址
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude]; //insert your coordinates
[ceo reverseGeocodeLocation:loc
completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
if (placemark) {
NSLog(@"placemark %@",placemark);
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
NSLog(@"addressDictionary %@", placemark.addressDictionary);
NSLog(@"placemark %@",placemark.region);
NSLog(@"placemark %@",placemark.country); // Give Country Name
NSLog(@"placemark %@",placemark.locality); // Extract the city name
NSLog(@"location %@",placemark.name);
NSLog(@"location %@",placemark.ocean);
NSLog(@"location %@",placemark.postalCode);
NSLog(@"location %@",placemark.subLocality);
NSLog(@"location %@",placemark.location);
//Print the location to console
NSLog(@"I am currently at %@",locatedAt);
}
}];
有没有办法将此地址与用户联系人中的地址进行比较,以查看是否存在匹配项
然后使用该联系人的名称创建NSString
所以我能够访问地址簿并获取它们所在的名字和姓氏以及索引。
我也能够找到为每个联系人输入的地址,但它是我无法使用的形式,因此我可以将图片的位置与中的位置进行比较地址簿。
下面是我的代码。任何人都可以帮我弄清楚如何获得每个联系人的地址,所以我也可以 比较街道地址和邮政编码或 将地址转换为一组可以比较的坐标
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add the contact
NSLog(@"granted");
} else {
// User denied access
// Display an alert telling user the contact could not be added
NSLog(@"denied");
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
NSLog(@"authorized");
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
//ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
for(int i = 0; i < numberOfPeople; i++) {
NSLog(@"i;%d",i);
NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName);
ABMutableMultiValueRef address = (ABRecordCopyValue(person, kABPersonAddressProperty));
if(ABMultiValueGetCount(address) > 0) {
[dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(address, 0) forKey:@"City"];
NSString *addressste = [dOfPerson valueForKey:@"City"];
NSLog(@"dOfPerson;%@",[dOfPerson description]);
NSLog(@"streetadr:%@",addressste);
}
NSLog(@"address:%@",address);
}
答案 0 :(得分:0)
从地址簿获取联系人,并将您的地址与联系街道地址进行比较,要从联系簿获取街道地址,本教程可能对您有所帮助
https://www.appcoda.com/ios-programming-import-contact-address-book/
答案 1 :(得分:0)
所以我想出了这个
if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
[self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]];
}
- (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url {
@try {
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
if (asset == nil) return;
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
//我获得资产位置
CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
//我将其分解为组件
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude]; //insert your coordinates
//然后我反转位置以获取街道地址和邮政编码并从那些
中创建一个字符串 [ceo reverseGeocodeLocation:loc
completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
if (placemark) {
NSLog(@"placemark %@",placemark);
NSString *strString =placemark.name;
NSString *midstring = [strString stringByAppendingString:@","];
if (placemark.postalCode == nil) {
postalcodeString = @"00000";
}
else{
postalcodeString = placemark.postalCode;
}
combineString = [midstring stringByAppendingString:postalcodeString];
NSLog(@"combineString:%@",combineString);
//然后我访问用户的地址簿,查找每个人的街道地址和邮政编码,并将它们放在一个字符串中。
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
//ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
for(int i = 0; i < numberOfPeople; i++) {
NSLog(@"i;%d",i);
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName);
ABMutableMultiValueRef address = (ABRecordCopyValue(person, kABPersonAddressProperty));
if(ABMultiValueGetCount(address) > 0) {
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(address, 0);
NSString *streetString = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
NSString *zipString = CFDictionaryGetValue(dict, kABPersonAddressZIPKey);
if (zipString == Nil) {
zipString = @"00000";
}
NSString *interstring = [streetString stringByAppendingString:@","];
NSString *allString = [interstring stringByAppendingString:zipString];
NSLog(@"allstring;%@ %@",allString, combineString);
//然后我将位置字符串与地址字符串进行比较。如果匹配,我保存该地址的人的姓名,并稍后将其附加到该地址以填写文本视图。
if ([combineString isEqualToString:allString]) {
NSLog(@"its a match");
NSLog(@"i:%d",i);
firstNameString = firstName;
}
else {
}
NSLog(@"streetString:%@",streetString);
NSLog(@"firstNameString:%@",firstNameString);
NSLog(@"combineString:%@",combineString);
}
}}
else {
NSLog(@"Could not locate");
}
}
];
// Do what you need with the file name here
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error) {
};
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
}
@catch (NSException *exception) {
}
感谢大家的帮助。