How to Encrypt mp3 Files in iOS - iphone

I have an app that downloads MP3 files from our web server and momentarily stores them in an NSData object. This object is then written to a .mp3 file stored to the /Library/Cache folder in the app's sandbox.
When it is time to play the file, I load it into an AVPlayerItem like so:
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp3", trackID]];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.trackPlayerItem = [[AVPlayerItem alloc] initWithURL:fileURL];
I think proceed to load this item into an AVPlayer and play it.
So my main question is: How do I encrypt these mp3 files while they're stored on the disk to ensure they can't just be plucked from the file system by anyone with a Jailbroken device?
I've already looked on Stack Overflow but couldn't find anything that helped.
Hope someone can help me out. Thanks

Check out this thread on adding RSA encryption/decryption to NSData.

Related

import *.pdf into my app iphone

how to import *.pdf all pdf from my iphone to my app like this add music file. http://developer.apple.com/library/ios/#samplecode/AddMusic/Introduction/Intro.html .Thanks in advance .New to iphone dev.Really appreciate any help.
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSFileManager *mgr = [[NSFileManager alloc] init];
NSArray *allFiles = [mgr contentsOfDirectoryAtPath:bundlePath error:NULL];
for (NSString *fileName in allFiles)
{
if ([[fileName pathExtension] isEqualToString:#"pdf"])
{
NSString *fullFilePath = [bundlePath stringByAppendingPathComponent:fileName];
// fullFilePath now contains the path to your pdf file
// DoSomethingWithFile(fullFilePath);
NSLog(#"file: %#",fullFilePath);
}
}
NSURL *url = [[NSBundle mainBundle] URLForResource:#"" withExtension: #"pdf"];
NSLog(#"File: %#",url);
You can just email the pdf file as attachment and the iphone can read it on its own. That feature already exists within the iOS of the iPhone or you could upload them somewhere (on your server) and send the links.
You can't just browse all the files on the phone from within your App.
As I said to you in the comment on your other questions (which, by the way is almost exactly the same as this one, but in the other question you ask about csv files rather than pdf), you need to read up about the App Sandbox.
In a nutshell, Apps can only see their own files and those that are written by the App. All apps have their own storage space and they can only see in that area.
Having said that, it is possible to pass some files around between apps, but they have to be written to support that. There is no such concept as a global file system on iOS devices.
READ THIS

AVAudioPlayer. Optional redirect of audio files. In _audio i am getting null

I have debug this code in url I am getting the "fileName" string, which i want. But in _audio i am getting null.
I am using an AVAudioPlayer to play and stream audio files. I am playing audio files from resources and document directory, but if they are absent in document directory then I want to play that specific audio from it's url. By streaming and buffering it. Here is my code:
NSString *fileName = #"http://ilm.coeus-solutions.de/frontend/images/audios/mp3test.mp3";
fileName = [fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:fileName];
AVAudioPlayer *_audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
You can try following code,
NSData *filelocation=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:#"http://ilm.coeus-solutions.de/frontend/images/audios/mp3test.mp3"]];
MyAudioPlayer = [[AVAudioPlayer alloc]initWithData:filelocation error:nil];
hope this will solve your problem
Slam dear! Please read again my question I have just asked about audio streaming rather than downloading it and then playing it.
This below link helped me. By the way thanks all who try to help me.
http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html
Thanks!

Renaming the images in ipad and then saving that image to app bundle?

I am creating an app in which i am displaying images from photo library and app bundle by clicking on two separate action buttons.
Now what i want is that i want to create a new action button and its purpose will be to select an image from photo library and then save that image into my app bundle.
Can anyone guide me to right direction regarding this topic.
Thanks,
Christy
I don't think you can modify the application bundle once it's on the iPhone. The entire thing is code signed, and changing it would cause it to not run any more. You can try saving the image to documents directory.
So far as I know you cannot repackage the bundles on the iPhone once your app has been released to the App Store. So go the other way, and put the data from the bundle on the filesystem so you can change it at runtime.
My usual technique for this stuff is:
bundle up the initial data
have a routine that checks for the
presence of a versioned file on the
iPhone's filesystem at startup
if that routine doesn't find the
current version of the file, copy
all the data into the iPhone's
filesystem
reference the data from the
filesystem in my app, rather than
using the bundle path
So, essentially your bundle is just a delivery mechanism, a way to preload the filesystem with the stuff you are going to need. Once it's on the filesystem you can change anything you wish.
References
Downloading image into bundle?
How to save a UIImage to the application's Bundle?
UPDATE
- (IBAction)saveImage:(UIImage *)image {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
NSData *imageData = UIImagePNGRepresentation(image);// Change according to your needs
[imageData writeToFile:savedImagePath atomically:NO];
}
You can make an NSData from the image you are picking from your photo library by using this -
NSData *imageData = UIImageJPEGRepresentation (UIImage *image,CGFloat compressionQuality);
then call
[imageData writeToFile:imgPath atomically:YES];
Here imgPath is the path for TMP dirextory where you want to write the file, get it as -
NSString *filename = #"a.png";
NSString *uniquePath = [TMP stringByAppendingPathComponent:filename];
and TMP is an enum
#define TMP NSTemporaryDirectory()

iPhone: How to download a full website?

what approach do you recommend me for downloading a website (one HTML site with all included images) to the iPhone?
The question is how to crawl all those tiny bits (Javascripts, images, CSS) and save them locally. It's not about the concrete implementation (I know how to use NSURLRequest and stuff. I'm looking for a crawl/spider approach).
Jail breaks won't work, since it is intended for an official (App Store) app.
Regards,
Stefan
Downloading? Or getting the HTML-source of the site and displaying it with a UIWebView?
If last, you could simply do this:
NSString *data = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://apple.com"] encoding:NSUTF8StringEncoding error:NULL];
// Load UIWebView with data
[webView loadHTMLString:data baseURL:[NSURL URLWithString:#"http://apple.com"]];
EDIT:
For this approach, you would probably be best off using a regex-library for iPhone to parse through the string and find needed objects.
You could use this: RegexKitLite, and do a couple of Regex-expressions to find, for example, <link rel="%" href="*"> and src="*". But you have to remember to store them and replacing the values of * with the new path.
Storing files:
You will get url's back from the regex-methods, and you can write the files from the url's like this:
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString pathToCurrentSite = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#/", fullUrlToPage]];
for (urlString in urlStrings) {
NSData *stringData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
[fileManager createFileAtPath:[pathToCurrentSite stringByAppendingPathComponent:urlString] contents:stringData attributes:nil];
}
NSString *data;
NSData *pageData = [data dataUsingEncoding:NSASCIIStringEncoding];
[fileManager createFileAtPath:[pathToCurrentSite stringByAppendingPathComponent:#"index"] contents:pageData attributes:nil];
[fileManager release];
Install wget on your jail broken iPhone
Use the spanning hosts options to download everything from the site.
wget -rH -Dserver.com http://www.server.com/
But why do you want to do this on a mobile device? This is somthing that should be done on a real computer with lots of memory, disk space, bandwidth and multiple CPU cores.
Was looking for similar functionality and found this. Can't claim any credit for it, just wanted to make sure it was mentioned (as a drop-in solution) for people interested in it.
http://robnapier.net/offline-uiwebview-nsurlprotocol
You can't save websites to your phone, only view them (unless your jailbroken.)
Hope this clears up your confusion,
Lee.
Here is the Appstore link https://itunes.apple.com/us/app/sitesucker/id346896838?mt=8
The app downloads entire websites natively to the phone.

Sound not working in device

I am writing an application and part of it is recording and playing the sound using AVAudioRecorder and AVAudioPlayer class. I set up the sound file as below.
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:#"/sound.caf"];
self.soundFileURL = [NSURL fileURLWithPath:resourcePath];
When i run the app in simulator, it works fine.However when i load the app into the device it does not work.Does anyone have any idea what the problem could be.
Hi coob. i tried with your answer .But no sound in device .It works in simulator. When i tested in distribution device i got a crash report "unknown kernal[0]:ERROR:AMC reset[non-fatal error]:could not lock BSU "
I don't think you can write to the resourcePath on the device, it's protected. Find the App's documents directory and write somewhere there:
NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recordingDirectory = [filePaths objectAtIndex: 0];
NSString *resourcePath = [recordingDirectory stringByAppendingString:#"/sound.caf"];
self.soundFileURL = [NSURL fileURLWithPath:resourcePath];
Is it possible that you have done the same thing that I did the first time I tried to get sound to work on an iPhone? Is the sound muted?
Another possibility:
I have discovered that filenames within the bundle are case-sensitive on the device but not in the simulator. (Or more case-sensitive -- I haven't experimented with exactly what works on each.) So if the case is wrong on a file, it may simply be sliding through on the simulator, but returning nil instead of the file when it runs on the device.
I know, it sounds odd, but I just confirmed it in my own project. Try taking a file that you know loads properly, change the case of the filename, and see how it works on the device. -- clarification -- change the case of the name by which you retrieve the file so it no longer matches the filename within the bundle.
Doesn't mean that's your problem, but something to watch out for nonetheless.
I just reviewed some of my code. Matching your intentions, I come up with:
NSString *soundName = #"sound"; // without extension -- mimicing your naming
NSString *resourcePath = [[NSBundle mainBundle] pathForResource: soundName ofType: #"caf"]]
self.soundFileURL = [NSURL fileURLWithPath:resourcePath];
Does that work?
Make sure your device is not in Silent Mode. Short sounds don't work in silent mode.
I had the same problem I add Audiosession then it is works fine.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
Hope this will work.