WebView doesn't load URL passed from another class - objective-c

I have two class (AppDelegate and WebViewController) with two nib file: one is the main window and one is a window with WebView. In AppDelegate I have this code to show WebViewController Window at launch:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.iTunes = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveNotification:) name:#"com.apple.iTunes.playerInfo" object:nil];
[self.lyricsTextView setAlignment:NSCenterTextAlignment];
[self refreshLyrics];
// Here I show my WebViewController Window
newWebViewController = [[WebViewController alloc] init];
[NSBundle loadNibNamed:#"WebViewWindow" owner:newWebViewController];
}
My WebView initialize itself correctly. In WebViewController I have this code:
- (void)goToURLFromExternal:(NSString *)urlToGo {
NSLog(#"%#", urlToGo);
NSURL *newURL = [[NSURL alloc] initWithString:urlToGo];
NSLog(#"%#", newURL);
NSURLRequest *newRequest = [[NSURLRequest alloc] initWithURL:newURL];
NSLog(#"%#", newRequest);
[self.myWebView.mainFrame loadRequest:newRequest];
}
But when I use from AppDelegate this code:
[newWebViewController goToURLFromExternal:[[urlArray objectAtIndex:0] stringValue]];
my WebView doesn't do anything. The page loaded is still the same.
Here there is the output of console for (void)goToURLFromExternal:(NSString *)urlToGo:
2011-10-04 16:55:54.838 iTunes Lyrics[25379:707] http://lyrics.wikia.com/Cake:Dime
2011-10-04 16:55:54.838 iTunes Lyrics[25379:707] http://lyrics.wikia.com/Cake:Dime
2011-10-04 16:55:54.838 iTunes Lyrics[25379:707] <NSURLRequest http://lyrics.wikia.com/Cake:Dime>
Someone have any idea about to resolve my issue?
Thank you so much!!!

Have you link your WebViewController class with the WebViewWindow.nib?
In The WebViewWindow.nib set the File's Owner Class property to WebViewController.

Related

playerItemDidReachEnd moves to wrong ViewController

My app has a start page and a second page with buttons that play videos. When a video is finished the AVPlayer is dismissed with playerItemDidReachEnd. But for some reason it shows the start page and not the second page when the player is dismissed. How can I get it to show the second page instead?
#import "ViewController.h"
#import AVKit;
#import AVFoundation;
#interface ViewController ()
#property(nonatomic, readonly) AVPlayerItem *currentItem;
#end
#implementation ViewController
AVPlayerViewController *playerViewController;
- (void)viewDidLoad {
[super viewDidLoad];
playerViewController = [[AVPlayerViewController alloc] init];
}
-(void)playerItemDidReachEnd:(NSNotification *) notification {
//remove the player
[self dismissViewControllerAnimated:NO completion:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (IBAction)playVideo:(id)sender {
NSURL *url = [NSURL URLWithString:#"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
UIScreen *mainScreen = [UIScreen mainScreen];
CGRect viewRect = mainScreen.bounds;
playerViewController.view.frame = viewRect;
playerViewController.showsPlaybackControls = YES;
[self.view addSubview:playerViewController.view];
[player play];
/* When the player item has played to its end time we'll dismiss the controller */
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerViewController.player.currentItem];
}
#end
If I understand your code correctly, your "second page" is ViewController.
You are adding AVPlayerViewController by [self.view addSubview:playerViewController.view];, so you need to remove this view by calling
[playerViewController.view removeFromSuperview].
After [self dismissViewControllerAnimated:NO completion:nil];, you dismiss whole ViewController, not only AVPlayerViewController.

Objective C - When tapping the tab bar item again, how to reload web view again?

I really need help please. Initially, by tapping on a tab bar item, I will load a webview on Gym VC user will then scroll down the web view
What I am trying to accomplish is when user tap on the same tab bar item again, the webView will reload again. I managed to call the method handleRefresh from MyTabBarController But the method doesn't do anything at all. Any guidance is much appreciated.
Currently I have the following code
MyTabBarController.m
#import "GymNavigationController.h"
#import "Gym.h"
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass:[GymNavigationController class]]){
Gym *myGym = [[Gym alloc] init];
[myGym handleRefresh:nil];
}
}
Gym.m
#import "Gym.h"
#import "AppDelegate.h"
#import "GymDet.h"
#import <QuartzCore/QuartzCore.h>
#interface Gym () {
AppDelegate *appDelegate;
NSString *sURL, *sRefresh;
}
#end
#implementation Gym
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp?"];
NSLog(#" The sURL to be load for the current page : %# ", sURL);
sRefresh = sURL;
[defaults setObject:sRefresh forKey:#"txtRefresh"];
[defaults synchronize];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView addSubview:refreshControl];
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//[self handleRefresh:nil];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
-(void)viewDidDisappear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
- (void)handleRefresh:(UIRefreshControl *)refresh {
//--- Call the stored txtMemCode, something like session ---
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sRefresh = [defaults objectForKey:#"txtRefresh"];
NSURL *url = [NSURL URLWithString:sRefresh];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[refresh endRefreshing];
}
First i would like to suggest why you're initiating a new Gym object every time tabbar is tapped you should take a reference of the Gym object and initialise it if its nil then process ahead with the reference, but it maybe your requirement and if it is i would like to suggest you to create a BOOL variable lets say isUpdateRequire and when you're instantiating your viewcontroller assign isUpdateRequire = true. Then in the end of view did load or view will appear (according to your need) check
if isUpdateRequire {
[self handleRefresh:nil]
}
Alternatively you can create protocols in your webviewcontroller and assign its delegate to your tabbarcontroller and fire the delegate method when ever required.
and if you don't want the method to call when you come back to vc simply put this condition in viewWillAppear
if self.isMovingToParentViewController {
[self handleRefresh:nil]
}

Background Video Objective-c

I want to play a loop background video in my view. I wrote this code :
-(AVPlayerLayer*)playerLayer{
if(!_playerLayer){
// find movie file
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"fond_login" ofType:#"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:[[AVPlayer alloc]initWithURL:movieURL]];
_playerLayer.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[_playerLayer.player play];
return _playerLayer;
}
return nil; }
-(void)replayMovie:(NSNotification *)notification {
NSLog(#"Finis");
[self.playerLayer.player play];}
And in my viewdidload, I have :
[self.view.layer addSublayer:self.playerLayer];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(replayMovie:)
name: AVPlayerItemDidPlayToEndTimeNotification
object:nil];
My problem is that the video don't play in loop. I see the video one time and it's finish. I don't know why ..
Thank's
EDIT :
I see another tutorial.
#import MediaPlayer;
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
My code in viewDidLoad :
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"my_video" withExtension:#"format"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayer.view.frame = self.view.frame;[self.view insertSubview:self.moviePlayer.view atIndex:0];
[self.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loopVideo) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
And :
- (void)loopVideo {
[self.moviePlayer play]; }
It's work perfectly.
In your replayMovie: method you need to seek back to the beginning:
- (void)replayMovie:(NSNotification *)notification {
[(AVPlayerItem *)notification.object seekToTime:kCMTimeZero];
}
You're also going to want to set the actionAtItemEnd of the AVPlayer so it doesn't pause:
_playerLayer.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

How to solve NSViewController/ NSView error in connect the action with target?

I have some Default NSViewController. In it I have code:
- (void)viewDidLoad {
// Do view setup here.
NSUserDefaults *defaults = [[NSUserDefaults alloc] init];
if(![defaults boolForKey:#"isLoggedIn"]){
NSLog(#"Not logged in... Loading settings");
NSViewController *settings = [[NSViewController alloc] initWithNibName:#"Settings" bundle:nil];
[self.view addSubview:settings.view];
}
else{
NSLog(#"Logged in... Loading inbox");
NSViewController *inbox = [[NSViewController alloc] initWithNibName:#"Inbox" bundle:nil];
[self.view addSubview:inbox.view];
}
[super viewDidLoad];
}
That code works it loads view depends on that NSUserDefaults.
But when try to declare something in lets say Inbox.m/Inbox.h I am geting this error:
Could not connect the action testView: to target of class NSViewController
In Inbox.m I have:
- (void)viewDidLoad {
NSLog(#"Inbox loaded.");
[super viewDidLoad];
// Do view setup here.
}
And that NSLog is never executed. I want to have several nibs and different NSViewController connected to it.
My guess is that I am adding subviews wrong way. It is like visual elements are there and nothing else.
I solve this by:
Inbox *inbox = [[Inbox alloc] initWithNibName:#"Inbox" bundle:nil];
_viewController = inbox;
//Where _viewController is
NSViewController *_viewController;
//defined in
#interface Default : NSViewController{
NSViewController *_viewController;
}
Now everything works ok.

Problem with video on NavigationController + TabBarController

I have an application with a TabBarController containig two TabBars with a NavigationController.
In the first tabbar, I have a ViewController containing some buttons. One of these buttons enable the user to play video. And all run fine for the moment.
My problem is that when the video is being played and I move to the second TabBar, the video continue playing (I hear the sound), then if I come back to the first TabBar (where there is my buttons) the application crashes without any notification.
I don't understand if it's a memory problem or something else.
Any suggestions?
Here is the function used :
-(IBAction)playMovie:(id)sender
{
videoView = [[UIViewController alloc]initWithNibName:#"video" bundle:nil];
videoView.title = #"title";
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"video.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
testbtnAppDelegate *appDelegate = (testbtnAppDelegate *)[[UIApplication sharedApplication] delegate];
videoView.view=moviePlayerController.view;
[moviePlayerController play];
[appDelegate.navController pushViewController:videoView animated:YES];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
[moviePlayerController pause];
before u go to secondtab pause.