ALAssetsGroupLibrary contains no video items - objective-c

I'm using the ALAssetLibrary to enumerate the videos on the device, and it works just fine for videos created on the device itself. But for groups that were created as a result of a synced album/event from iTunes I get no videos, although there are videos in the correspondence directory of the "Videos" app.
Code that search for videos and log the number of assets and videos in every group:
//using dispatch_async just to make sure it is called on the main thread
dispatch_async(dispatch_get_main_queue(),^{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupEvent|ALAssetsGroupLibrary usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group!=nil) {
NSLog(#"Number of assets in %# :%d",[group valueForProperty:ALAssetsGroupPropertyName],[group numberOfAssets]);
[group setAssetsFilter:[ALAssetsFilter allVideos]];
NSLog(#"Number of videos in %# :%d",[group valueForProperty:ALAssetsGroupPropertyName],[group numberOfAssets]);
}
} failureBlock: ^(NSError *error) {
NSLog(#"ERROR: %#",[error localizedDescription]);
}];
});
Any idea what's wrong? or how to get those videos that were synced from iTunes?
(iOS5 with iPhone4)

The AssetsLibrary only enumerates the Photo-Library. Therefore you only get photos/videos that are managed by the "Photos"-App (e.g. videos recorded with the device's camera). The videos managed by the "Videos" app are in a different Library. There is no way to access these by an official developer API.
Cheers,
Hendrik

Related

objective c fetch metadata for Images and video in iCloud without downloading the media File

I have the following code that provides me access to metadata of a Video File that maybe saved locally or in iCloud. I am not sure if this is the best way to get access to only the metadata without downloading the Video file.
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.networkAccessAllowed = YES;
options.deliveryMode = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:currentAsset
options:options
resultHandler:^(AVAsset * _Nullable avAsset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
// gather the metadata that interests you.
}];
This code works, but the problem is my test device has all the Photo Library backed up to iCloud and this moves along very slowly for Videos. So I suspect that this code is downloading the Video from iCloud and then I am getting access to the Metadata.
Any advice on how to speed it along ? Thanks in advance.

YouTube PlayListitemInsert code not working API

I have code that executes a youtube upload that's working fine, when it's done, it then attempts to move the video to a specific playlist.
For testing, I have hardcoded the playlist (it's correct and re-verified) and the videoid (it does exist in my channel) into the code.
I cannot get the video to to the playlist I specify and get the output below from the NSlog query1, but no errors.
Query1: GTLQueryYouTube 0x1700b0860: {method:youtube.playlistItems.insert params:(part) bodyObject:GTLYouTubePlaylistItem}
Any ideas what I am missing?
// Enter PlaylistItem Code
GTLYouTubePlaylistItem *playlistitem = [[GTLYouTubePlaylistItem alloc] init];
GTLYouTubePlaylistItemSnippet *playlistitemSnippet = [[GTLYouTubePlaylistItemSnippet alloc] init];
playlistitemSnippet.playlistId = #"PL4YcQc6s41BjKOoAPAQ_B-KNC2JBB3gl2";
playlistitemSnippet.resourceId.kind = #"youtube#video";
playlistitemSnippet.resourceId.videoId = #"4frmxoGMOcQ";
GTLQueryYouTube *query1 = [GTLQueryYouTube queryForPlaylistItemsInsertWithObject:playlistitem part:#"snippet"];
UIAlertView *waitIndicator1 = [Utils showWaitIndicator:#"Moving Video to Playlist"];
NSLog(#"Query1: %#", query1);
[service executeQuery:query1
completionHandler:^(GTLServiceTicket *ticket,
GTLYouTubePlaylistItem *resource, NSError *error) {
[waitIndicator1 dismissWithClickedButtonIndex:0 animated:YES];
Ended up scrapping this and used the playlist auto add feature in youtube. Way easier.

Apple Music API - Create a Playlist

I have been exploring the Apple Music API to see what kind of functionality I can expect to be able to use in an iOS app. I have created a little test app that gains permission from the user and outputs the playlists I have (and songs) to NSLog.
MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
[myPlaylistsQuery setGroupingType:MPMediaGroupingPlaylist];
NSArray *playlists = [myPlaylistsQuery collections];
for (MPMediaPlaylist *playlist in playlists) {
NSLog (#"%#", [playlist valueForProperty: MPMediaPlaylistPropertyName]);
NSArray *songs = [playlist items];
for (MPMediaItem *song in songs) {
NSString *songTitle =
[song valueForProperty: MPMediaItemPropertyTitle];
NSLog (#"\t\t%#", songTitle);
}
}
From this, I have been able to deduce the following (but I'm not 100% certain):
the playlist (basic info: name, id) is stored locally on the device
the playlist songs are also pulled from local storage but if the playlist hasn't been downloaded to the device it goes off to Apple to grab the song list.
So far, so good. What I want to know is:
is there a way of creating a playlist from my app (via the API)?
I know there is an MPMediaPlaylist addItem and add method but can't seem to find a way of creating the new playlist itself.
According to this page it should be possible: https://affiliate.itunes.apple.com/resources/blog/apple-music-api-faq/
Can a developer create brand new playlists on the user’s device with the Apple Music API?
Yes. The API allows develops to new create playlists on the user’s device.
I've figured this out. If you use the following code you can generate a new playlist and perform an action on it.
NSUUID *uuid = [NSUUID UUID]; //uuid for the playlist
[[MPMediaLibrary defaultMediaLibrary] getPlaylistWithUUID:uuid creationMetadata:[[MPMediaPlaylistCreationMetadata alloc] initWithName:#"YOUR PLAYLIST NAME"] completionHandler:^(MPMediaPlaylist * _Nullable playlist, NSError * _Nullable error) {
NSLog(#"%#", error);
if (!error) {
NSLog(#"All ok let's do some stuff with the playlist!");
}
}];
Apple's documentation on the whole API is severely lacking in terms of sample code and practical examples!

can't load Safari contentBlocker. because can't access app group's NSUserDefault

I am making iOS 9 Safari AdBlocker app.
I am using the module of AdBlockPlusSafari.
App works good on simulator.
But when try to run it on device(iPhone6), it fails to reload contentBlocker.
[SFContentBlockerManager
reloadContentBlockerWithIdentifier:self.contentBlockerIdentifier
completionHandler:^(NSError *error) {
if (error) {
NSLog(#"Error in reloadContentBlocker: %#", error);
}
dispatch_async(dispatch_get_main_queue(), ^{
wSelf.reloading = NO;
[wSelf checkActivatedFlag];
if (completion) {
completion(error);
}
});
}];
it gives error
Error Domain=ContentBlockerErrorDomain Code=3 "(null)"
It caused by accessing the values in NSUserDefault (App Group).
- (instancetype)init
{
if (self = [super init])
{
_bundleName = [[[[[NSBundle mainBundle] bundleIdentifier] componentsSeparatedByString:#"."] subarrayWithRange:NSMakeRange(0, 2)] componentsJoinedByString:#"."];
NSString *group = [NSString stringWithFormat:#"group.%#.%#", _bundleName, #"AdBlockerPro"];
NSLog(#"Group name: %#", group);
_adblockProDetails = [[NSUserDefaults alloc] initWithSuiteName:group];
[_adblockProDetails registerDefaults:
#{ AdblockProActivated: #NO,
AdblockProEnabled: #YES
}];
_enabled = [_adblockProDetails boolForKey:AdblockProEnabled];
_activated = [_adblockProDetails boolForKey:AdblockProActivated];
}
return self;
}
The App Group name in host app and safari extension is same.
But in Safari extension, when app accesses the setting in NSUserDefault, it gives me the error.
In Project setting/Capabilities, I did all for App Group. In app id, it involves app group name exactly.
This happens on only device. On simulator, it works good. I can't find the reason of this error.
Please help me if you are experienced this.
Looking forward to your help.
I found the reason myself.
I have put something (NSMutableDictionary) in app group container and did something to write file to extension bundle.
It is prohibited by Apple.
So I deleted all from AdBlockManager (interacts with app group) except flag variables (Boolean type).
And I proceeded the file management using NSFileManager.
http://www.atomicbird.com/blog/sharing-with-app-extensions
Finally, app extension is working for me on device.
Good luck!

AssetsLibrary does not get images saved in the Camera roll when I run the program on the device

I wrote a simple iOS program to get number of photo images which are saved in the camera roll by using 'Assets Library' framework provided in the SDK4.2.
The program worked well as I expected when I ran it on the iPhone simulator.
But, it didn't retrieve any images when I ran on the 'real' iPhone device (iPhone 3GS with iOS 4.2.1).
This problem looks like as same as the problem discussed in the below article:
Assets Library Framework not working correctly on 4.0 and 4.2
So, I added the "dispatch_async(dispatch_get_main_queue()..." function as below, But I couldn't solve the problem.
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray assets = [[NSMutableArray array] retain]; // Prepare array to have retrieved images by Assets Library.
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset != NULL) {
[assets addObject:asset];
dispatch_async(dispatch_get_main_queue(), ^{
// show number of retrieved images saved in the Camera role.
// The [assets count] returns always 0 when I run this program on iPhone device although it worked OK on the simulator.
NSLog(#"%i", [assets count]);
});
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
// Create instance of the Assets Library.
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos // Retrieve the images saved in the Camera role.
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failed.");
}];
}
Could you please tell me if you have any ideas to solve it?
I have 1 update:
To get error code, I modified the failureBlock of the enumerateGroupsWithTypes as below, and then reproduced the symptom again.
Then, the app returned the error code -3311 (ALAssetsLibraryAccessUserDeniedError).
However I didn't any operation to deny while my reproducing test.
What's the possible cause of the err#=-3311?
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failed");
resultMsg = [NSString stringWithFormat:#"Failed: code=%d", [error code]]; }];
It is strange that location services should be involved when accessing saved photos. Maybe it has to do with geo-tagging information on the photos. Anyways Apple says that enabling location services is required when using enumerateGroupsWithTypes:usingBlock:failureBlock:
Special Considerations
This method will fail with error ALAssetsLibraryAccessGloballyDeniedError if the user has not enabled Location Services (in Settings > General)."