OpenURL to a file in NSBundle - iphone

I have a file, which i want to open in safari (or call it view in safari).
I add this file to my bundle resources.
and do the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"tryFile.txt" ofType:nil inDirectory:nil];
NSURL *url = [NSURL fileURLWithPath: path];
NSLog(#"path: %# \n url:%# \n can open url:%d",path,url,[[UIApplication sharedApplication] canOpenURL:url]);
BOOL didGo = [[UIApplication sharedApplication] openURL:url];
NSLog(#"didgo?: %d",didGo);
Of course, as the sandboxed environment says, I am not able to open the file ins safari. It says, canOpenURL as yes but then didGo is NO.
How can I do this without setting up a local server. Please suggest.
Should I keep the data stored in a variable and then open an html page with that data...or what?
EDIT:
These are the logs:
path: /var/mobile/Applications/C78C7CD8-22C7-468A-AA5D-AF22C6042378/TryApp.app/tryFile.txt
url:file://localhost/var/mobile/Applications/C78C7CD8-22C7-468A-AA5D-AF22C6042378/TryApp.app/tryFile.txt
can open url:1
didgo?: 0
PLEASE NOTE opening the file in a webview is not an option. The requirement wants it to be opened in safari. And anyways, this example is of a text file...the filethat may be opened can be anything, and webview does not support all file types.

The txt file resides within your application sandbox. The URL that you are trying to load in Safari will not get loaded due to security in iOS. Read more about it here
As a workaround you can use a UIWebView in your application and open the file in that.
Cheers!Amar.

You'll need to setup a server or open the page in a UIWebView inside the app.

NSString *path = [[NSBundle mainBundle] pathForResource:#"tryFile.txt" ofType:nil inDirectory:nil];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:path]];
This code is for open url in directly in safari OR you can use Webview and direct open url in your webview.thanks

Related

How to make link open "Maps" window when clicked

I would like to have a link in a web page in such a way that when the link is clicked it opens the standard "Maps" view in iPhone.
If such a thing is possible, what tag format do I need to use with the link?
It's pretty simple; you don't even need to use a specific scheme identifier. Any Google Maps URL will be opened with the Maps app automatically, as long as all the parameters are supported.
So links like these:
http://maps.google.com/maps?q=cupertino
http://maps.google.com/maps?daddr=San+Francisco,+CA&saddr=cupertino
Would automatically be opened in Maps. To find out more about what works and what doesn't see the Map Links page from the Apple URL Scheme Reference.
I used this code which works passing longitude lattitude:
NSString *url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",cur_lat, cur_lon,[loc stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"current %f %f",cur_lat,cur_lon);
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];

display local html file with css in iphone..?

I want to display local html file that has css too in UIWebView. I am able to do that using following code..
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"info" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path];
[webView loadHTMLString:htmlString baseURL:baseUrl];
I am not able to get hover effects and my understanding is my be there is no hover kind of thing in a touch mobile,
Any ways my main issue is I had deleted my index.html from resources and then re added the updated one but not getting the updated one..
I don't know what happen the web view still loads the old html file. I tried to clean project too.
Second thing can I get css effects in iphone..?
Thanks..
As far as I know you can only achieve this result by using javascript-events like ontouchstart (or something).
I believe if you reset your simulator (While running your simulator, go to iOS Simulator > Reset Content and Settings in the menu bar) then re-run your app, the UIWebView will load the new index.html. UIWebViews cache these things, so you can add code to clear that cache and/or ignore the cache. Check here: Clearing UIWebview cache

Loading a local HTML file in Navigation based application

I am new to iOS development.
I have been trying to find an answer to my question but I could not, I have found some related answers but I could not fix the problem.
My problem is, I have made an application which has a table, and for every row there should be a URL which loads a UIWebView, and should be loaded when you click on the row, but it loads an online URL such as #"http://www.google.com". But I want it to load a local HTML file.
I have used this for the URL:
azkarDetailViewController.detailURL=
[[NSURL alloc] initWithString:
[[[azkarData objectAtIndex:indexPath.section] objectAtIndex:
indexPath.row] objectForKey:#"url"]];
And for every row in the table I use:
[AZKAR addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"THE NAME OF THE ROW",#"name",#"DayandNight.png",#"picture",#"http://www.google.com",#"url",nil]];
So I want the application to load a local file from the Resources (say for example the name of the HTML file is "index.html").
Can you please help me solving this problem ?
Thank you very much..
When you compile your app, the output is a “bundle” of files and directories. You can actually access the bundle through the NSBundle class; the static method +mainBundle will return a pointer to an instance of NSBundle that represents your app's main bundle.
You can then use the -URLForResource:withExtension: and -URLForResource:withExtension:subdirectory methods of NSBundle (these require iOS 4.0 or higher—there are older equivalents as well). If the HTML file is stored in your main bundle directory (that will be the case unless you created a an actual directory—different from a group—in your bundle), you can find its URL this way:
NSString *myDocumentName = #"index.html";
NSURL *documentURL = [[NSBundle mainBundle] URLForResource:myDocumentName extension:Nil];
Note that you do not have to specify the extension separately if it's already in the filename.
Note: my explanation is a little oversimplified (I'm assuming you don't need to deal with localizations, otherwise there are other issues you should be aware of explained in the docs).
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 40000
// code for iOS below 4.0
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"WindowsTest" ofType:#"html"]];
#else
// code for iOS 4.0 ++
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"WindowsTest" withExtension:#"html"];
#endif

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];

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.