get selected text from a uiwebview Xcode - text

I have a UIWebView that loads text from an htmlString.
I need when the user selects a part of the text and presses a button, i will be capable of extracting it in order to use it elsewhere, so i am using this code :
// The JS File
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"HighlightedString" ofType:#"js" inDirectory:#""];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[WebV2 stringByEvaluatingJavaScriptFromString:jsString];
// The JS Function
NSString *startSearch = [NSString stringWithFormat:#"getHighlightedString()"];
[WebV2 stringByEvaluatingJavaScriptFromString:startSearch];
NSString *selectedText = [NSString stringWithFormat:#"selectedText"];
NSString * highlightedString = [WebV2 stringByEvaluatingJavaScriptFromString:selectedText];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Highlighted String"
message:highlightedString
delegate:nil
cancelButtonTitle:#"Oh Yeah"
otherButtonTitles:nil];
[alert show];
Along with HighlightedString.js :
/*!
------------------------------------------------------------------------
// Search Highlighted String
------------------------------------------------------------------------
*/
var selectedText = "";
function getHighlightedString() {
var text = window.getSelection();
selectedText = text.anchorNode.textContent.substr(text.anchorOffset, text.focusOffset - text.anchorOffset);
}
// ...
function stylizeHighlightedString() {
var range = window.getSelection().getRangeAt(0);
var selectionContents = range.extractContents();
var span = document.createElement("span");
span.appendChild(selectionContents);
span.setAttribute("class","uiWebviewHighlight");
span.style.backgroundColor = "black";
span.style.color = "white";
range.insertNode(span);
}
// helper function, recursively removes the highlights in elements and their childs
function uiWebview_RemoveAllHighlightsForElement(element) {
if (element) {
if (element.nodeType == 1) {
if (element.getAttribute("class") == "uiWebviewHighlight") {
var text = element.removeChild(element.firstChild);
element.parentNode.insertBefore(text,element);
element.parentNode.removeChild(element);
return true;
} else {
var normalize = false;
for (var i=element.childNodes.length-1; i>=0; i--) {
if (uiWebview_RemoveAllHighlightsForElement(element.childNodes[i])) {
normalize = true;
}
}
if (normalize) {
element.normalize();
}
}
}
}
return false;
}
// the main entry point to remove the highlights
function uiWebview_RemoveAllHighlights() {
selectedText = "";
uiWebview_RemoveAllHighlightsForElement(document.body);
}
I always get nothing as a result ... The alert view shows nothing...What's the problem with this code ? Any help ? Any ideas ? It will be really appreciated.

The solution was actually pretty simple and no need for all the above code!
For any future users just use:
NSString *textToSpeech = [WebV2 stringByEvaluatingJavaScriptFromString: #"window.getSelection().toString()"];
NSLog(#" -**-*--****-*---**--*-* This is the new select text %#",[WebV2 stringByEvaluatingJavaScriptFromString: #"window.getSelection().toString()"] );

NSString *theSelectedText = [self.webView stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
This will pass your selection to the string variable.

Related

Control Center, doesn't pause/play my audio stream

Hope that the issue goes to the correct stream.
I play HLS, i.e. I use AVPlayer for playing a mixed content( audio & pure video) from a server.
I keep playing audio after moving the app into background: I have enabled such feature in plist, plus I do store Prev Player, disable visual tracks, and set to nil AVPlayerLayer.
Here are the methods:
- (void)changePlayerState:(BOOL)restored
{
if (restored && !self.storedPlayer)
return;
if (!restored){
self.storedPlayer = self.playerView.player;
[self.playerView setPlayer:nil];
} else if (self.storedPlayer) {
[self.playerView setPlayer:self.storedPlayer];
self.storedPlayer = nil;
}
AVPlayerItem *playerItem = self.playerView.playerItem;
NSArray *tracks = [playerItem tracks];
for (AVPlayerItemTrack *playerItemTrack in tracks)
{
//
if ([playerItemTrack.assetTrack hasMediaCharacteristic:AVMediaCharacteristicVisual])
playerItemTrack.enabled = restored; //
}
}
And following one:
- (void)setPlayer:(AVPlayer *)player
{
if (_player != player) {
[self removePlayerObservers];
_player = player;
[self configurePlayerLayer];
if (_player)
[self addPlayerObservers];
}
}
- (void)removePlayerObservers
{
if ([_player observationInfo] != nil) {
[_player removeObserver:self
forKeyPath:#"rate"
context:NPUIPlayerViewPlayerRateObservationContext];
[_player removeObserver:self
forKeyPath:#"isExternalPlaybackActive"
context:NPUIPlayerViewAirPlayVideoActiveObservationContext];
}
[self removePeriodicTimeObserver];
}
- (void)addPlayerObservers {
[_player addObserver:self
forKeyPath:#"rate"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:NPUIPlayerViewPlayerRateObservationContext];
[_player addObserver:self
forKeyPath:#"isExternalPlaybackActive"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:NPUIPlayerViewAirPlayVideoActiveObservationContext];
[self addPeriodicTimeObserver];
}
- (void)removePeriodicTimeObserver
{
if (_registeredTimeObserver) {
[_player removeTimeObserver:_registeredTimeObserver];
_registeredTimeObserver = nil;
}
}
- (void)addPeriodicTimeObserver
{
__weak NPUIPlayerView *weakSelf = self;
_registeredTimeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.5f, 10) queue:dispatch_get_main_queue()
So after moving the app to the background , pressing HOme button, and activating Control Center(sliding up from the button, where is a calculator, camera, light buttons) , I can set the title of item being played, but apple's player controls doesn't work : pressing pause, play doesn't work also slider doesn't work.
I do receive events when I press the buttons, but at the same time, inspite of the fact, that I am calling [player pause], or change rate it doesn't change the button. But when there is an issue with accessing audio via internet, or sound buffer is empty, the button is changed, it has paused UI.
Below goes the code for setting now playing media center and adding commands
- (void)adjustNowPlayingScreen
{
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *mDic =[NSMutableDictionary dictionary];
NSString *newTitle = self.playerTitle.text ?: self.itemToPlay.shortName ?: self.itemToPlay.name ?: #"";
if (newTitle.length)
mDic[MPMediaItemPropertyTitle] = newTitle;
AVPlayerItem * item = [self.playerView.player currentItem];
CMTime itemDuration = [self.playerView.player.currentItem duration];
if (CMTIME_IS_VALID(itemDuration) ) {
NSTimeInterval duration = CMTimeGetSeconds(itemDuration);
if (duration)
mDic[MPMediaItemPropertyPlaybackDuration] = #(duration);
}
else
{
NSTimeInterval duration = CMTimeGetSeconds([item.asset duration]);
if (!isnan(duration) && duration > 0)
mDic[MPMediaItemPropertyPlaybackDuration] = #(duration);
else {
duration = CMTimeGetSeconds([[[[self playerView] playerItem] asset] duration]);
if (!isnan(duration) && duration > 0)
mDic[MPMediaItemPropertyPlaybackDuration] = #(duration);
}
}
NSString *urlStr = self.itemToPlay.autoQualityURL ?: self.itemToPlay.lowAutoQualityURL;
if (urlStr.length)
mDic[MPMediaItemPropertyAssetURL] = urlStr;
CMTime curTime = self.playerView.playerItem.currentTime;
if (CMTIME_IS_VALID(curTime)) {
NSTimeInterval duration = CMTimeGetSeconds(curTime);
mDic[MPNowPlayingInfoPropertyElapsedPlaybackTime]= #(duration);
}
if (mDic.count) {
#warning #"HACK: Necessary to change number of played items & index"
mDic[ MPMediaItemPropertyAlbumTrackNumber] = #(1);
mDic[ MPMediaItemPropertyAlbumTrackCount] = #(1);
mDic[ MPNowPlayingInfoPropertyPlaybackRate] = #(self.playerView.isPlaying ? 1.0 : 0.0);
UIImage *img = [UIImage imageNamed:#"team_4f6ae0b99d4b856118000124"];
mDic[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc]initWithImage:img];
infoCenter.nowPlayingInfo = mDic;
}
}
- (void)addRemoteCommands
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
MPRemoteCommand *command = [commandCenter pauseCommand];
command.enabled = true;
[command addTarget:self action:#selector(pauseCommand:)];
command = [commandCenter playCommand];
command.enabled = true;
[command addTarget:self action:#selector(playCommand:)];
command = [commandCenter togglePlayPauseCommand];
command.enabled = true;
[command addTarget:self action:#selector(toggleCommand:)];
command = [commandCenter seekForwardCommand];
command.enabled = true;
[command addTarget:self action:#selector(seekForwardCommand:)];
command = [commandCenter seekBackwardCommand];
command.enabled = true;
[command addTarget:self action:#selector(seekBackwardCommand:)];
}
- (void)updateElapsedTime
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
MPRemoteCommand *command = [commandCenter togglePlayPauseCommand];
if (!command.enabled)
return;
[self adjustNowPlayingScreen];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
[self updateElapsedTime];
});
}
- (void)removeRemoteCommands
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
MPRemoteCommand *command = [commandCenter pauseCommand];
command.enabled = false;
[command removeTarget:self action:#selector(pauseCommand:)];
command = [commandCenter playCommand];
command.enabled = false;
[command removeTarget:self action:#selector(playCommand:)];
command = [commandCenter togglePlayPauseCommand];
command.enabled = false;
[command removeTarget:self action:#selector(toggleCommand:)];
command = [commandCenter seekForwardCommand];
command.enabled = false;
[command removeTarget:self action:#selector(seekForwardCommand:)];
command = [commandCenter seekBackwardCommand];
command.enabled = false;
[command removeTarget:self action:#selector(seekBackwardCommand:)];
}
- (void)seekBackwardCommand:(MPRemoteCommandEvent *)event
{
NSLog(#"%#",NSStringFromClass([event class]));
}
- (void)seekForwardCommand:(MPRemoteCommandEvent *)event
{
NSLog(#"%#",NSStringFromClass([event class]));
}
- (void)pauseCommand:(MPRemoteCommandEvent *)event
{
[self pause]; //_player pause];
AVPlayerItem *playerItem = self.playerView.playerItem;
NSArray *tracks = [playerItem tracks];
for (AVPlayerItemTrack *playerItemTrack in tracks)
{
/
if ([playerItemTrack.assetTrack hasMediaCharacteristic:AVMediaCharacteristicAudible])
playerItemTrack.enabled = false; /
}
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *mDic =[NSMutableDictionary dictionaryWithDictionary:infoCenter.nowPlayingInfo];
mDic[ MPNowPlayingInfoPropertyPlaybackRate] = #(0.0);
infoCenter.nowPlayingInfo = mDic;
}
So , my pause command is called, I call AVPlayer's pause method. But it doesn't stop audio stream.
And Slider doesn't work, therefore my seekBackwardCommand/seekForwardCommand are not called.
But as I said when the stream is over (audio also) the player on Control Center changes button from playing into pause, i.e. somehow it listen to Audio Session changes.
I adjust the sound category by setting AVAudioSessionCategoryPlayback, and setting mode : AVAudioSessionModeMoviePlayback
Please help how properly to handle pause/ play buttons on Control Screen, how to enable slider?
I am running my code on iOS9, iPhone 6.

AVAssetExportSession audio missing in iOS9

I have a code to export a video file. The code works fine on iOS8, but on devices with iOS9 there is no sound on the video file.
I think the problem is on the AVAssetExportSession, but I'm unable to finde the issue and fix it.
What is also weird, is the asset works fine when I export it for playback.
(void)export {
self.videoComposition.renderScale = 1.0;
self.videoComposition.animationTool = [self coreAnimationTool];
FakeAsset * assetFaker = [FakeAsset new];
[assetFaker fakeAssetWithDuration:self.currentTime completitionBlock:^(AVAsset *asset) {
[self addEmptyBackgroundTrackFromAsset:asset];
AVAssetExportSession * exportSession =
[AVAssetExportSession exportSessionWithAsset:[self.composition copy]
presetName:AVAssetExportPresetHighestQuality];
[MovieExport setCurrentExporter:exportSession];
NSUInteger i = 0;
NSString * path;
do {
path =
[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"movieExport%i.mov",i]]; i++;
} while ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:NULL]);
NSURL * url = [NSURL fileURLWithPath:path];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
}
exportSession.outputURL = url;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.audioMix = self.audioMix;
exportSession.videoComposition = self.videoComposition;
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, self.currentTime);
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusCompleted:
self.callback(#{MovieCreatorVideoURL : exportSession.outputURL}, nil);
break;
case AVAssetExportSessionStatusFailed:
self.callback(nil,exportSession.error);
break;
case AVAssetExportSessionStatusCancelled:
self.callback(nil, exportSession.error);
break;
default:
break;
}
}];
}];
}
- (void)exportForPlayback {
self.videoComposition.renderScale = 2.0;
FakeAsset * assetFaker = [FakeAsset new];
[assetFaker fakeAssetWithDuration:self.currentTime completitionBlock:^(AVAsset *asset){
[self addEmptyBackgroundTrackFromAsset:asset];
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:self.composition];
playerItem.videoComposition = self.videoComposition;
playerItem.audioMix = self.audioMix;
MovieMetadata * metaData = [MovieMetadata new];
metaData.orderedElements = self.orderedElements;
metaData.orderedMarkers = self.movieMarkers;
metaData.elementBeginTimes = self.elementBeginTimes;
metaData.filmSize = self.filmSize;
metaData.filmFormat = self.filmFormat;
metaData.filmQuality = self.filmQuality;
self.userInfos =
#{MovieCreatorAnimations : self.animationLayer,
MovieCreatorAVPlayerItem : playerItem,
MovieCreatorMetadata : metaData};
self.callback(self.userInfos, nil);
}];
}
I found the issue.
My AVMutableComposition have multiple AVMutableCompositionTrack. If one AVMutableCompositionTrack don't have a track, the video exported will be mute on iOS9 but will work correctly on iOS8
To fix this issue, when I initialize the AVMutableCompositionTrack, I add audio with no sound.
-(void)initEmptyAudio:(AVMutableCompositionTrack *) obj{
NSString* path = [[NSBundle mainBundle] pathForResource:#"emptyAudio" ofType:#"mp3"];
NSURL* audio_inputFileUrl = [NSURL fileURLWithPath:path];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
[obj insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:CMTimeMake(0,1) error:nil];
}

How do you programmatically position the mkmapregion to fit annotation

I'm trying to figure out how to reposition the MKMap region programmatically so that my annotation (automatically selected when the map loads) will all fit centered.
Current Result: https://www.evernote.com/shard/s46/sh/7c7d2ed8-203c-4878-af8c-83ff77ad7b21/ce7786acdf66b0782fc689b72d1b67e7
Desired Result: https://www.evernote.com/shard/s46/sh/21fb0eab-d5c4-4e6d-b05b-322e7dcce8ab/ab816f2a24f11b9c9e15bf55ac648f72
I have tried to reposition everything in - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views but that didn't work. Is there a better approach?
// here is viewWillAppear logic
[self.mapView removeAnnotations:self.mapView.annotations];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLGeocodeCompletionHandler completionHandler = ^(NSArray *placemarks, NSError *error) {
if (error) {
EPBLog(#"error finding placemarks: %#", [error localizedDescription]);
} else {
if (placemarks) {
[placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CLPlacemark *placemark = (CLPlacemark *)obj;
if ([placemark.country isEqualToString:#"United States"]) {
EPBAnnotation *annotation = [EPBAnnotation annotationWithCoordinate:placemark.location.coordinate];
annotation.title = self.locationObj.locationName;
annotation.subtitle = self.locationObj.locationAddress;
[self.mapView addAnnotation:annotation];
self.mapView.selectedAnnotations = #[annotation];
[self.mapView setCenterCoordinate:placemark.location.coordinate];
/**
* #todo
* MOVE THIS OUTTA HERE
*/
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = placemark.location.coordinate;
region.span.longitudeDelta = 0.003f;
region.span.latitudeDelta = 0.003f;
[self.mapView setRegion:region animated:YES];
[self.mapView regionThatFits:region];
*stop = YES;
}
}];
}
}
};
[geocoder geocodeAddressString:self.locationObj.locationAddress completionHandler:completionHandler];
Following method will fit the map on region to show all annotations. You can call this method in Map's didAddAnnotations method.
- (void)zoomToFitMapAnnotations {
if ([mMapView.annotations count] == 0) return;
int i = 0;
MKMapPoint points[[mMapView.annotations count]];
//build array of annotation points
for (id<MKAnnotation> annotation in [mMapView annotations]){
points[i++] = MKMapPointForCoordinate(annotation.coordinate);
}
MKPolygon *poly = [MKPolygon polygonWithPoints:points count:i];
[mMapView setRegion:MKCoordinateRegionForMapRect([poly boundingMapRect]) animated:YES];
}
Howevcer you should see if you want to add user location annotation in visible area also. If you don't then in loop check if current annotation is MkUserLocation and don't add it's points in points array.
if ([annotation isKindOfClass:[MKUserLocation class]]) {
continue:
}
Now if you wanted an Annotation to be in center and selected automatically then do this
annotation.coordinate=mMapView.centerCoordinate;
[mMapView selectAnnotation:annotation animated:YES];

Storing UIActionSheet data into array in CoreData

I've got an issue that's been bothering me. I have to store information from a UI Action Sheet in iOS into an array provided in CoreData. Trouble is, the Action Sheet is in a different function than the one used to store the data.
First: Here's the relevant code for storing the data:
(... checking for all fields; working properly)
}
else
{
newContact = [NSEntityDescription insertNewObjectForEntityForName:#"Contacts" inManagedObjectContext:context];
[newContact setValue:Salutation.text forKey:#"Salutation"];
[newContact setValue:FirstName.text forKey:#"FirstName"];
[newContact setValue:LastName.text forKey:#"LastName"];
[newContact setValue:CompanyName.text forKey:#"CompanyName"];
[newContact setValue:EmailAddress.text forKey:#"EmailAddress"];
[newContact setValue:PhoneNumber.text forKey:#"PhoneNumber"];
newContact.Disinfector =[NSNumber numberWithBool:yesWasher];
newContact.Sterilizer =[NSNumber numberWithBool:yesSterilizer];
newContact.CoffeeMaker =[NSNumber numberWithBool:yesCoffeeMaker];
Salutation.text = #"";
FirstName.text = #"";
LastName.text = #"";
CompanyName.text = #"";
EmailAddress.text = #"";
PhoneNumber.text = #"";
yesWasher = YES;
[WasherTog setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
yesSterilizer = YES;
[SterilizerTog setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
yesCoffeeMaker = YES;
[CoffeeMakerTog setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
Second, here's the code for the Action Sheet and handling the input:
- (void)showSalutation:(id)sender
{
UIActionSheet *popUp = [[UIActionSheet alloc] initWithTitle:#"Choose Salutation" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Cancel" otherButtonTitles:#"Mr.", #"Mrs.", #"Ms.", #"Dr.", nil];
popUp.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[popUp showInView:self.view];
[popUp release];
}
- (void)showSalutation:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
Salutation.text = #"Mr.";
}
else if (buttonIndex == 1)
{
Salutation.text = #"Mrs.";
}
else if (buttonIndex == 2)
{
Salutation.text = #"Ms.";
}
else if (buttonIndex == 3)
{
Salutation.text = #"Dr.";
}
}
I feel like I'm making a lot of newbie mistakes, so please forgive me. I've been learning how to code all weekend and you guys have been my best friend for this stuff. I just haven't seen this particular issue on the net.
Thanks in advance for your help!
Chris
If you are jsut trying to select a string based on the button index use an instance variable. For example you could declare NSString* yourString in the header and use it as follows:
if (button.index == 0) {
yourString = #"Mr."
}
Just following that pattern should work and you can do whatever you want with yourString from there.

Is it possible to open addContactScreen from native app?

I want to replicate add contact like screen iPhone has. But I don't want to add contact in default phonebook instead I want to use the details in my app. Is it possible to open default add new contact screen and get all the data? If yes then how? A simple code snippet will be very helpful. Here is an image of add contact screen to better understand my question
You can try to add the contact to the address book, pull the data and then delete it from the address book. this is a fairly simple process.
I use this function to save all the person data to core data in my app. and then to delete the person from the addressBook.
+(void)savePersonDetails:(Person*)person{
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef ref = ABAddressBookGetPersonWithRecordID(addressBook,[person.ID intValue]);
ABMutableMultiValueRef multiPhones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
NSString *phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(multiPhones, i);
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiPhones, i);
NSString *phoneNumberLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
CFRelease(locLabel);
Phone *phone =(Phone*)[NSEntityDescription insertNewObjectForEntityForName:#"Phone" inManagedObjectContext:person.managedObjectContext];
phone.number = phoneNumber;
phone.label = phoneNumberLabel;
phone.person = person;
[person addPhonesObject:phone];
[person release];
CFRelease(phoneNumber);
CFRelease(phoneNumberLabel);
}
CFRelease(multiPhones);
ABMutableMultiValueRef multiEmail = ABRecordCopyValue(ref, kABPersonEmailProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(multiEmail); i++) {
NSString *mail = (NSString*)ABMultiValueCopyValueAtIndex(multiEmail, i);
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiEmail, i);
NSString *mailLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
Mail *mailEntity =(Mail*)[NSEntityDescription insertNewObjectForEntityForName:#"Mail" inManagedObjectContext:person.managedObjectContext];
mailEntity.mail = mail;
mailEntity.label = mailLabel;
mailEntity.person = person;
[person addMailsObject:mailEntity];
CFRelease(locLabel);
[mail release];
[mailLabel release];
}
CFRelease(multiEmail);
ABMultiValueRef streets = ABRecordCopyValue(ref, kABPersonAddressProperty);
for (CFIndex j = 0; j<ABMultiValueGetCount(streets);j++){
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(streets, j);
CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(streets, j);
CFStringRef lbl = ABAddressBookCopyLocalizedLabel(typeTmp);
NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];
Address *addressEntity =(Address*)[NSEntityDescription insertNewObjectForEntityForName:#"Address" inManagedObjectContext:person.managedObjectContext];
addressEntity.label = (NSString*)lbl;
addressEntity.street = street;
addressEntity.city = city;
addressEntity.state = state;
addressEntity.zip = zip;
addressEntity.country = country;
[street release];
[city release];
[state release];
[zip release];
[country release];
CFRelease(dict);
CFRelease(lbl);
CFRelease(typeTmp);
addressEntity.person = person;
[person addAddressesObject:addressEntity];
}
CFRelease(streets);
ABMutableMultiValueRef multiURL = ABRecordCopyValue(ref, kABPersonURLProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(multiURL); i++) {
NSString *url = (NSString*)ABMultiValueCopyValueAtIndex(multiURL, i);
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiPhones, i);
NSString *urlLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
Url *urlEntity =(Url*)[NSEntityDescription insertNewObjectForEntityForName:#"Url" inManagedObjectContext:person.managedObjectContext];
urlEntity.url = url;
urlEntity.label = urlLabel;
urlEntity.person = person;
[person addUrlsObject:urlEntity];
CFRelease(locLabel);
[urlLabel release];
[url release];
}
CFRelease(multiURL);
ABAddressBookRemoveRecord(addressBook, ref, nil);
ABAddressBookSave(addressBook, nil);
CFRelease(addressBook);
if (![person.managedObjectContext save:&error]) {
// Update to handle the error appropriately.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
exit(-1); // Fail
}
}

Resources