iPhone: How to download a full website? - iphone

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.

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

How to Encrypt mp3 Files in iOS

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.

Search and save images from google images in iphone sdk

In my app I need to search images from google images for a particular word.
for example, i want to search image for Earth.
How to search images from Google Images and save selected images in documents directory?
Is there any API that can I use? Or any other way to do it?
Google API
Google Image Search API / JSON Developer's Guide.
You will have to display the images yourself as the result is a json response. However this will give you maximum control. The result will contain a thumbnail URL which you can use to show the preview.
Downloading images
Downloading images can be done in many ways:
URL Loading System Programming Guide / Using NSURLConnection
ASIHTTPRequest
NSData class method dataWithContentsOfURL and usage of the UIImage method imageWithData:
The use of the Google API does only work under certain circumstances such as
"Applications that use this interface must abide by all existing Terms of Service. Most importantly, you must correctly identify yourself in your requests."
Saving Images
After receiving the results you can save the image to disk with:
NSString *aFileName = #"myImage.jpg";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:aFileName];
[myImageAsData writeToFile:path atomically:YES];
alternatively if you've got the image as UIImage already:
NSData *myImageAsData = [NSData dataWithData:UIImagePNGRepresentation(anImage)];
[myImageAsData writeToFile:path atomically:YES];
First, you want the Google Image Search API. http://code.google.com/apis/imagesearch/
Next, if you're cool with only targeting iOS 4.0 and later, you can use Grand Central Dispatch and the very useful sycnhronous data APIs included in iOS. Specifically, I'm thinking of [NSData dataWithContentsOfURL:]. This will block the thread until it downloads the whole image, so don't run it on the main thread. Fortunately, GCD lets you throw stuff on a background thread with ease. So your code will probably look something like this:
dispatch_queue_t NetworkQueue = dispatch_queue_create("NetworkQueue", 0);
dispatch_async(NetworkQueue,^{
NSData* imageResultsData = [NSData dataWithContentsOfURL:
[NSURL urlWithString:#"https://ajax.googleapis.com/ajax/services/search/images?some_images_query_here"];
//do something with that data
//let's say you unpacked it into an NSArray of NSURLs
for(NSURL* url in myImageArray) {
dispatch_async(NetworkQueue,^{
NSData* imageData = [NSData dataWithContentsOfURL:url];
[imageData writeToFile:file_name_here atomically:YES];
});
}
});
Google Image Search is not working:
"The Google Image Search API has been officially deprecated as of May
26, 2011. It will continue to work as per our deprecation policy, but
the number of requests you may make per day may be limited. We
encourage you to use the Custom Search API, which now supports image
search."
Here is the link to the Custom Search API

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()

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.