What is the method for WebView finish loading? - objective-c

I need a method like - (void)applicationDidFinishLaunching:(NSNotification *)aNotification but I need it for my WebView IBOutlet WebView*webView;. What is the right method?

You have a few different options with WebView, as there are around 5 informal delegates which you can implement methods to:
For example, you might want to implement the frame load and resource load delegates to monitor the load progress and display status messages. Applications that use multiple windows may want to implement a user interface delegate. See the individual informal delegate protocols for more details: WebFrameLoadDelegate Protocol Reference, WebPolicyDelegate Protocol Reference, WebResourceLoadDelegate Protocol Reference, and WebUIDelegate Protocol Reference.
I find WebFrameLoadDelegate to be the easiest, just set yourself as the delegate:
[webView setFrameLoadDelegate:self];
Then implement this method:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
source: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/Reference/Reference.html

- (void)webViewDidFinishLoad:(UIWebView *)webView
It's in UIWebViewDelegate protocol.

First Bound ResourceDelegate of webview with file Owner
(void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
This will work for me perfectly.

Related

Why doesn't webFrameLoadDelegate protocol work?

I am just starting out at Xcode, and I'm trying to make a OS X web browser.
I want to figure out when my webview is loading and when it isn't. I've already looked at a lot of pages both from here and the Apple Developer Library on this, and this is what I get:
- (void)webViewDidStartLoad:(WebView *)webView {
//enter code here
}
And of course I also saw the webViewDidFinishLoad void, but when I try this in my AppDelegate.m nothing happens. I have connected the webview's frameLoadDelegate to the App Delegate and from what I understand, I also need to use the <> protocols in the AppDelegate.h file. My problem is that when I type in webFrameLoadDelegateProtocol into the <>s it tell me that webFrameLoadDelegateProtocol doesn't exist.
It appears to be because you haven't set the webView delegate. You do not need to add the protocol to your header though. Instead you need to add this code somewhere, I suggest in applicationDidFinishLaunching
[webView setFrameLoadDelegate:self];
Then you can override the methods. If that doesn't work, then make sure you have connected your webView from the header to the webView in your IB. Also be sure to synthesize the webView in the .m.
Finally, you could use my open source example for an OS X browser. It is under the MIT license, so you can use it freely.
https://github.com/JosiahOne/basic_cocoa_web_browser
EDIT
I just realized, you are using the wrong method for Cocoa. Use these methods instead.
-(void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
{
//Did start Load
}
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
//Did finish Load
}
My problem is that when I type in "webFrameLoadDelegateProtocol" into the "<>"s it tell me that "webFrameLoadDelegateProtocol" doesn't exist.
WebFrameLoadDelegate Protocol is an informal protocol. It is not eligible for adoption in the same way. Omit <webFrameLoadDelegateProtocol> from your class's #interface.
when I try this in my AppDelegate.m nothing happens. I have connected the webview's frameLoadDelegate to the App Delegate
When do you set the web view's frameLoadDelegate property to be your app delegate?

WebView did finish launching does not work

what is wrong with this method?
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
[activityIndicator stopAnimation:self];
}
I want to stop the Circular Progress Indicator (activityIndicator). But there is something wrong with - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame. I am coding for mac osx and not for iOS. I heard something from Delegates, what does that mean?
Check to be sure that your UIWebView has set its delegate. Setting a delegate is basically telling the program who you want to handle events (like taps, gestures, or, in this case, the loading of a webView). Thus when an event is fired, it will inform the delegate and the delegate can process it. Maybe if you post more of your code it would help, but I would check your declaration of the UIWebView in question. Be sure that after you allocate it and initialize it, you set its delegate to self (assuming that this method is in the same class), like so:
UIWebView *myWebView = [[UIWebView alloc] init];
[myWebView setDelegate:self];
If you have not set the delegate, it is firing off events and no one is receiving them to process them. The method you are using is waiting for the specific event sent by any webView. When it is sent an event message it passes, as a parameter, the webView that triggered. In any case, put in a log statement to be sure you are entering the method. That will tell you if it is receiving the event messages.
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
NSLog(#"Did finish loading...");
[activityIndicator stopAnimation:self];
}
NOTE: This is as per iOS experience, but should work for Mac OS as well. Let me know what your log result is, if the method is getting called or not.

How do I handle a mousedown event inside a window in Cocoa

How do I handle a mousedown event inside a window in Cocoa?
My code:
-(void)mouseDown:(NSEvent *)event {
NSLog(#"yay");
}
I am using Mac OS10.6, in xcode 4.0.1.
EDIT:
Yes, this is in the app delegate, but this is my .h:
#interface jumperAppDelegate : NSWindow {
Which I have done before in app delegates (just not for mouse events). This is really annoying me
Make sure you inherit from NSWindow, as well as conform to the <NSWindowDelegate> protocol. Like this:
#interface YourWindow : NSWindow <NSWindowDelegate> {}
#end
Then you should receive the event notification.
-(void)mouseDown:(NSEvent *)event {
}
For this method to be called the class it is being called in needs to inherit from NSResponder. Windows and views are all subclasses of NSResponder. If the class you are calling this from is not a subclass of NSResponder then the method will not fire.
* Update *
Also be sure to override acceptsFirstResponder to return yes.
- (BOOL)acceptsFirstResponder {
return YES;
}
I don't know for sure, but I have heard that in your header file (.h) that you need to replace the "NSObject" with "NSWindow". I would test it but I am not at my computer right now.
Also, make sure that you put the following code into your header file:
- (void) mouseDown:(NSEvent*)event;
EDIT: I have done some tests and research, but I cannot get it to work. I have two tips though.
Use the '-acceptsFirstMouse method.
Try creating an NSEvent:
NSEvent * someEvent;
-(void)mouseDown:(NSEvent*)someEvent;
This probably won't work, but I will have more information tomarrow

UITextView delegate not getting all method calls

I have a UIViewController that implements UITextViewDelegate and is connected as the delegate to my UITextView. Whenever the text view is tapped, I get a call to:
- (void)textFieldDidBeginEditing:(UITextField *)sender
and whenever the contents of the view change (keyboard, programmatic modification), I get calls to:
- (void)textViewDidChange:(UITextView *)textView
But when I hit the Return key on the keyboard, I am not getting a call to:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Is there something different about this method? Is there something special that needs to be set somewhere to make sure I get this call?
Thanks in advance!
Better to use keyboard notifications instead of using delagates. Register your viewController as observer for that notification. I am pretty sure that it sounds good.
Yep, different delegate.

Delegate methods of NSTextField using NSNotification

I have an NSTokenField in a window. I am using it to store tags related to a Core Data object. Right now I have it set up such that I can add tags to the objects, but I cannot delete them. I need a delegate method on the NSTokenField that can let me know when the user has moved the focus out of the NSTokenField. Since NSTokenField is a subclass of NSTextField I figured that I could use its delegate methods. It has two that I think could be useful:
- (void)textDidChange:(NSNotification *)aNotification
- (void)textDidEndEditing:(NSNotification *)aNotification
I set my controller class as the delegate of my NSTokenField and put both of these methods into my controller class. I put a basic NSLog into each of them and neither is triggered when I interact with the NSTokenField. I am guessing it has something to do with NSNotification. How do I activate these methods?
The NSTokenField invokes the controlTextDidChange: and the controlTextDidEndEditing: notifications; change the two methods above, implementing them as:
- (void)controlTextDidChange:(NSNotification*)aNotification
{
//Code here..
}
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
//Code here..
}