iPad: How to generate an HTML file programmatically and view it? - ipad

I want to generate some program output and display it in Safari.
Alternatively I could display it in a UIWebView if there were a simple method for providing a Print button and then have printing occur.
Currently my approach is to save my HTML to a file, generate a full path, and then try to invoke Safari like so:
NSString *str = [[NSString alloc] initWithUTF8String: htmlpath];
NSURL *url = [NSURL URLWithString: str];
[[UIApplication sharedApplication] openURL:url];
But this is not working. Can somebody tell me what I'm doing wrongly? Thanks.

Apps in iOS are sandboxed, so Safari cannot access the contents of your app.
If you simply want to print it, I suggest you use a UIWebView.

Related

Get html code and edit then load on to web view

I want to load a web site on a UIWebView which is not under my control and edit/add certain UI changes (Some texts, images, etc) to it. Can I do this within my iOS source code? I can't change the hosted html contents since them not under my control.
If this cannot doable within iOS source code, please advice me the correct way to achieve this.
Load the webpage into an NSString, make any modifications and then put the html into the UIWebView.
NSURL *url = [NSURL URLWithString:#"http://example.com/"];
NSString *page = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
/* Make changes to page here */
[self.webView loadHTMLString:page baseURL:nil];
I'd get the dom with JavaScript, manipulate, then inject back with JavaScript.
See stringByEvaluatingJavaScriptFromString:.
You can write your own full featured, minified JavaScript, then pass into using this method.
// Change body color of any HTML content inside a UIWebView.
NSString *javaScript = #"document.getElementByTagName('body').backgroundColor = '#888';";
[webView stringByEvaluatingJavaScriptFromString:javaScript];

how to open links in json in safari?

In my app I have a UITableView displaying tweets from a downloaded json file. Many of the tweets contain a link (to Instagram for example). How would I go about making these links open in Safari? What code would I need?
[[UIApplication sharedApplication] openURL:myurl];
Or is your problem elsewhere?
If that doesn't solve the problem, some code would be helpful to show what your table view cells look like.
First of all you will need to detect your links. Is the link part of the tweet string or is it a separate object in an array?
NSString *yourLink = #""; //we will need more detail in your question for this.
Once you have your link you need to store it as an NSURL like so:
NSURL *url = [[NSURL alloc] initWithString:yourLink];
And then you can open this in a browser using:
[[UIApplication sharedApplication] openURL:url];
It would be useful to see what you are displaying in your table. Is it an NSArray from your JSON, or an NSDictionary?

Webview is not showing anything

I am trying to load UIWebView on detailview when users tap in masterView Table. But somehow my UIWebView is not showing anything.
Here is my code:
NSString *manualURL = self.detailItem;
NSURL *url = [NSURL URLWithString:manualURL];
[self.manualWebView loadRequest:[NSURLRequest requestWithURL:url]];
I have also tried to check the value of url in debugger by printobject (po url) its showing correct url address but webview is still empty.
I just found out that if I put whole address like http://en.wikipedia.org/wiki/london then its working but if I put anything like www.google.com then its not. Any explanation ?
My code was just fine the problem was with the url. I was pulling out urls from plist and in Plist they were stored as "www.xyz.com" and when I replaced them with "http://www.xyz.com" it worked fine. Still don't know why it behaves in that way.

Opening phone application

This is most likely a very obvious answer, but I am just not seeing it. I want to open the phone application when I click a button.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://???????"]];
That line of code should do it. The problem is, depending on what happens in the app, I want it to dial a different number. I made a string variable, and I want to put it into the space with the ???
Something like:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://(myPhoneNumberString)"]];
Any ideas?
Thanks
Take a look at the NSString documentation.
Something like that will work:
[ NSURL URLWithString: [ NSString stringWithFormat: #"tel://%#", myPhoneNumberStringVar ]
String format is basically the same as in the printf C function, with the additional %# format, used to print object representations.

Add address annotation when finding route via google maps api

I'm using google maps to get directions from one place to another. Here is the code
NSString *mapAPIString = [NSString stringWithFormat:#"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",
sourceAddress.coordiate.latitude,
sourceAddress.coordiate.longitude,
destAddress.coordiate.latitude,
destAddress.coordiate.longitude,
];
NSURL *url = [[NSURL alloc] initWithString:mapAPIString];
[[UIApplication sharedApplication] openURL:url];
The code works perfect. But both addresses are marked as 'Unknown address' in iOS Map App. Is it possible to add custom title for each address ?
iOS 6 is still under NDA, but if you have access to the docs have a look at them and you will probably find useful information for this problem.

Resources