Not able to get the data in a webview in detailview of splitviewcontroller - iphone

In my ipad app i'm not able to get the data in a web view. That web view is in a detail view of splitviewcontroller. That data is coming from a link.
I'm using this code.
In split view table did select row at index path
post=[self.postListMutableArray objectAtIndex:indexPath.row];
PostDetailsViewController *postDetails=[[PostDetailsViewController alloc]initWithNibName:#"PostDetailViewController_Ipad" bundle:nil];
postDetails.detailItem=post.postLink;
In split view detail view
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem)
{
_detailItem = newDetailItem;
// Update the view.
NSLog(#"detail Item String is %#",_detailItem);
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
NSString* str =_detailItem;
NSURL* url = [NSURL URLWithString:str];
NSLog(#"URL is ----> %#",url);
self.webView.scalesPageToFit=YES;
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
//[self.view addSubview:webView];
}

create new Variable at PostDetailsViewController .h class:-
#property(strong,nonatomic)NSString *strGetPostDetail;
and synthesize PostDetailsViewController .m
#synthesize strGetPostDetail
Now you can call splitViewTable Delegate DidSelect method:-
post=[self.postListMutableArray objectAtIndex:indexPath.row];
PostDetailsViewController *postDetails=[[PostDetailsViewController alloc]init];
postDetails.strGetPostDetail=post;

Related

Press a Button and open a URL in another ViewController

I am trying to learn Xcode by making a simple app.
But I been looking on the net for hours (days) and I cant figure it out how I make a button that open a UIWebView in another ViewController :S
first let me show you some code that I have ready:
I have a few Buttons om my main Storyboard that each are title some country codes like UK, CA and DK.
When I press one of those Buttons I have an IBAction like this:
- (IBAction)ButtonPressed:(UIButton *)sender {
// Google button pressed
NSURL* allURLS;
if([sender.titleLabel.text isEqualToString:#"DK"]) {
// Create URL obj
allURLS = [NSURL URLWithString:#"http://google.dk"];
}else if([sender.titleLabel.text isEqualToString:#"US"])
{
allURLS = [NSURL URLWithString:#"http://google.com"];
}else if([sender.titleLabel.text isEqualToString:#"CA"])
{
allURLS = [NSURL URLWithString:#"http://google.ca"];
}
NSURLRequest* req = [NSURLRequest requestWithURL:allURLS];
[myWebView loadRequest:req];
}
How do I make this open UIWebview on my other Viewcontroller named myWebView?
please help a lost man :D
Well, you've firstViewController already designed and coded, now you've to create secondViewController with a UIWebView binded in IB it self, also it have a setter NSString variable like strUrl that you need to pass at the time of pushing or presenting secondViewController, and assign it to UIWebView in viewDidLoad of secondViewController. See my answer about how to pass a NSString? Also UIWebView has its delegates method (What's delegate & datasource methods? - Apple Doc) Which you can use to handle URL request.
These are the delegate of UIWebView, if you'll going to use it, you need to give UIWebView delegate to self.
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
firstViewController.m
- (IBAction)ButtonPressed:(UIButton *)sender {
NSURL* allURLS;
//get your URL
secondViewControlelr *secondView=[[secondViewControlelr alloc]init];
second.urlToLoad=allURLS;
[self.navigationController pushViewController:secondView animated:YES];
}
secondViewControlelr.h
//declare url and set property
NSURL *urlToLoad;
secondViewControlelr.m
- (void)viewDidLoad
{
myWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:myWebView];
NSURLRequest *urlReq=[NSURLRequest requestWithURL:self.urlToLoad cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
[myWebView loadRequest:urlReq];
}

Universal iOS App : UIWebView in detailViewController does not display content for iPad part

I am working on a Universal iOS Application on Xcode 4. It's my first time trying to create an iPhone/iPad app. I am using the Master-Detail Application Template.
This app is roughly a RSS Feed Reader.
I have followed this tutorial to get the big idea: http://cocoadevblog.com/iphone-tutorial-creating-a-rss-feed-reader
I succeed to make the iPhone part working but i have a problem with the UIWebView in the iPad part.
Here is the code which may not be correct.
From MasterViewController ... :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (!self.detailViewController) {
NSDictionary *theItem = [items objectAtIndex:indexPath.row];
DetailViewController *nextController = [[DetailViewController alloc] initWithNibName:#"DetailViewController_iPhone" bundle:nil];
[nextController setDetailItem:theItem];
[self.navigationController pushViewController:nextController animated:YES];
}
}
else {
if (!self.detailViewController){
NSDictionary *theItem = [items objectAtIndex:indexPath.row];
DetailViewController *nextController = [[DetailViewController alloc] initWithNibName:#"DetailViewController_iPad" bundle:nil];
[nextController setDetailItem:theItem];
self.detailViewController = nextController;
}
else {
[self.detailViewController setDetailItem:[self.items objectAtIndex:indexPath.row]];
}
}
}
... to DetailViewController, here is the implementation of setDetailItem: and configureView :
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (_detailItem) {
NSLog(#"webpage creation");
NSString *html = [_detailItem objectForKey:#"title"];
[...html stuff added to html string...]
webView = nil;
webView.delegate=self;
//[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
[self.webView loadHTMLString:html baseURL:nil];
NSLog(#"webView loaded");
}
}
I got a blank screen on the detailView pane when i select a row on the MasterView (MasterView/detailView -> in the universal app case, the iPad app is a splitview style App).
I linked the webView in InterfaceBuilder (for both iPhone/iPad .xib files), i received every NSLog, but i cannot make the UIWebViewDelegate answer me even if i implemented it. Of course, the comment with loading the Google Home page doesnt work either.
UPDATE 25/10/11:
I created an new Universal App, a simple one, where i added a UIWebView and i try to load Google webPage.
I can load the page from the viewDidLoad/viewDidAppear (detailViewController) but not from configureView (detailViewController) which is called when i select a row (fire the setDetailItem in detailViewController).
It is like i cannot modify it after loading the panel. There is something I dont get.
(The code I use to launch a webpage is the same code line than the comment above in configureView)
UPDATE 26/10/11
The webView has no value (null) in configureView (when i try to display it for a selected row). I tried this, which is not working but I (obviously/at least) get a value for webView in that case :
- (void)configureView{
if (_detailItem) {
UIWebView *tempWebView = [[UIWebView alloc] init];
self.webView = tempWebView;
NSLog(#"webpage creation");
NSString *html = [_detailItem objectForKey:#"title"];
[...html stuff added to html string...]
//[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
[self.webView loadHTMLString:html baseURL:nil];
self.webView.delegate=self;//better to add "self" to get it working
NSLog(#"webView loaded");
NSLog(#"%#",self.webView); //i got this : <UIWebView: 0x6a5a550; frame = (0 0; 0 0); layer = <CALayer: 0x6a59750>>
}}
By writting "self.webView.delegate=self;" i got the delegate answering me. However this one produce two answers (??) like if there were two webView running ?:
2011-10-26 10:49:57.089 APPName[18334:f803] didStart Loading web Page
2011-10-26 10:49:59.416 APPName[18334:f803] didStart Loading web
Page
2011-10-26 10:49:59.523 APPName[18334:f803] finished loading web
page
2011-10-26 10:49:59.806 APPName[18334:f803] finished loading
web page
I finally got it working.
However, the only solution i've found is to create another project with STORYBOARD and not xib files, which was I guess the problem.
So i dont really have the answer for my problem, but i guess it is for sure a problem of link in the xib files. But i didnt find it.
With Storyboard, I worked out the iPad version easily, but not the iPhone version... until I found the good tutorial : http://www.youtube.com/watch?v=NHIOx_1mz-Q
So for the Storyboard, i deleted the tableview section (inserted by default) and put this code :
MasterViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
NSLog(#"SELECT ROW IPHONE");
if (!self.detailViewController){
NSDictionary *theItem = [items objectAtIndex:indexPath.row];
DetailViewController *nextController = [self.storyboard instantiateViewControllerWithIdentifier:#"Detail"];
[nextController setDetailItem:theItem];
[self.navigationController pushViewController:nextController animated:YES];
}
}
else {
NSLog(#"SELECT ROW IPAD");
self.detailViewController.detailItem = [self.items objectAtIndex:indexPath.row];
}
}
DetailViewController
- (void)configureView{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
self.webView.delegate=self;
NSString *html = #"";
[...html stuff...]
[self.webView loadHTMLString:html baseURL:nil];
}
}
I dont mention it, but i also created and linked webview of course.
If someone find the answer for the xib version of a simple project with TableView linked to a detailView for iPad (on Universal iOS project), i would like to ear it.
I don't know if this will fix your problem completely, but at least this line is suspicious:
webView = nil;
It looks to me like you are setting the webView to nil just before you set the delegate? If it is nil at that point, there is no way that the delegate is going to be set. In fact, when you try to send it any messages, it will just ignore them.
(Also that may be leaking memory.)
A split view controller must always be the root of any interface you create.
The panes of your split-view interface may then contain navigation controllers,
tab bar controllers,…
the split view controller automatically handles most of the rotation behaviors
And we can’t use UISplitViewController inside TabBars !!

MWFeedParser in-app web browser INSTEAD of safari

So i'm using the MWFeedParser, which I think is a beautiful RSS feed app, but when clicking on a link it goes to Safari. I fear that a good portion of iPhone users are unaware of the fact that they can double-click on the iPhone's one and only button to view all open apps and go back to the RSS feed, and will thus get stuck. Therefore I'd rather use an in-app browser.
In other words, currently when I click on a link from MWFeedParser it goes to Safari; I prefer it goes to an in-app browser.
I have the browser class set up called WebViewController.
basically with [detailWebView loadRequest:[NSURLRequest requestWithURL:detailURL]];
So "detailURL" is what it will look for
What do I put in the DetailTableViewController instead of the sharedApplication code???
bummer nobody was able to answer...in case others are looking for how to do it, change the contents of didSelectRowAtIndexPath to
if (_webViewController == nil) {
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
MWFeedItem *entry = [parsedItems objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
// Deselect
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
Add a WebViewController;
the .m has
#import "WebViewController.h"
#import "MWFeedItem.h"
#implementation WebViewController
#synthesize webView = _webView;
#synthesize entry = _entry;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:_entry.link];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)viewWillDisappear:(BOOL)animated {
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[_entry release];
_entry = nil;
[_webView release];
_webView = nil;
[super dealloc];
}
And the .h has
#class MWFeedItem;
#interface WebViewController : UIViewController {
UIWebView *_webView;
MWFeedItem *_entry;
}
#property (retain) IBOutlet UIWebView *webView;
#property (retain) MWFeedItem *entry;
(Basically I just took the webview part of Ray Wenderlich's RSS feeder and threw it in to this one.)

iPhone Creating a Modal Movie Window

I have created an app that incorporates a table view, detail view, and a web view. I have established a cell in the table view that takes a URL from an RSS feed and will display it in a webview. The URL is an .MP4 file which causes the video to play. The problem I am having is that when the video ends, I cannot go back to the previous screen. The code is below:
DetailViewController.m load webview
if (indexPath.section == SectionHeader && indexPath.row == SectionHeaderEnclosure) {
if (item.enclosures) {
for (NSDictionary *dict in item.enclosures){
NSString *url = [dict objectForKey:#"url"];
NSLog(#" url is : %#",url);
//EXPERIMENTAL
WebViewController *webVC = [[WebViewController alloc] initWithURL:url];
[self presentModalViewController:webVC animated:YES];
}
}
}
WebViewController.m
- (void)loadView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = webView;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
}
//Dismiss modal view
- (IBAction) done:(id)sender {
NSLog(#"done:");
[self dismissModalViewControllerAnimated:YES];
}
I think it would be better to use an MPMoviePlayerViewController. The documentation shows how to set it up with the URL directly using initWithContentURL: and present it modally. You can listen for notifications thet the movie has ended and dismiss it when done. Lots of good stuff in the documentation.

Catching modal UIWebView links touches

I have a view controller and I am intercepting the touches on links within the web views it manages.
My main view controller has this method.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
//I can see this request come in upon a touch
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(#"raw: %#", urlString);
//do some stuff (like figure out what capturedFilename is)
ExplainViewController *explanation = [[ExplainViewController alloc] initWithNibName:#"ExplainViewController" bundle:nil file:capturedFilename];
[self.navigationController presentModalViewController:explanation animated:YES];
}
And this loads a modal view properly.
ExplainViewController has a webView itself. When a user touches a link within ExplainViewConroller I'd like to handle that request too (and present another modal view).
ExplainViewController has this but I get no log activity from either method (the following nor the previous):
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(#"raw: %#", urlString);
}
I want to know where this link touch request is going and how I can intercept it.
Both mentioned view controllers are using this in their .h
<UIWebViewDelegate>
You mention that both view controllers conform to the UIWebViewDelegate protocol, but is the delegate outlet of the UIWebView in the nib for ExplainViewController.h set to an instance of ExplainViewController. It will not call the method unless the UIWebView delegate property is set.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
BrowserController *browserController = [[BrowserController alloc] initWithUrl:request.URL.absoluteString];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browserController];
[self.navigationController presentModalViewController: navController animated:YES];
[browserController release];
[navController release];
return NO;
}
return YES;
}