is it possible to execute:
[UIWebView loadRequest:[NSURLRequest requestWithURL:url]];
in a way that I know when UIWebView is finished loading the NSURL so I can proceed with further processing ? Other than using a delegate. If not what are the conditions under which the delegate will work.
Thank you for any tips/ideas
To explain further. I have this code that worked for me in a test application:
webview = [[UIWebView alloc] init];
webview.delegate = self;
[webview loadRequest:[NSURLRequest requestWithURL:url]];
In the test app's header I had:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webview;
}
I'm trying to replicate this in a very complex environment that I don't fully understand.
I don't know where to put since it's not clear to me. The interface is defined as:
#interface Stream (Extension)
#end
and it has four more interface defined later in the header file.
In my implementation I get "undeclared identifier self"
You could find out when the web view has finished fetching the content of the NSURL and any embedded content requests in the page if you used an NSURLProtocol to intercept the network traffic (but you would have to know in advance if the html page is going to make requests to fetch i.e. images).
But you cannot detect when the UIWebView has loaded the dom or finished displaying the page
without the delegate (out of curiosity may I know why you are asking such a question).
Related
I have a strange problem and I have no idea how to solve it. I have a root viewcontroller. Via button you can switch to the child viewcontroller. In the child view controller I open a link like this as a request to the webview:
myURL = [NSURL URLWithString:#"http://m.imdb.com/video/imdb/vi2284695065/"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
the video on the page is autoplaying and switching to fullscreen mode. In exactly this moment I get a log message which indicates that the viewdidload method on my rootviewcontroller is called. Which eventually makes my code not working anymore. But why? Its not the viewdidload of the current viewcontroller its the one of the rootviewcontroller?! I donĀ“t know what kind of code I should provide actually...
Edit
I switch to viewcontroller like this:
UIViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:#"child"];
[self presentViewController:NVC animated:NO completion:nil];
The rootviewcontroller does not get a received memory warning.
Note: I have looked at other questions and the answers to them are quite vague or unhelpful
I have this code in View_Controller.h
#property AVAudioPlayer *playerSaxophone;
Then I do this in the same file (in viewDidLoad):
NSURL *backgroundMusicSaxophone = [NSURL fileURLWithPath:
[[NSBundle mainBundle]
pathForResource:#"saxophone" ofType:#"wav"]];
self.playerSaxophone = [[AVAudioPlayer alloc]
initWithContentsOfURL:backgroundMusicSaxophone error:nil];
self.playerSaxophone.numberOfLoops = -1;
[self.playerSaxophone setVolume:0.5];
[self.playerSaxophone play];
In a different view controller I want to be able to stop or start this audio from playing by clicking 2 buttons. Is there any way I can do this?
Edit: I tried this in the "different" view controller .m file
//I do import ViewController.h in this file
- (IBAction)stop:(id)sender {
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[viewController.playerSaxophone stop];
}
But it didn't work.
You need to call:
[playerSaxophone stop];
to make the music stop.
Problem: Another view controller won't have a reference to playerSaxophone.
The fact that you want to add code that references this from another view controller means you'll have to find a way to have the three objects - view controller A, view controller B, and your audio player - communicate.
Here are a few options:
Use the delegation pattern to have one view controller tell the other view controller to stop the player.
The same as #1, but using the notification pattern. (This is ideal if you'd have a 3rd place that controls the player).
You could move playerSaxophone to be a #property on either your App Delegate or a singleton. Then each view controller will be able to find it.
These are just a few solutions. There are others.
Additional reading:
Apple's Coordinating Efforts Between View Controllers
This Stack Overflow answer
How do I set up a simple delegate to communicate between two view controllers?
I have some issues with this UIWebViewDelegate method.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Issue:
While changing any tab or clicking on any link, this method gets called automatically.
It was working fine for me also, but surprisingly it's not getting called since yesterday. I tried everything, but it's still not working. How can I resolve this?
Check u have done this:
#interface yourViewController : UIViewController <UIWebViewDelegate>
Also check add this line:
yourWebView.delegate = self;
IF you are making webView using xib then connect its delegate to file's owner, or if you are making webView through code then write
myWebView.delegate = self;
This is probably a simple question...
I have a UIView with a button that corresponds to a URL address http:// etc. I want to be able to click this web address button and have it load a UIWebView on a separate UIViewController and XIB that shows the website, allowing me to then hit a back button and go back to my first view that has the original button. Basically I want to be able to load the webpage but have it operate within the App rather than start up Safari. Is this possible?
So far I have build a dedicated XIB and ViewController for my webpage for which the viewDidLoad method looks like this:
- (void)viewDidLoad{
[super viewDidLoad];
NSString *urlAddress = pushURL; //pushURL is passed as parameter
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObject];
[addressBar setText:urlAddress];
}
Question is... how do I call the UIWebView from my button (which is in the first class/parent viewController)? All my other viewControllers in my app are using UITableView so it's a simple DidSelectRowAtIndex.
Thanks.
It sounds like you want to be using a UINavigationController. Then when the button is tapped just push the UIViewController with the web view to the UINavigationController.
I have an app at the moment that when a button is pushed on the first screen does some work and makes a URL, and then does
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:currentURL]];
which launches Safari with my URL. I want instead to have a webview launch from here so the user can do some custom things with it. I am still having a hell of a time understanding MVC in iOS and need help. My project is minimal and consists of an AppDelegate.h/m and a ViewController.h/m, the.m of the view controller is where the function that does this Safari launch lives.
Can anyone help me understand how to do what I'm trying to d?
Thanks...
The simplest way is just to add a UIWebView when the button gets pressed. Add this method to your ViewController.m and have this be performed when the button gets pressed.
Programmatically:
//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; //Change self.view.bounds to a smaller CGRect if you don't want it to take up the whole screen
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
[self.view addSubview:webView];
[webView release];
}
Using Interface Builder:
1) Add a UIWebView object to your interface.
2) Set the "hidden" property to checked (in the "Attributes Inspector" window in Interface Builder). You'll keep it hidden until you want to show it.
3) Add the following code to your ViewController.h, below the other #property lines:
#property (nonatomic, retain) IBOutlet UIWebView *webView;
4) Add the following line below the #synthesize in your ViewController.m
#synthesize webView;
And add [webView release]; in the dealloc method.
5) Go back into IB and click on File's Owner, and connect the webView outlet to the webView you created.
6) Add the following method instead of the method I showed above (for the programmatic example):
//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
self.webView.hidden = NO;
}
You could set up an IBAction method to respond to the button press, but it sounds like you already have the button press working, so I wouldn't bother with that now.
As far as adding a button above the web view, you can either subclass web view and add the button there, or just have a separate button you define in your nib or programmatically and have that hide the webView to "get rid of it".
For adding UIWebView using Swift to your app just drag the WebView to your story board and then using an assistant editor connect WebView to ViewController.swift
And then Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url.
let url = NSURL (string: "https://www.simplifiedios.net");
let request = NSURLRequest(URL: url!);
webView.loadRequest(request);
As simple as that only 3 lines of codes :)
Ref: UIWebView Example
Since this is the top result on Google, if you can use WKWebView instead of UIWebView, you should.
import WebKit
let webViewConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: CGRect(), configuration: webViewConfiguration)