I can't find any documentation to confirm this, but it appears that you can only call the method stringByEvaluatingJavaScriptFromString in overridden methods from a UIWebView delegate. Can anyone confirm this?
Here's what I've tried. I setup a button on a view, link it to a method on my viewcontroller, and make sure it works fine. My view has a UIWebView control on it as well. If I run the project on the simulator or on the iPhone, there are no issues. Then I add this code to the button's method.
[theWebView stringByEvaluatingJavaScriptFromString:#"alert('Hi there!');"];
When I run the project, I can click the button and see the 'Hi there' prompt and I can click OK to dismiss it. Usually 4-5 seconds later the simulator crashes. I occasionally see the "__TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__" error, but not consistently; sometimes there's no error. It also doesn't always crash the first time. Sometimes I go to another page, and then try it again, and it crashes.
If I put the same code in the webPageDidFinishLoad event it works fine. But I'd like the code to be called when the user demands it so that event doesn't suit my needs.
I'm open to a workaround if you have any ideas? Thanks in advance!
I still don't know the exact reason this didn't work, but I found I could rewrite my code to get called during the UIWebView delegate methods instead.
Related
My app uses multiple features for Apple's demo project AVCam. Everything works just fine except when I exit the app and go back in it, it doesn't show the camera preview anymore. Does anyone know what piece of code I am supposed to use and where it belongs? I tried searching but a lot of questions relating to android popped up.
You need to reinitialize your camera once the App becomes active again. In your app delegate methods, override, applicationDidBecomeActive and send a notification so your view controller knows that your app became active again.
Pending the notification received, you can reload the viewDidLoad, or move the contents of viewDidLoad to viewDidAppear. There's multiple ways to do this. You can also reload the contents of viewDidLoad in viewWillAppear. There's many many many ways to do this, like I said.
In my project I show an Alert to the user to indicate an 'empty list'.
Right now, I show it in AppDelegate>applicationDidBecomeActive.
I'm doing this because I want the alert to show if the list is empty
at app startup and when coming out of the background (iOS 4.2 through 5.x).
EDIT:
I use a method in the AppDelegate, and call it with a slight delay, and I still get this notice.
[self performSelector:#selector(checkForNoMessages) withObject:nil afterDelay:1.0];
However, this causes a "wait_fences" notice in the debugger and I'd prefer not to submit to Apple with this notice.
Where is the proper place to put a popup Alert so that it appears:
1) At App startup
AND
2) When the App is coming out of the background?
Do I need the Alert in more than one place?
I recommend writing a method in your AppDelegate or better in your root view controller which shows the message. Maybe with some arguments, so you can reuse it but that's up to you.
If you are following the MVC architecture ask your model about existing entries and trigger the Alert message if necessary. But encapsulate this behavior in a controller as well.
application:didFinishLaunchingWithOptions: and applicationDidBecomeActive: are the places where you want to delegate this task to your controller.
More about iOS Multitasking: https://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Edit:
Don't forget that you have to call the methods from the main thread.
And do all startup stuff first.
OK - the problem wasn't where I called the alert, it was because it was in a method. Once I moved the code from a method into applicationDidBecomeActive, all is well.
EDIT: Problem Solved!
For others, To fix it, simply right click the WebView, click the "delegate" circle, and drag it to "File's Owner" on the left side.
Thanks for the help
--I'm fairly new to app developing, I'm in my first semester actually. Since it's a brand new course, the instructor knows very little as well. Therefore I have complete permission to ask for help on this. Thanks!
On to my question...
I want to include an activity indicator in my app as my WebView starts and stops loading, simply enough. At this point, it will animate. However, my problem is that it starts before the WebView starts loading anything, and also doesn't ever stop. My current code is this:
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activity startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activity stopAnimating];
Other than that, I have it set up in the header...
IBOutlet UIActivityIndicatorView *activity;
And as far as I can tell, set up rather properly. Oh, also, I do have "Animated" checked and "Hide When Stopped" checked in the .xib file.
UPDATE: NSLogs and breakpoints show that the methods aren't being called.
I am a highly visual learner, so, I will need to see what I am missing, unless it is a problem in the interface. Thanks...
Any help is much appreciated
Do you have your webView correctly connected to the delegate? Toss in an NSLog or a couple breakpoints and make sure that your functions are being called.
You can put a break point in your code to see if the webViewDidFinishLoading is called. I'm guessing it is not being called. There is another delegate methods you should implement (see UIWebViewDelegate Protocol Reference), called webView:didFailLoadWithError:. Implement that and put a break point inside it. Let us know what happens.
Did you see the apple developer examples?
Apple Developer Example Webview
I am using a UIWebView in my app and I am overriding the shouldStartLoadWithRequest message to detect what kind of link is being clicked. If it's a "special" link, I push a UIViewController onto the stack and return NO from this method. This works just dandy most of the time.
Sometimes, however, I click on a link and my shouldStartLoadWithRequest never gets called. Now what's weird is that the UIViewController which houses the UIWebView is in a UITabBarController and when I click on another tab, the UIWebView finally gets its shouldStartLoadWithRequest called. Until I click this other tab, I do NOT get a call to shouldStartLoadWithRequest. The other interesting bit is that the failure case never happens the first time I click on a link; it's always on a subsequent time.
Has anyone seen this? To me, this sounds like the UIWebView is, sometimes, not getting a touchEnded event and by switching tabs, the underlying framework is forcing a touchEnded event which in turn causes my shouldStartLoadWithRequest to get called.
Are you implementing the
– webView:didFailLoadWithError:
method from the UIWebViewDelegate? Perhaps you're getting an error on the request and then you're never seeing it? This delegate should get thrown when you have issue such as timeouts and other things.
Hi I regulary created using IB one UISegmentedControl that I called showAllSwitch.
If I try to do [showAllSwitch setHidden:YES]; nothing happens!
Why? How can I do to hide it?
Where are you calling setHidden? if it is being called before it is added to the view, then there could be problems. Try calling it in viewDidAppear and see it that works. If it doesn't either the segmentedControl is not connected correctly in IB, or there is a rather big problem that we are missing.
Hiding an object is relatively straight forward. So if there is a problem then its something simple. You need to just go through some basic diagnostic steps:
Verify that your segmented control is actually connected to the correct outlet in IB. Really. Go look. Even if you are sure. Go look again.
Verify that the line where you are hiding it is being called. Add an NSLog just after and see if it shows up when it should.
Make sure that the hide command is not getting sent too soon. If its being sent in ViewDidLoad try setting it up in ViewDidAppear.