how to open links in json in safari? - ios

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?

Related

iOS Apple Map native links - URL Scheme examples

In one of my projects I'm capturing the Lat/Long coords in a core data store, and later on providing this information in an email with Attached PDF.. On the receiving end, I'd like the convenience of another iPhone user to be able to click on the link, and go to that location in Apple Maps
I find the native apple map comes up, but it's simply an open map view w/no pin:
NSString *appleLink2 = [NSString stringWithFormat:#"http://maps.apple.com/maps
ll=%#,%#",self.lat,self.lon];
I'm creating the clickable in a common fashion with a CGRect, and NSURL
It appears on the surface that I should probably get the street address instead of the Lat/Long coords also, and use the address for better map resolution... Thoughts?
Also, I'm looking for examples of Apples URL Schemes to query strings mentioned in the Docs, but can't find any real examples... ll = , son =, t = , z =. I understand saddr and daddr, and am surprised that I can't find any reference or real examples anywhere.
If you want the pin to be placed on the map in Apple's Maps app, use a 'q' instead of 'll' in the URL:
NSString *appleLink2 = [NSString stringWithFormat: #"http://maps.apple.com/maps?q=%#,%#", self.lat, self.lon];
The following apple link has all details for the questions
http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html
USE NSString for URL
NSString *appleLink2 = [NSString stringWithFormat:#"http://maps.apple.com/maps
ll=%#,%#",self.lat,self.lon];
NSURL *myUrl = [NSURL URLWithString:[appleLink2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Then use myUrl is as URL.

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.

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

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.

Resources