I've created a very simple Mac program to load a web page. It works and loads it well but I can't run events! Nothing is logged!
#import "BenotaAppDelegate.h"
#implementation BenotaAppDelegate
#synthesize webViewIns;
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURL *url = [NSURL URLWithString:#"http://example.com"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[[webViewIns mainFrame] loadRequest:req];
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
NSLog(#"didFinishLoadForFrame");
}
#end
I can not use delegate right....
You need to set outlet frameLoadDelegatefrom your webView object to a class, that contains a method webView:didFinishLoadForFrame:
Just a note that my (iOS) app was rejected due:
non-public API/s in your app:
webView:didFinishLoadForFrame
You might want to reconsider your app, I definitely must find the 3rd party lib, which called that, and get rid of it. "It wasn't me!" is not a valid excuse :)
Related
This is a Cocoa-component WebView. For example he called JS-function:
[webView stringByEvaluatingJavaScriptFromString:#"foo"];
I need to somehow wait until this function is executed and start doing another job. How this can be done on the Objective-C? I need something like:
[webView waitUntilJavaScriptCodeIsCompleted];
[webView stringByEvaluatingJavaScriptFromString:] is executed synchronously and will return an NSString of the result of executing the passed in script. So, if you just want to know that script has executed, call the function and when it returns, it has executed.
If, however, you're talking about a script with some sort of asynchronous execution, like an XMLHTTPRequest or setTimeout, then you're going to need to have your JavaScript call back into your Objective-C code to let it know when it's finished. Here is an example project that does just that. The most relevant parts are in AppDelegate:
#import "AppDelegate.h"
#import WebKit;
#import JavaScriptCore;
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#property (weak) IBOutlet WebView *webView;
#end
#interface AppDelegate (WebDelegate) <WebFrameLoadDelegate>
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
JSGlobalContextRef jsGlobalContext = _webView.mainFrame.globalContext;
// Add a function named "allDone" to the window object. It can be called from JavaScript like so:
JSContext* context = [JSContext contextWithJSGlobalContextRef:jsGlobalContext];
context[#"allDone"] = ^{
NSLog(#"All Done was called");
};
context[#"jsLog"] = ^(NSString* message) {
NSLog(#"JSLOG: %#", message);
};
_webView.frameLoadDelegate = self;
NSURL* url = [NSURL URLWithString:#"https://example.com"];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];
[_webView.mainFrame loadRequest:req];
}
#end
#implementation AppDelegate (WebDelegate)
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
NSLog(#"Page loaded, calling JavaScript");
// Script that logs a message, and then invokes allDone after 2 seconds.
NSString* script =
#"jsLog('Script running');"
#"setTimeout(function() {\n"
#"jsLog('javascript timer fired, invoking allDone...');\n"
#"window.allDone();\n"
#"}, 2000);";
NSLog(#"Before stringByEvaluatingJavaScriptFromString");
[_webView stringByEvaluatingJavaScriptFromString:script];
NSLog(#"After stringByEvaluatingJavaScriptFromString");
}
#end
Which results in the following output:
Page loaded, calling JavaScript
Before stringByEvaluatingJavaScriptFromString
JSLOG: Script running
After stringByEvaluatingJavaScriptFromString
JSLOG: javascript timer fired, invoking allDone...
All Done was called
I'm hoping someone can help as I'm new to iOS / objective C and very puzzled. I'm trying to play a simple sound using AVAudioPlayer as follows:
NSString *path = [[NSBundle mainBundle] pathForResource:#"soundFile" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
[self.player play];
I am using ARC so I also have in my .h file, the following reference to my player so that ARC does not deallocate my player prematurely:
#property (nonatomic, strong) AVAudioPlayer *player;
This code works just fine and plays my sound PROVIDED that I run this code from a ViewController or my application's AppDelegate.
However if I cut and paste this very same code, plus all the necessary #includes and the #property and add them into another class in the same application but which is not a ViewController, and call the code there then no error is raised but no sound is played.
It is exactly the same code just called on a different class??
Why would it not work?
I have looked and looked for a similar post but nowhere have I seem exactly this scenario addressed. Many thanks if you can help me- would be much appreciated.
To clarify the issue-- here is how I call this code on another class say a class I have named Audio Tester, I would write in AppDelate say
#import "AppDelegate.h"
#import "AudioTester.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
AudioTester * tester = [[AudioTester alloc]init];
[tester playAudio];
}
where AudioTester playAudio is defined as
#import "AudioTester.h"
#implementation AudioTester
-(void) playAudio {
NSString *path = [[NSBundle mainBundle] pathForResource:#"soundFile" ofType:#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
[self.player play];
}
#end
with AudioTester.h as follows
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface AudioTester : NSObject
#property (nonatomic, strong) AVAudioPlayer *player;
-(void) playAudio;
#end
Stepping through this code, it gets called just fine but it does not play sound?
If you can help that would be much appreciated. I'm totally stumped.
a little conceptual explanation about your code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
AudioTester * tester = [[AudioTester alloc]init];
[tester playAudio];
}
1.
if you use ARC then the instance won't be not kept alive after the scope runs out, therefore the tester object will be immediately released, so in your case the object is deallocated before it could do anything – that is the reason why you can't hear any noise or sound.
if you want to keep your tester instance alive independently from your current scope where you inited in, you need to create like e.g. a property which is outside of the scope; you could put that into a class extension for instance:
#interface AppDelegate ()
// ...
#property (nonatomic, strong, nullable) AVAudioPlayer * tester;
// ...
#end
2.
we don't put anything like this into the AppDelegate.m file, the app delegate class basically handles the app-related events globally like launch, terminate, etc... briefly, the global and major events of the app's life cycle in runtime.
you can read more about its purpose in the official docs.
3.
you may use the –applicationDidFinishLaunching: method deliberately for initing your app, but I feel necessary to mention you may want to put everything inside the method –application:didFinishLaunchingWithOptions: instead.
you can read more about the initial procedure as well in the same documentation.
TL;DR
the answer to your original concern: NO, a class can be inited and instantiated in any other instance of any type of classes in general, but you need to worry about keeping the object alive as long as you want to use it.
I have a string of HTML that was parsed by libxml2.dylib that looks like:
Hello,<br />\n<br />\nThis is almost HTML.<br />\n<br />\n
I've unsuccessfully tried to display certain strings parsed from the XML in a WebView; I'm hoping there's a simple way to do it such as how an HTML page is displayed in my Cocoa application:
HTMLView.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface htmlView : NSObject {
IBOutlet WebView * webview;
}
-(IBAction) showHTML:(id) sender;
#end
HTMLView.m
#import "HTMLView.h"
#implementation htmlView
-(IBAction) showHTML:(id) sender
{
[[webview mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://www.example.com"]]];
NSString * string = #"<br>test</br>";
[self loadHTMLString:string baseURL:(NSURL *)baseURL];
}
-(void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL {
}
#end
try this... [[aWebView mainFrame] loadHTMLString:aString baseURL:nil];
I've unsuccessfully tried to display certain strings parsed from the XML in a WebView
How did you try to display the strings and what was the problem? I think
-(void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
Might be what you need...!?
Edit:
You have to call the method on your webview!
What you did now is implementing your own loadHTMLString method in your viewController. Which would be fine if it did anything and did call loadHTMLString on the webview at some point.
[self.webView loadHTMLString....]
I think you have to familiarise yourself a bit more with objective-c.
Hello I am working on a web view based application, I have a WebController within that I have a variable which holds the current URL.
I have recently added code to launch a view, this works fine, this view also holds another web view, this web view works fine if I load a site into it such as this:
[[mywebview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
[#"http://www.google.com/search?hl=en&q=" stringByAppendingString:#"test"]]]];
However I am trying to access the variable which holds the current URL like this:
[[mywebview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
[#"http://www.google.com/search?hl=en&q=" stringByAppendingString:currentURL]]]];
However when doing this by linking to the WebController the currentURL seems to equal nothing, however this works perfectly fine when doing it from the first responder (except of course the view no longer shows)
My question is how can I get my currentURL variable working when linked from the WebController?
I am relatively new to cocoa so I am sorry if this is easy question!
EDIT: added from comments
In the method initWithWindowController currentURL is set to #"", and in dealloc to nil. The currentURL comes from the other web view see here:
- (void)webView:(WebView *)wv didStartProvisionalLoadForFrame:(WebFrame *)frame {
if (frame != [self.webView mainFrame])
return;
self.currentUrl = [[[[frame provisionalDataSource] request] URL] absoluteString];
[self retain];
}
I am declaring currentURL in the WebController.h
#interface WebController : NSObject <DOMEventListener>
{
IBOutlet NSString *currentURL;
}
#property (nonatomic, copy) IBOutlet NSString *currentURL;
#end
I am trying to use the currentURL in the WebController.m in the DisplayInView function.
-(IBAction) DisplayInView:(id) sender
{
if ([siteview isInFullScreenMode])
{
[siteview exitFullScreenModeWithOptions:nil];
}
else
{
[[mywebview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [#"google.com/search?hl=en&q="stringByAppendingString:currentURL]]]]; siteview enterFullScreenMode:[[siteview window] screen] withOptions:nil];
}
}
#synthesize siteview;
#end
I don't know if this is your problem, or a copy and paste error, but the code you posted is:
[#"google.com/search?hl=en&q="; stringByAppendingString:currentURL]]]];
It has a stray semicolon in the middle. That shouldn't even compile though.
I have been trying to add a link to Google Maps so that I can show some directions to a location. The only problem i'm having is the code not recognizing the method. Here's samples below, hope they help.
.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface More : UIViewController{
}
- (IBAction)directions:(id)sender;
-(CLLocationCoordinate2D)getCurrentLocation;
#end
.m:
- (IBAction)directions:(id)sender {
CLLocationCoordinate2D currentLocation = [self getCurrentLocation];
NSString* address = #"********";
NSString* url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",
currentLocation.latitude, currentLocation.longitude,
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];
}
Error:
Method definition for 'getCurrentLocation not found
Thanks
It looks like you haven't written getCurrentLocation. I'm not sure what that method is but google for CoreLocationControllerDelegate, that will show you how to get your location.
Is there a reason you don't want to use MKMapView? It really makes your life easy.