Calling a URL from a Label - ios

I've parsed a YouTube link from a remote XML file and placed the string into a Label that the user sees. Now I want to open it using that label's text ("link0" is the label name). The log shows the label properly populated with the URL....
NSString *stringURL = [NSString stringWithFormat: #"%#", link0.text];
NSLog(#"%#\n",stringURL);
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
But it's not working. If I manually insert the link into the above code, it works. How do I convert text from a label "link0" into a useable URL string? Thank you.

Related

Open apple maps on ios 8 and above not working

Im trying to create an application which will open apple maps located in iOS device with given source and destination address.
NSString* addr = [NSString stringWithFormat: #"http://maps.apple.com/?daddr=%#&saddr=%#",[_fromTextfield.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]],[_toTextfield.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
addr=[addr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:addr];
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
But canOpenURL is not working ! It always returns NO.
I have added
"LSApplicationQueriesSchemes
urlscheme
urlscheme2
urlscheme3
urlscheme4
"
in Info.plist file.
Try below coding.it works perfectly.
NSString* addr = [NSString stringWithFormat: #"http://maps.apple.com/?daddr=%#&saddr=%#",#"Lacock" ,#"Avebury"];
NSURL* url = [NSURL URLWithString:addr];
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
If error is
"This app is not allowed to query for scheme whatsapp"
1) Check Info plist.
add LSApplicationQueriesSchemes Array
add whatsapp String.
If error is invalid url.
2) Check, whether the string you are passing is not having special characters. Specially whitespace.
string = [string stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
then use this string for url.
Both of these should solve. Cheers!

Make a phone call from UIWebView without prompt

SCENARIO
I have an app that is a UIWebView, I make some url overriding for requirements.
PROBLEM
To make a call opening url with tel: works weird in iOS7 and iOS8, it makes the phone call direct in the background, but it also ask for the confirmation, so user experience is horrible:
[[UIApplication sharedApplication] openURL:request.URL];
SOLUTION
To solve this issue, I used telprompt. It works nice in all iOS versions:
NSURL *url = [NSURL URLWithString:#"telprompt://637****"];
return [[UIApplication sharedApplication] openURL:url];
But shows this confirmation dialog:
QUESTION
Now, I have a new requirement, to make the phone call without confirmation or prompt. So... There is some way to make a phone call in iOS omitting the confirmation prompt?
I want something like
NSURL *url = [NSURL URLWithString:#"telnoprompt://637******"];
return [[UIApplication sharedApplication] openURL:url];
NSMutableCharacterSet *characterSet =[NSMutableCharacterSet characterSetWithCharactersInString:#" "];
NSArray *arrayOfComponents = [phone_number componentsSeparatedByCharactersInSet:characterSet];
phone_number = [arrayOfComponents componentsJoinedByString:#""];
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phone_number];
NSString *escapedUrlString = [phoneURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneURL = [NSURL URLWithString:escapedUrlString];

How to share content on WhatsApp from iOS

I would like to share one Url link and some text message into WhatsApp from my application. How can i share content?
I got this code for only text
NSString * msg = #"Trueman India Magazine";
NSString * urlWhats = [NSString stringWithFormat:#"whatsapp://send?text=%#",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
{
[[UIApplication sharedApplication] openURL: whatsappURL];
}
but how i share my url link in WhatsApp?
I had a problem with this whatsapp api with url strings, especially when they contained a query string with several fields, e.g. http://example.com/foo?bar=foo&foo=bar.
When opening the app I found the message text would be empty.
The solution was to properly percent escape the string using the CFString functions.
See the apple documentation here:
https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/index.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes
But for anyone else with this issue here is my solution in full:
CFStringRef originalURLString = (__bridge CFStringRef)[NSString stringWithFormat:#"%#", #"http://example.com/foo?bar=foo&foo=bar"];
CFStringRef preprocessedURLString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, originalURLString, CFSTR(""), kCFStringEncodingUTF8);
NSString *urlString = (__bridge NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, preprocessedURLString, NULL, CFSTR("!*'();:#&=+$,/?%#[]"), kCFStringEncodingUTF8);
NSString *whatsAppURLString = [NSString stringWithFormat:#"whatsapp://send?text=%#", urlString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:whatsAppURLString]];
Note the use of the characters to be escaped in the CFURLCreateStringByAddingPercentEscapes function.
Include the plain link inside the text, e.g.:
NSString * msg = #"Trueman India Magazine http://www.truemanindiamagazine.com";
The link will be generated/tappable after sending it to someone
We can achieve this by using simple jquery. here is the article link http://www.stepblogging.com/how-to-share-web-article-on-whatsapp-using-jquery/
and you can check demo on your smart phone Demo Link

IOS IBAction button not going to website using URL

i have a button on a view controller and i want to click the button and it goes to a web site.
the website is held on parse.com.
the code as follows
- (IBAction)WebAddressBtn:(id)sender {
NSString *url = [self.exam objectForKey:#"Website"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(#"website: %#",url);
}
the NSLog shows Null for the value url
but the data is held at
self.exam objectForKey:#"Website"
NSLog confirms its there
this works and will go to google
- (IBAction)WebAddressBtn:(id)sender {
NSString *url = #"http://www.google.com";
//[self.exam objectForKey:#"Website"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(#"website: %#",url);
}
if i try
NSURL *url = [self.exam objectForKey:#"Website"];
url is still showing as Null
but i know the data is in self.exam objectForKey:#"Website
NSLog Output for data
name = "DMK Media & Photography Ltd";
phone1 = 01993835148;
phone2 = 07795966848;
postcode = "OX28 4BT";
products = "<PFRelation: 0x10dc75320>(<00000000 00000000>.(null) -> products)";
website = "http://www.dmkmedia.co.uk";
[UIApplication sharedApplication] openURL wont work properly if it doesnot have properly formatted url. Please check whether your url has "http://"

Map app not working

Have a look at the below code it's working properly
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
but it opens map in safari browser.
after that i've tried below code
NSString *urlString = [NSString stringWithFormat:#"http://maps.apple.com/maps?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
it open map app as i desired but not giving me the directional result with route or the same result that above google map gives how can i achieve the result as first code in map app?
please help!!
Apple's map does not have the same API as google maps, thus you URL will not work. With iOS 6 apple introduced the MKMapItem which allows developers to interact with the maps.app.
If you want to keep using the the maps via http then you should change you url:
NSString *urlString = [NSString stringWithFormat:#"http://maps.apple.com/?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
As stated in the Apple URL Scheme Reference you should not add the /maps/ path.
URLs that contain no path parameters or that contain specific map
paths are opened in Safari and displayed there. For example, URLs
based on the paths http://maps.apple.com/, http://maps.apple.com/maps,
http://maps.apple.com/local, and http://maps.apple.com/m are all
opened in Safari. To open a URL in the Maps app, the path must be of
the form http://maps.apple.com/?q.
The rules for creating a valid map link are as follows:
The domain must be maps.apple.com.
The path cannot be /maps/*.
A parameter cannot be q=* if the value is a URL (so KML is not picked up).
The parameters cannot include view=text or dirflg=r

Resources