iPhone: Open a url Programmatically - iphone

I am new to iPhone.
I want to open an url in my application.
How can I do this task? Please suggest me and provide some useful link.

Apparently the link given above is outdated. Here is the update link for the UIApplication class.
The quick and simple code snippet is:
// ObjC
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://www.google.com"]];
// Swift
UIApplication.shared.open(URL(string: "http://www.google.com")!, options: [:], completionHandler: nil)

Update (2016): The best way to do this nowadays is to instantiate and present an SFSafariViewController. This gives the user the security and speed of Safari, and access to any cookies or Safari features they may already have set without having to leave your app.
If you want to open the URL in Safari (and exit your application) you can use the openURL method of UIApplication
If you'd rather have it handled inside of your app, use WKWebView.

If you would like to open and just get the data from the URL, you could use NSString:
NSString *ans = [NSString stringWithContentsOfURL:url];
If what you are trying to get is an XML from a URL, you can directly use NSXMLParser:
NSURL *url = [[NSURL alloc] initWithString:urlstr];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
// parse here
[parser release];
[url release];
On the other hand, if by opening you mean, open a URl in an embedded browser, you could use UIWebView class.

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"https://medium.com/the-traveled-ios-developers-guide/swift-3-feature-highlight-c38f94359731#.83akhtihk"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://medium.com/the-traveled-ios-developers-guide/swift-3-feature-highlight-c38f94359731#.83akhtihk"]];
}
else{
[SVProgressHUD showErrorWithStatus:#"Please enable Safari from restrictions to open this link"];
}

Related

How to return to my iphone application after call ends?

I am able to call from my iphone application by using below code:
NSString *phoneNumber = #"tel://1234567890";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Now, i want to know how to return to my application back when the call ends ?
UIWebView *phoneCallWebview = [[UIWebView alloc] init];
// [self.view addSubview:phoneCallWebview];
NSURL *callURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", 9238928399]];
[phoneCallWebview loadRequest:[NSURLRequest requestWithURL:callURL ]];
As far as I'm aware, such interaction is impossible since your application has been demoted to background, and all UI interaction has been delegated to the Phone app, and the user.
I found this SO question
End call, don't return to app automatic in iphone 3.1 version
Which pointed to an article on apple dev forums
https://devforums.apple.com/message/128046 (dev account required)
Which says it was a change in iOS 3.1 but a "workaround" is
use UIWebView to open the tel: url, after the call, your app will relaunch, but you get the annoying do you want to make this call alert.
I have't verified this works as described, just thought I'd point it out
From iOS 5, use below...
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:#"12345678"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Just use telprompt:// instead of tel://
telprompt will prompt the user first, and when call ends,it will go back to your application.
NSString *myNumber = [#"telprompt://" stringByAppendingString:txtMobileNo.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:myNumber]];

how to open itunes link in iphone app?

I am trying to open itunes link on UIControlEventTouchDown button press event. My code is as follows.
NSString *urlString =#"http://itunes.apple.com/in/album/carnatic-vocal-sanjay-subrahmanyan/id106924807?ign-mpt=uo%3D4";
NSString *str_ur=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:str_ur];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview_buy loadRequest:requestObj];
webview_buy.delegate=self;
[self.view addSubview:webview_buy];
This will go to itunes in the device and open the itunes store. But i get an alert message that says
Your Request could not be Completed.
Please give idea with code.
First copy the url from iTunes which you want to open in app.
Then use below code:
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"http://itunes.apple.com/in/album/carnatic-vocal-sanjay-subrahmanyan/id106924807?ign-mpt=uo%3D4"]];
here, URLWithString value will be your app url which you want to open.
Let me know in case of any difficulty.
Use the itms-apps:// prefix to open in iTunes, e.g.
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=409954448"]];
It will work only with device. Throws Request could not be completed on simulator.

Iphone: how to launch a route in google maps api from your application

I have read a lot of topics, but i don't find when i want
I want to launch google map api from my application with that:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://maps.google.com/maps?saddr=41.029598,28.972985&daddr=41.033586,28.984546"]]
But it's open safari with Google maps page. I want open Google map API with my route. There is a maps:// or other things?
Thanks for help !!!
Try this
UIApplication *app = [UIApplication sharedApplication];
NSURL *url = [[NSURL alloc] initWithString: #"http://maps.google.com/maps?saddr=41.029598,28.972985&daddr=41.033586,28.984546"];
[app openURL:url];
[url release];
have a look at the Apple URL Scheme Reference for MapLinks
Edit: your URL looks correct. Looks like you are testing on Simulator! GoogleMaps app is only available on device!
Try this
NSString *urlstring=[NSString stringWithFormat:#"http://maps.google.com/?&saddr=%# &daddr=%#",sourceaddress,destinationaddress];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlstring]];

UIApplication sharedApplication openURL does not work with File:// (load local pdf file)?

This does not work
NSString *myPDF = #"file:///Users/sm/Documents/doc.pdf";
This works
NSString *myPDF = #"http://www.site.org/doc.pdf";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: myPDF]];
So should I create a web view and load the pdf on the web view?
you might want to take a look at UIDocumentInteractionController
here is a tutorial
http://blog.objectgraph.com/index.php/2010/06/14/ipad-sdk-3-2-document-support-1/

Send PDF to iBooks

Now iBooks supports PDF. My app has access to many PDF files. Is it possible to send PDFs to iBooks (or other PDF readers like GoodReader and iAnnotate PDF) from my app?
//get path to file you want to open
NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:#"readme" ofType:#"pdf"];
//create NSURL to that path
NSURL *url = [NSURL fileURLWithPath:fileToOpen];
//use the UIDocInteractionController API to get list of devices that support the file type
docController = [UIDocumentInteractionController interactionControllerWithURL:url];
[docController retain];
//present a drop down list of the apps that support the file type, click an item in the list will open that app while passing in the file.
BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
To answer my own question - yes, you can send a PDF to apps supporting custom URL scheme using this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
URL scheme for GoodReader is:
url=#"ghttp://www.mysite.com/myfile.pdf";
iAnnotate PDF is:
url=#"pdfs://www.mysite.com/myfile.pdf";
Does anyone know the custom URL scheme for iBooks?
Use this link to find the url schemes http://schemes.zwapp.com/
and use [[UIApplication sharedApplication] openURL:url];
to launch in that application.
thanks