Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

Sunday, 15 January 2017

Contact Picker Controller

One of the useful framework provided by Apple is "ContactsUI Framework". ContactUI Framework display user's contact list in a graphical interface.  The ContactUI Framework is useful for display, edit, select and create contact within the app. Before enter in code lets discuss about some important classes of this framework.

CNContactPickerViewController:
CNContactPickerViewController present the entire list of contact to user. This class allows to user to select one or multiple contacts. Its supports both single selection and multi-selection. The app does not need access to the user’s contacts and the user will not be prompted for access. It will only have access to the final selection of the user.

CNContactViewController:
The CNContactViewController is used to display a contact. It can display a new contact, unknown contact, or existing contact.

Now we are ready for display entire list of contacts  within the application.

Here code snippet for  display CNContactPickerViewController
//Create an instance of  CNContactPickerViewController
CNContactPickerViewController *contactController = [[CNContactPickerViewController alloc] init];
//Set the CNContactPickerDelegate
    contactController.delegate = self;
//Present picker controller
    [self presentViewController:contactController animated:YES completion:nil];

Implement the delegate method  for getting details of selected contant.
(In this example we are going to handle onle single selection of contact)

 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
            NSLog(@"Given Name = %@",contact.givenName);
           //Get numbers
           NSArray *phoneNumbers= contact.phoneNumbers;
    for(CNLabeledValue *phoneNumber in phoneNumbers){
        if ([phoneNumber isKindOfClass:[CNLabeledValue class]]) {
            NSLog(@"CNLabeledValue - %@", ((CNLabeledValue *)phoneNumber).value);
            id valueType = ((CNLabeledValue *)phoneNumber).value;
            if ([valueType isKindOfClass:[CNPhoneNumber class]]) {
                    NSLog(@"phone number - %@",[valueType valueForKey:@"digits"]);
                    NSLog(@"Country -> %@", [valueType valueForKey:@"countryCode"]);
            }
         }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}


// contactPickerDidCancel invoked, when user cancel picker selection
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


For more detail Click here

Monday, 19 December 2016

Get All Album and Assets

ALAssets library has deprecated in iOS9, and Apple introduce new framework  'Photos Framework' for retrieve assets for display and playback, edit image or video content, or work with collections of assets such as album, moments, and iCloud Shared Album.

Features:
1. Fetching objects and requesting changes
    Class's instance of Photos framework model retrieve assets(images, videos,  and Live Photos), collections of assets(albums or moments), and list of collections(such as album folder or moment clusters). There objects are read only, immutable and contain metadata only.
2. Change observing.
3. Support for Photos app features.
4. Assets and thumbnail loading and caching.
5. Assets content editing.

For details about Photos framework Click here

In this part of blog we will fetch all video assets from device(Photos app).
PHAssets:
All images, videos and live photos in Photos library represented by an instance of PHAssets.
 Instances of PHAsset are immutable.

PHFetchResult:
An ordered list of assets or collections returned from Photos fetch methods.
Photos provide all assets in the in a fetch result.

PHImageManager:
PHImageManager provide methos for retrieving and generating preview thumbnails and full-size image or video data associated with assets instance.

PHAssetCollection:
PHAssetCollection is collection of Photos and Videos assets , such as a moment, user-created album, smart album.

Get All Collection List:

1. Get list of smart albums
PHFetchResult *smartAlbum = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
    for (PHAssetCollection *collection in smartAlbum){
        NSLog(@"Title Album= %@",collection.localizedTitle);
        [tempArray addObject:collection];
    }


2. Get list of User created albums
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
    for (PHAssetCollection *collection in userAlbums){
        NSLog(@"Title Album= %@",collection.localizedTitle);
        [tempArray addObject:collection];
    }


Above code return all available list of collection of assets.

Now we can retrieve all assets from collection list.
here code for fetching all assets from collection list

 //initialize a mutable array for contain all assets
self.allImageAssetArray = [[NSMutableArray alloc] init];
 //Initialize an object of image manager
PHImageManager *manager = [PHImageManager defaultManager];
//fetch all image assets from collection list
    PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollectionIdentifier options:nil];
    NSLog(@"Total Count = %zd", assetsFetchResult.count);
    for (PHAsset *asset in assetsFetchResult){

    //Get image from Asset
        [manager requestImageForAsset:asset targetSize:thumbnailSize contentMode:PHImageContentModeAspectFit options:PHImageRequestOptionsResizeModeNone resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//add image to array
            [self.allImageAssetArray addObject:result];
        }];
    }
//end for loop