I have a table which lists out items I would to make each of the row in the table to point to a URL which is assigned to each item.
In your tableview delegate function didSelectRowAtIndexPath you can use this code to open a url in the Safari application.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSURL *url = [NSURL URLWithString:#"http://stackoverflow.com"];
[[UIApplication sharedApplication] openURL:url];
}
If you don't want to leave your app, you can open the url in a UIWebView.
UIWebView * wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,460)];
[self.view addSubview:wv]; // or push onto Navigation Stack
// if adding wv as subview, also need to add a back button to self.view to dismiss webview.
[wv loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString: myURLForSelectedRow]]];
[wv autorelease];
When a cell is selected, the -tableview:didSelectRowAtIndexPath: delegate method will be called. From there, assuming you have the url for each row, the following code will open the URL url in Safari.
NSURL *url = /* Assume this exists */;
if( [[UIApplication sharedApplication] canOpenURL:url] )
{
[[UIApplication sharedApplication] openURL: url];
}
Related
I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.
shouldStartLoadWithRequest should do the trick, but that method is never called when I click to a link. I cannot find why this doesn't work.
I have read somewhere that I need to add this line :
webView.delegate = (id)self;
I tried it, and still get the same issue. Please help
AppDelegate.m
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL] absoluteString] ;
NSLog(#"Url: %#", currentURL);
return YES;
}
Controller.m
- (void)viewDidLoad
{
webView.delegate = (id)self;
NSString *tokenString = #"123";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"http://192.168.1.69:8888/webapp/test.php?uid=%#", tokenString];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
If your Controller instance adds itself as the delegate then the method you add to the AppDelegate instance will never be called.
Move the shouldStartLoadWithRequest method into your Controller class.
You've set your view controller as the web view's delegate. But then you've implemented the UIWebView delegate method in our application's delegate instead.
Try implementing that delegate method in your view controller. Your view controller is your web view's delegate.
i have one problem with my app.Im building an app having navigation controller.when i select a particular row,its related url will be loaded.it is working fine.but how to get back from this view to the previous view?I tried to place a navigation bar top of the view but it is not visible.could u please help me out.....
I placed the following coding in did selectrowmethod
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the selected country
NSString *selectedCountry = [listOfItems objectAtIndex:indexPath.row];
//NSString *url=#"";
if (indexPath.row==0) {
//url=#"http://www.osha.gov/dcsp/osp/stateprogs/alaska.html" ;
//label.text=url;
NSLog(#"======");
NSURL *url = [[NSURL alloc] initWithString:#"http://labor.state.ak.us/lss/oshhome.htm"];
[[UIApplication sharedApplication] openURL:url];
// self.label.text=#"url";
// NSLog(#"++++++:%#",self.label.text);
//textView.text=#"url";
}
else if(indexPath.row==1) {
NSURL *url = [[NSURL alloc] initWithString:#"http://www.ica.state.az.us/ADOSH/"];
[[UIApplication sharedApplication] openURL:url];
//url=#"http://www.osha.gov/dcsp/osp/stateprogs/arizona.html";
}
else if(indexPath.row==2) {
NSURL *url = [[NSURL alloc] initWithString:#"http://www.dir.ca.gov/occupational_safety.html"];
[[UIApplication sharedApplication] openURL:url];
//url=#"http://www.osha.gov/dcsp/osp/stateprogs/arizona.html";
}
else if(indexPath.row==3) {
NSURL *url = [[NSURL alloc] initWithString:#"http://www.ctdol.state.ct.us/osha/osha.htm"];
[[UIApplication sharedApplication] openURL:url];
//url=#"http://www.osha.gov/dcsp/osp/stateprogs/arizona.html";
}
}
Your code opens Safari. You can't go back from there. I suggest you check out UIWebView.
Is it possible to launch an application at the end of phone call or after sending SMS?
NO this is not at all possible. Because you cant get the event when the call is ended or message is sent. So theres no way you can open up the application.
Happy Coding...
I got this code from Apple site and it works perfectly:
- (IBAction) dialNumber:(id)sender{
NSString *aPhoneNo = [#"tel://" stringByAppendingString:[itsPhoneNoArray objectAtIndex:[sender tag]]] ;
NSURL *url= [NSURL URLWithString:aPhoneNo];
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
if ([osVersion floatValue] >= 3.1) {
UIWebView *webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
webview.hidden = YES;
// Assume we are in a view controller and have access to self.view
[self.view addSubview:webview];
[webview release];
}
else {
// On 3.0 and below, dial as usual
[[UIApplication sharedApplication] openURL: url];
}
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:aPhoneNo]];
}
Looks like it might help ..
iPhone SDK: Launching an app after call ends
So i'm building a webView into my application to show the contents of a URL that I am passing from a selection in a UITableView. I know the UIWebView is loading content properly because if you hard code say http://www.google.ca into the NSURL then it loads fine, however when I'm passing the URL that I parsed from an RSS feed back from the UITableView it won't load the URL properly. I tried the debugger and the URL is coming out as nil right before I try and parse it, however I can use NSLog to print the value of it out to the console.
here's the code in my UIViewController that has my UIWebView
#import <UIKit/UIKit.h>
#interface ReadFeedWebViewController : UIViewController
{
NSString *urlToGet;
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain) IBOutlet UIWebView *webView;
#property(nonatomic, retain) NSString *urlToGet;
#end
Here's the code for my implementation's viewDidLoad method...
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Url inside Web View Controller - %#", urlToGet);
NSURL *url = [NSURL URLWithString:urlToGet];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Once again, I can print the URL to NSLog fine and if I hard code the URL into the NSURL object then it will load fine in the UIWebView. Here is where I'm setting the value in my UITableViewController...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReadFeedWebViewController *extendedView = [[ReadFeedWebViewController alloc] init];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
extendedView.urlToGet = [[stories objectAtIndex: storyIndex] objectForKey:#"link"];
//NSLog([[stories objectAtIndex: storyIndex] objectForKey:#"summary"]);
NSLog([[stories objectAtIndex: storyIndex] objectForKey:#"link"]);
[self.navigationController pushViewController:extendedView animated:YES];
[extendedView release];
}
However, since I can print the value using NSLog in the extendedView view controller I know it's being passed properly. Cheers
The urlToGet is null into your ReadFeedWebViewController in the viewDidLoad method because when this method is call this variable is not yet affected. The viewDidLoad is call when the initialisation of the ViewController is finished.
I should you to call it in the viewWillAppear method like that:
- (void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:urlToGet];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Also, there is a memory leak in your code. You should release extendedView after pushing it on the navigation controller.
ReadFeedWebViewController *extendedView = [ReadFeedWebViewController new];
if (extendedView != nil) {
... setup extendedView here ...
[self.navigationController pushViewController:extendedView animated:YES];
[extendedView release];
}
Try this
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
This time, I was wondering when I was supposed to set my properties ...
I got a nav bar which I use to push a new controller (controlling a Web View) :
NewsViewController *webViewController = [[NewsViewController alloc] init]; // I create my controller
webViewController.urlText = #"http://www.google.fr"; // I set the property
InfonulAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.newsNavController pushViewController:webViewController animated:YES];
[webViewController release];
I don't know why but the code below doesn't work :
- (void)viewDidLoad { //viewDidLoad from my webViewController
[super viewDidLoad];
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlText];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
I just want to create a UIWebView but I need to give the controller the URL to use !
Any idea where and when my urlText property needs to be set ?!?
Cheers,
Gauthier
Do you use property correctly? Like
#property(nonatomic,retain) NSString *urlText;
If so, try to use a customized init method like this;
-(id)initWithUrl:(NSString *)url
{
if(self = [super init])
{
self.urlText = url;
}
return self;
}
dont forget to release urlText in dealloc. Now use;
NewsViewController *webViewController = [[NewsViewController alloc] initWithUrl:#"someUrl"];