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.
Related
i create an app with xcode 4.6 on iOS 6.1 with a TabBar and 2 different tableView.
Each table view read a line form a file.plist and when tap on a row, you load a DetailView.
The DetailViewController is the same for the 2 TableView but:
If start app tap on a first button i see FirstView with my table and Audio List, if I choose one file on list I load a Detail view and listener my file very well, but if now tap on second view on TabBar i can start playing other audio file from the second view but i listen overlay to firs audio and AVplayer not override.
There my coding:
From TableView loading plist:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([#"detailList" isEqualToString:segue.identifier]) {
NSIndexPath *index = [self.tableView indexPathForCell:sender];
[[segue destinationViewController] setSaved:[loadData objectAtIndex:index.row]];
}
}
In a detali view:
if (mysound.rate == 1.0) {
UIImage *img = [UIImage imageNamed:#"Play_BT.png"];
[control setImage:img forState:UIControlStateNormal];
[mysound pause];
} else {
UIImage *img = [UIImage imageNamed:#"Pause_BT.png"];
[control setImage:img forState:UIControlStateNormal];
NSURL *url = [NSURL URLWithString:[saved objectForKey:#"audio"]];
mysound = [[AVPlayer alloc] initWithURL:url];
[mysound setAllowsExternalPlayback:YES];
[mysound setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[mysound play];
}
}
Here incoming the issues, this code working very goog, but if tapo on tab bar and load other audio from other plist I listen Audio overlay.
How can use AVPlayer in Override the NSURL *url = [NSURL URLWithString:[saved objectForKey:#"audio"]];when change from tabbar?
Thanks.
Solved, using this code:
In my AppDelegate .h adding
#interface AppDelegate : UIResponder <UIApplicationDelegate> {
AVPlayer *MyPlayer;
}
#property (strong, nonatomic) AVPlayer *MyPlayer;
in my AppDelegate .m inside didFinishLaunchingWithOptions adding
[MySound superclass];
Now can calling a method in all app with this code directly inside a .m file:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSURL *url = [NSURL URLWithString:[saved objectForKey:#"link"]];
appDelegate.MySound = [[AVPlayer alloc] initWithURL:url];
[appDelegate.MySound setAllowsExternalPlayback:YES];
[appDelegate.MySound setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[appDelegate.MySound play];
With this method now is possible controlling the AVPlayer evry where in the APP!!
I Hope this Help any guys ;)
I am new to iOS and Objective C programming world. I am trying to create a App which will scan the barcode then use that data to go to a website.
Now, the only issue is when I load the website using a webview, I don't know how to return to mainview/rootview. As, I have made this a view based App, I don't know how to implement navigation controller or simply a back button would do.
I have used this SDK: http://zbar.sourceforge.net/iphone/sdkdoc/
The code:
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
// ADD: get the decode results
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
// EXAMPLE: just grab the first barcode
break;
// EXAMPLE: do something useful with the barcode data
resultText.text = symbol.data;
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView= [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress= #"http://www.flipkart.com/search/a/books?query=";
urlAddress =[urlAddress stringByAppendingString:resultText.text];
NSURL *url= [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
// [webView release];
/*
// ADD: dismiss the controller (NB dismiss from the *reader*!)
[reader dismissModalViewControllerAnimated: YES];
}
How can I add a back button in the webview so that I can return to main view?
Here you have tutorial how to implement toolbar with UIWebView:
http://iosdeveloperzone.com/2011/05/21/tutorial-building-a-web-browser-with-uiwebview-part-1/
You don't have to implement the navigationBar.
You have create a UIViewController that contains your UIWebView, then you have to put this controller into your current UINavigationCotroller's stack by calling [self.navigationController pushViewController:yourVC animated:YES];, then as #mientus said, call [self.navigationController popToRootViewControllerAnimated:YES]; to back to the root view controller.
So in this case i suggest you to start a new Master-Detail Application project.
If you don't want to do this, in your case just set a tag to your webView and add a UIButton with an action like this:
-(IBAction)removeWebView:(id)sender{
[[self.view viewWithTag:yourWebViewTAG] removeFromSuperView];
}
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 !!
I have a UIWebView which loads a html string on viewDidLoad. It detects if the user clicks a link and display a modal view, but when i close the modal view the UIWebView's html has gone! If i use a FormSheet Modal Style the content stays behind, its only fullscreen modals that cause this. Obviously i can just input the html string again on viewWillAppear which fixes it but this causes makes the content flick back on which i don't want. heres my code:
-(void)viewDidLoad {
[self loadArticleText];
...
}
-(void)loadArticleText {
NSString *htmlHead = #"<html><head>...";
NSString *htmlFoot = #"</body></html>";
NSString *htmlContent = [self.articleData valueForKey:#"fulltext"];
NSString *html = [[NSString alloc] initWithFormat:#"%#%#%#", htmlHead, htmlContent, htmlFoot];
[self.articleView loadHTMLString:html baseURL:nil];
[html release];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
ImageViewer *imageView = [[[ImageViewer alloc] initWithNibName:#"ImageViewer" bundle:nil] autorelease];
imageView.imageURL = URL;
[self presentModalViewController:imageView animated:YES];
return NO;
} else {
return YES;
}
}
If i change 'return NO;' to 'return YES;' the webView contents stays, like it should, but obviously it loads the image.
Any help??
Thanks
[self presentModalViewController:ImageView animated:YES];
It looks like you are making a class call rather than an instance call on ImageView.
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;
}