Storing NSData in app build-path with ASIHTTPRequest - iphone

I am using ASIHTTPRequest to download tiles to be used in my MKMapView. Testing the code it downloads whatever I download into a NSData variable. (My files average 50mb) Note that this is basically almost the same as this unanswered question.
I have found that I can directly download the file from my webserver into a file e.g.:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:#"/Users/ben/Desktop/my_file.txt"];
For mapview I need the files in a folder called Tiles which is inside my app's build. It ends up in this directory:
/var/mobile/Applications/887F4691-3B75-448F-9384-31EBF4E3B63E/MyApp.app/Tiles
Which I found out when I called:
NSLog(#"%#",[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Tiles"]);
So what I am trying to do now is download these files (take note the plural) and put them in in the Tiles folder. To start with, I can either ZIP these images together to extract later on my device and place in the Tiles folder, or I can just go ahead and download all the images separately (which seems easier since I already know all the file-names)
I tried this piece of code and it gives me an error. It is in my mapview's viewDidLoad just before I load the tiles. Take note it is only there for testing the download-code and will later be place somewhere else to be triggered by a button.
Here is the code:
NSURL *url = [NSURL URLWithString:#"http://...my-website.../25.png"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Tiles"]]; // THE PROBLEMATIC LINE
[request startSynchronous];
NSError *error = [request error];
if(!error)
{
NSLog(#"Loaded Map");
}
else
{
NSLog(#"Error occured while loading map");
NSLog(#"%#",error);
}
And this appears in the console:
2011-06-26 21:12:09.651 MyApp[5309:707] Error occured while loading map
2011-06-26 21:12:09.669 MyApp[5309:707] Error Domain=ASIHTTPRequestErrorDomain Code=8 "Failed to delete file at path '/var/mobile/Applications/887F4691-3B75-448F-9384-31EBF4E3B63E/MyApp.app/Tiles'" UserInfo=0x184b20 {NSUnderlyingError=0x1d4ac0 "The operation couldn’t be completed. (Cocoa error 513.)", NSLocalizedDescription=Failed to delete file at path '/var/mobile/Applications/887F4691-3B75-448F-9384-31EBF4E3B63E/MyApp.app/Tiles'}
So how can I store the downloaded file in a directory on the iPhone where I can find and use it later?
Thanks for the trouble.

You can't write directly into your application bundle. You should use a directory inside your app's sandbox, such as /Documents or /Library/Caches. See Getting paths to standard application directories for how to get these paths.

Related

how to access the content within the zip file in iphone

I have a zip file. In that zip file I have some ppt presentation.
I want to show that ppt presentation in my UIWebView.
I cannot extract and show ppt files there directly .
How do I access the ppt inside the zip in objective c?
This isnt easy. You might want to consider doing this another way. For example you could put the file unzipped on a server and open the link in a webview.
However, if you really want to do it this way there is 2 step.
1. Unzip the file.
You want end up with NSData. Read though some of the links suggested in the comments. You will need to use a 3rd party library to achieve this.
2. Load the data in to a UIWebView.
Write the NSData to the temp directory then point the UIWebView at it.
NSString *path = // .. Get a location in the NSTemporaryDirectory
if ([pptData writeToFile:path atomically:YES])
{
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}

UIWebView display locally stored website (HTML, images, Javascript)

I've looked EVERYWHERE for this, can't find anything. I basically need to store an entire website inside of my iPhone for some interesting reasons. Regardless I can't figure out how to display it correctly in my UIWebView.
EDIT: I should clarify, I can load the original HTML file, and I have chagned all of the pathing to be local, except nothing gets linked in.
Here is the code
self.dataDetectorTypes = UIDataDetectorTypeLink;
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self loadRequest:request];
index.html has a bunch of <script type="text/javascript" src="somescript.js">
None of the JS code gets executed
Looks like you're loading the HTML from inside your bundle. This means that all the additional files (.js, .css, and any media files) also need to be present in your bundle. So the first thing to check is to look inside the contents of your executable and make sure the js, etc. files are included.
If that looks fine the next thing to check is if the html, js, or css files reference content via relative or absolute URLs. If there's an absolute path reference in the web content then UIWebView is going to try to download that content each time so it'll only work when you have a net connection. If the path is relative then it's going to look in the bundle to see if such a file exists.
When you included the html and content into the XCode project file you probably dragged the file(s) over to the project side-bar and were asked whether to "Recursively create groups for any added folders" or to "Create Folder References for any added folders."
The default is the first one which means XCode creates a yellow folder in your project, but it'll ignore the directory hierarchy on disk when time comes to generate the output bundle. If you choose the second option then the folder is blue and if you look in your output bundle you'll see that the whole folder hierarchy has been replicated.
The first works for simple web pages where everything is at the same folder level and you can use the method you list above to load it. The second case works better if your web page is complex and references content in sub-folders in which case you need to load the web pages from a relative path (say, the 'webpages' folder):
NSString *path = [[NSBundle mainBundle]
pathForResource:#"index" ofType:#"html"
inDirectory:#"webpages"];
The last thing to check for is if there are any BASE tags in the html file. This is a way to specify a default address or target for all links on a page, but it can muck up webview links.
The problem is that this call:
NSURL *url = [NSURL fileURLWithPath:path];
doesn't setup a baseURL and so relative paths in the .html file for things like javascript, css, images etc don't work.
Instead use this:
url = [NSURL URLWithString: [path lastPathComponent]
relativeToURL: [NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory: YES]];
and then things like "styles.css" in the index.html file will be found IFF they are copied into the bundle next to the .html file.
You need to set this:
myWebView.dataDetectorTypes = UIDataDetectorTypeLink
Make sure that the .js files are in your copy to resource bundle section and not in the compile section. Xcode places them in the compile group by default.
When adding pathFor resource in Dictionary , it displays a nil string error.
My attempt was to run an entire web page out of the Xcode project file. To do that you must:
When importing the file select "Create folder references for any added folders".
Set up the web view, but make sure you set the relative path as previously mentioned.
NSString *path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"html"
inDirectory:#"Directory"];
NSURL *url = [NSURL URLWithString:[path lastPathComponent] relativeToURL:
[NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory:YES]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.currentWebView loadRequest:request];

working with loaddata is not working properly

I have file on local desktop.
I'm converting its url by using [NSURL fileURLWithPath:filePath] but I'm getting error. Here is my code:
NSString* filePath = #"/Users/Desktop/bb.ppt";
[powerWeb loadData:[NSData dataWithContentsOfFile:filePath]
MIMEType:#"application/vnd.ms-powerpoint"
textEncodingName:#"utf-8"
baseURL:[NSURL fileURLWithPath:filePath]];
It is giving me this error:
error:::Operation could not be completed. (NSURLErrorDomain error 100.)
error:::Frame load interrupted.
The following may work for you:
[[NSBundle mainBundle] URLForResource:#"mypresentation" withExtension:#"ppt];
I suspect that this is not right:
baseURL:[NSURL fileURLWithPath:filePath]
You are using the same full path in loadData and baseURL parts.
Do you have a file at the location: /Users/Desktop/bb.ppt, even if you had it there I suspect it will try to access /Users/Desktop/bb.ppt/Users/Desktop/bb.ppt looking at the baseURL setting.
I use this code to display ppt in iPhone application:
[myWebView loadRequest:[NSURLRequest
requestWithURL:[NSURL
fileURLWithPath:tempFilePath]]];
But it have a leak when viewing. I don't know why.

How to open xls file in iphone application?

I want to devlop an application in iphone where user can open a xls file ,Can do some editing on data already present and finally save it.
How can i do it any idea?
Yes, Hardik it is very simple if you want to open a local xls file.
Add a local xls file in to your project
Drag and drop an UIWebView control on the view.
Connect Files owner and UIWebView Object
Add this:
- (void)viewDidLoad {
[super viewDidLoad];
//Get the path where your local xls file is located
NSString *FilePath = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"xls"];
//Now To use a local resource file in a web view you need to use NSURL fileURLWithPath:
NSURL *url=[NSURL fileURLWithPath:FilePath];
//URL Requst Object
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[m_webView loadRequest:requestObj];
}
Now just Build and Go
You can use UIWebView to view an XLS file, but you won't be able to edit it. There's nothing built in to the iPhone SDK that will do this for you. You could try contacting these folks to see if they would license their software to you.

Using TwitPic API from ObjectiveC/iPhone

There's a similar question on here about this which I've read and I've tried to follow the advice given there... and I think I'm 95% complete but the remaining 5%... well you know ;-)
So I'm trying to call the twitPic API to upload an image and I've got the image contained in a UIImageView which I'm displaying on the screen (I can see it so it's definitely there). My code to form the API call looks like this:
NSURL *url = [NSURL URLWithString:#"http://twitpic.com/api/upload"];
NSString *username = #"myUsername";
NSString *password = #"myPassword";
NSData *twitpicImage = UIImagePNGRepresentation(imageView.image);
// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:twitpicImage forKey:#"media"];
[request setPostValue:username forKey:#"username"];
[request setPostValue:password forKey:#"password"];
// Initiate the WebService request
[request start];
I get an error back from it stating 'image not found'.
Is it obvious what I'm doing wrong? Any hints at all? I'm only a week deep in ObjectiveC so it's quite likely it's a real newbie error.
On the same track - it's not clear to me how I can capture a success or failure properly in code here - I'm currently dumping the 'request responseString' to an alert which isn't the best thing - how can I check the result properly?
I've also seen the use of 'NSLog' - which I suspect is a debugging/console logging tool - but I can't see the output from this anywhere in XCode - it doesn't SEEM to be shown in the debugger - any clues at all?!
Sorry if the above is really dumb - I can take a little ridicule - but I'm kind of isolated with my iPhone adventures - no one to bounce anything off etc - so I'm venting it all off here ;-)
Cheers,
Jamie.
You need to use the setData method to copy the image data into the post, like this:
[request setData:twitPicImage forKey:#"media"];
You're making a synchronous call, which is going to stall your app while you upload all that image data - you might want to switch to using an NSOperationQueue, or the ASINetworkQueue subclass that allows you to show a progress bar.
You should be able to see NSLog output in the debugger window of XCode. Make sure you've switched to this (control top left with a spray can on). You can also launch the console.