iCloud not work after update - objective-c

At the stage of testing applications iCloud works fine. But after the application checks for updates from apple and download from itunes, icloud is not working, then there is a line
NSURL * returnedURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier: nil];
returns nil.
Where did I go wrong?
Thanks in advance.

Are you sure your containers are listed in the com.apple.developer.ubiquity-container-identifiers entitlement?
Apple's Documentation on NSFileManager URLForUbiquityContainerIdentifier: mentions:
If you specify nil, this method returns the first container listed in the com.apple.developer.ubiquity-container-identifiers entitlement.

Related

Get list of files in Library folder with sandbox in cocoa

I'm trying to get list of files in Library folder in cocoa, i use this code and NSHomeDrirectory() function without sandboxing and work well.
TempArray = [[NSFileManager defaultManager] directoryContentsAtPath:FolderURL]
but when I checked sandbox is code not work, Is it any entitlements should i add? or what code can replace?
I haven't tried if this will get all the files but it should since you can save and read files from the library.
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *libraryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:libraryPath error:nil];
If you add below entitlement you can access ~/Library or /Library folders
com.apple.security.temporary-exception.files.home-relative-path.read-write: /Library/
But after this you will get reply from apple is
2.31:Apps that are not sandboxed appropriately may be rejected
I didn't get answer of this. If anyone knows please post here.

Is there any way to differentiate between iTunes Match and DRMed tracks on iOS 5?

For a given MPMediaItem representing a track in an iOS5 user's music library, how can we determine if a track is:
an iTunes Match track that has not been downloaded from iCloud
vs.
a DRMed track
?
In both cases the NSURL returned by MPMediaItemPropertyAssetURL is nil. Therefore instantiating an AVAsset to check the exportable flag is not a viable solution.
It's my understanding that it depends on the version of iOS you use. I think prior to maybe 4.3, an asset returning nil meant simply that the item was DRMed and you didn't have access to it. However, in current versions (5), nil means it's iCloud only. Maybe you have tracks that you think are just DRMed but are in fact iCloud stored songs. On the current app I'm working on, I originally didn't account for iCloud tracks at all (as I was targeting the app for prior versions of iOS) and so I was getting crashes depending on who's device I used. To solve the issue and test for iCloud/DRM I use:
AVURLAsset* asset;
NSURL* realAssetUrl = [item valueForProperty:MPMediaItemPropertyAssetURL];
if(!realAssetUrl){
//track is iCloud
}
asset = [[AVURLAsset alloc]initWithURL:realAssetUrl options:nil];
if(asset == nil || asset.hasProtectedContent){
//asset is DRMed such that it cannot be played back.
//most apps can stop here but I need to be able to export the song
}
if (!asset.exportable || !asset.readable){
//the asset cannot be exported and thus cannot be cached to a file
//the current app directory and cannot be transferred over network
//if asset passed earlier check, can still be used for local playback
}
[asset release];
That seems to work fine for me, but you also imply you were headed down the same path, already, so I'm not sure how much help that'll be to you. However, good luck with your project and I hope you find the answer you're looking for!

Load files from folder

I'm loading a folder with files using this method:
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:#"GBC/Caras/"];
fotosJugadores = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: #"/Users/exular/Developer/GBC/exular/GBC/Caras/" error: &error];
But the problem remains to get the same functionality to do it once the app will run in the device, how can I set a folder with all that images to be available in the device? Is there any bundle method similar to contentsOfDirectoryAtPath: to load images from project bundle, something like imageNamed: ?
thanks
Is there any bundle method...
When you start a question like that, the answer is usually found in the NSBundle class reference. In this case, the answer is yes, there is such a method. You will see that if you read the documentation. You should always check the documentation before asking for help.

Is it possible to copy a different plist into the application bundle code wise?

My question is stated above in the title but I'll write it here again
Is it possible to copy a different plist into the application bundle code wise??
This is actually a bit related on my previous question ;) Create a dictionary property list programmatically
But since I can write out the plist (and test if I'm doing something wrong ) I was wondering if I can copy my written file
[testBook writeTofile:#/Users/jack/testbook2.plist" atomically:NO];
to my application bundle in code (so that it can read from here with
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *dataPath = [path stringbyAppendingPathcomponent:#"testbook2.plist"];
Another alternative is good as well for instance if I can read my plist directly from another path instead of the mainBundle
Physically, yes it is possible sometimes.
NO, DON'T DO THAT!!
Okay... copying stuff into your App Bundle is bad, your app bundle is not where you keep settings or data. You should think of your app bundle as readonly. You should do this for several reasons:
While most of the time a user on a Mac OS X system has permission to write to an app sometimes they don't. Think about children who are not using admin accounts, or lab deployments at schools.
It will invalidate codesigning. If you sign your application this will change the signature and break the codesign, causing all sorts of weird issues (losing any firewall permissions the app has, telling the user the app has changed and asking them for keychain permission again, etc). In the future this may also pop up all sorts of warnings since tampering with an app is a big security red flag.
It won't work on iPhone. That may not be an issue, but if you every intend to use this code on an iPhone the apps bundles are sandboxed readonly
The correct place to put this sort of stuff is into a sub folder of the Application Support folder
I don't know exactly what you're doing, but depending on your application, you might want to use the Application Support directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) {
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"App Name"];
// Create the directory if it doesn't exist, etc.
} else {
NSLog(#"Fail!");
// ...
}

Best way to copy or move files with Objective-C?

I was wondering, is there a best practice to write an OSX programm that copies or moves a file from one place to another?
is there some NSSomething method I can call?
Do I have to work with Input/Output streams?
Or is the best way maybe to just rely on passing commands to the finder?
Bonus question: How do I get percentages a la "copy 12% complete" with one of these methods?
Thanks for your help!
NSFileManager and NSWorkspace both have methods to move, copy, and delete files. Usually you'd use NSFileManager since its easier to work with:
if ( [[NSFileManager defaultManager] isReadableFileAtPath:source] )
[[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:nil];
However, NSWorkspace can easily move files to the Trash, which NSFileManager can't do.
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:foldername destination:#"" files:filenamesArray tag:&tag];
Check the documentation for a more complete description of the two classes. (NSFileManager, NSWorkspace)
[[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:&error]
Here's the link to the class reference:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/copyItemAtPath:toPath:error:
You can also use FSCopyObjectAsync function. You can display file copy progress and you can also cancel file copy using FSCopyObjectAsync().
Take a look at this post.
FSCopyObjectAsync is Deprecated in OS X v10.8
copyfile(3) is alternative for FSCopyObjectAsync. Here is example of copyfile(3) with Progress Callback.