I am parsing RSS feeds and loading results into a table. When the user clicks on a story, they are taken to the web page.
At the moment I am using:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:story]];
Trouble is that this launches Safari and closes my app.
Is there a way to open the webpage in my app so to speak? I'd like the webpage to appear with a "Done" button that closes the web page and returns the user back to the table.
Any help (and code examples) are much appreciated.
You can use UIWebView in a new view controller launched via navigationController pushViewController selector, and have a toolbar item to "View in Safari" which will launch full Safari browser (and putting your app in the background/terminating).
Some example code snippets:
Assumptions: (1) You have a view controller entitled: WebViewController (with same .xib) file. (2) This view controller has a UIView which is subview of the main view;
@interface WebViewController: UIViewController <UIWebViewDelegate> {
UIWebView *webview;
NSString *someUrlToLoad;
}
@property (nonatomic, retain) IBOutlet UIWebView *webview;
@property (nonatomic, copy) NSString *someUrlToLoad;
In this view controller's viewDidLoad method:
-(void) viewDidLoad {
[super viewDidLoad];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.someUrlToLoad]]];
}
You load this controller when someone clicked on the cell in your tableview:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewController *viewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
viewController.someUrlToLoad = <url for this cell>;
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
Most likely you will want to include some hook into the UIWebViewDelegate to stop/show an activity indicator in the navigation bar so the user is aware that something is happening while the page loads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With