UIWebView webViewDidStartLoad not called - objective-c

I post this message because i haven't found any answer.
I send a request to my UIWebView, webViewDidStartLoad is not called. I need to use the method reload, and then it will work. By the way the shouldStartLoadWithRequest is called then twice.
If i'm using a test url (like say Google), i will not get any problem. So that it not seems to be from my code.
But here, I'm calling a ugly site who use lot of angular JS (and i don't know how it has been made). Strange things is that it's working fine on Safari.
What i don't understand is that it just get "stuck" in shouldStartLoadWithRequest. But after no callback. Nothing happen, even (didFailLoadWithError:).
Any idea?
*** EDIT *****
Ok i found the problem, but that's still a strange related issue. I've found the solution trying to send you the code, and i couldn't believe that the code was actually working.
the fact is that i was calling http request while my web view was hidden, and i don't know why, the second call is never working. You cannot reproduce the "bug", since the website is sandboxed, and i've tested on Google, and that works.
You need to know that the site i have tested on use a lot of angular JS based on GUI state. If someone still can explain me how can a uiwebview can be stuck from server side (webViewDidStartLoad never called), i'll be happy. So the bug is 100% server related.
Here the code:
#interface ViewController ()<UIWebViewDelegate>
#property(nonatomic, retain)UIWebView* webview;
#end
#implementation ViewController
#synthesize webview = _webview;
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType{
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
// With that ugly site it will be never called if _webview.hidden is set to true.
}
#pragma mark - lifeCycle
- (void)viewDidLoad{
[self setUpWebView];
// first time, it will work.
[self performSelector: #selector(loadUrlString:) withObject: #"http://theUglySite.com" afterDelay: 1];
// now, if the uiwebview is hidden, no response.
[self performSelector: #selector(loadUrlString:) withObject: #"http://theUglySite.com" afterDelay: 3];
[super viewDidLoad];
}
#pragma mark - logic
- (void)setUpWebView{
self.webview = [[[UIWebView alloc] initWithFrame:(CGRect){0,0,320,480}] autorelease];
_webview.delegate = self;
// hidding the webView will break the delegate callBack. That will still work on normal
// urlRequest.
_webview.hidden = YES;
[self.view addSubview: _webview];
}
- (void)loadUrlString:(NSString*)urlString{
NSURL* url = [NSURL URLWithString: urlString];
NSURLRequest* request = [NSURLRequest requestWithURL: url];
[_webview loadRequest: request];
}
#end

apparently, the webview in ios checks specifically for "iframe" tags in the html's dom and in case it finds, it creates events based on that tag as well. therefore, a simple however a patchy solution would be adding the following code in the part of the dom that is changing (for example, in the actual template of each section):
<iframe></iframe>
did the trick for me.. i'm also using angularjs version 1.3.16

Related

Custom BackButton in iOS webview

I want to create custom back button on each page in my iOS web-view (objective-C). Can anyone please suggest me how to implement.
Thanks in Advance
I am new to iOS Web-view that is why I posted directly what I want this is my code :
#import "ViewController.h"
#interface ViewController () <UIWebViewDelegate>
#end
#implementation ViewController{
__weak IBOutlet UIImageView *logoImage;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.delegate = self;
NSString *urlString = #"https://www.anything.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
self.webView.hidden = YES;
if ([_webView canGoBack]) {
[_webView goBack];
}}
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.webView.hidden = NO;
logoImage.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"%#", error);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am passing a url of my website and I want to show a back button on every page of my application so that user can go to previous page. I have one more issue that whenever I minimise my application and opens it back from recent apps it get restarted while I want it to be resumed from the same screen
For the UIWebView, check the method - (void)goBack; out.
For the WKWebView, check the method - (WKNavigation *)goBack; and - (WKNavigation *)goBack:(id)sender; out.
UPDATE
// init the back button
[btnBack addTarget:self action:#selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- (void)backButtonTapped:(id)sender
{
[self.webview goBack];
}
Assume you have set up your webview and back button (in the navigation bar or somewhere else) in the full code.
The browser could go back when the current (tab) page has a history list and the current page url is not the first and last one.
Make sure you really want to goBack in viewDidLoad, it needs some time to finishing loading the target page you specified (https://www.anything.com) just like you do it in a browser.
The method loadRequest of webview is asynchronous, it pushes the url request to webview and returns it immediately.
Bind the webview's goBack method to your custom back button like the above code snippets.
Try to click a sample link in the initial page (https://www.anything.com), once the new page finishes loading, it could be able to goBack with satisfying [self.webview canGoBack].
Make a breakpoint in the implementation of method backButtonTapped, if needed.
Drop the logoImage or other unrelated code issues in your next question, that makes your question more clear and helpful.

Objective-C: Grab title of web page from WebView

I'm trying to retrieve the title of a web page and display it in the NSWindows title. This application is document based and I haven't tried anything in a standalone application with an AppDelegate. How would I go forth and retrieve the title and display it in the window?
UPDATE: Here's my code (Note: Doesn't work quite yet)
TitleWindow.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface TitleWindow : NSWindow <NSApplicationDelegate> {
}
#property
(retain, nonatomic) IBOutlet NSWindow *displayTitle;
#end
TitleWindow.m
#import "TitleWindow.h"
#import <WebKit/WebKit.h>
#implementation TitleWindow
- (void)displayTitle:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
{
if (frame == [sender mainFrame]){
[[sender window] setTitle:title];
}
}
#end
To get an NSWebViews page title.
You use it's mainFrameTitle
NSString * pageTitleString =[_webView mainFrameTitle];
NSLog(#"%#" , pageTitleString);
Read the Docs
example 2.
WebFrameLoadDelegate_Protocol
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)webFrame{
[_window setTitle:[_webView mainFrameTitle]];
}
UPDATE 2
I have not really built a document based app before. So setting one up to display a web view was fun :-P.
Which leads me to think the problem is two fold.
The first thing I released is just adding the example 2 code to the document.m is not enough.
The webview needs a delegate.
What worked for me and may not be (probably not) the right way to do it was in the Document.xib with the webview selected.
The Connections Inspector at the top shows you the frameLoadDelegate.
I connected this to the file's Owner by dragging it over to the file's Owner in the Place holders pane.
This worked.
And each time I changed the webpage from within the document the title changed along with it.
The second thing to note is I think the real method of setting the documents title is:
[self setDisplayName:#"a title"];
which works when set in:
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
NSURLRequest* request = [NSURLRequest requestWithURL:_theUrls];
[[_webView mainFrame] loadRequest:request];
[self setDisplayName:#"a title"];
}
And a normal string, But if I change it to:
[self setDisplayName:[_webView mainFrameTitle]];
It does not work as the frame is not loaded yet so there is no title.
So you would think then that [self setDisplayName:[_webView mainFrameTitle]]; would work in the WebFrameLoadDelegate. No it does not. And I am not sure why.
So for now you may want to use the code in example 2 code.

WebView in Cocoa not rendering

In my window controller, I have this:
- (void)windowDidLoad {
[super windowDidLoad];
NSLog(#"webView = %#", webView);
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://www.apple.com/"]]];
}
My understanding is that should make the webView in my window now download and render Apple's home page. But all I see in a white panel. What am I missing?
(I've checked that webView is not null; NSLog shows something like
"webView = <WebView: 0x10be7c3b0>")
I've also tried the more modern approach, but still had no success:
[webView setMainFrameURL:#"http://www.apple.com/"];
As this project is sandboxed, the solution in this case was to add the relevant entitlement to the project.

MobFox iOS Framework, Crashes When XIB unloaded... what am I doing wrong here?

A quick rundown of whats happening here.
I've got a GameScene and a GameOverUIController. When Game ends, It loads the GameOverUIController, which loads the xib, which loads the MobFoxBannerView.
Here's the issue: If you push the "play again" button to fast or you play a round longer than about 3 minutes, the ad loads in the background but essentially has no where to go, and crashes.
I took a friends advice of adding the ViewDidUnload to set the bannerview to nil, but it still crashes... I'm not the most amazing programmer admitted, but I feel like I'm making the dumbest error somewhere... Any help is greatly appreciated. Am I just using the wrong syntax on something here?
I've done the following things.
I synthesize bannerView on the GameOverUI Implementation.
Manually adding it to the view now, before I had added a UIwebview directly to xib Interface builder for GameOverUIController
self.bannerView = [[[MobFoxBannerView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)] autorelease];
bannerView.delegate = self;
[self.view addSubview:bannerView];
[self.view bringSubviewToFront:bannerView];
And thats in my main implementation. So everytime it loads the GameOver Screen it requests a new add.
Then I set the values in the header file.
-(NSString *) publisherIdForMobFoxBannerView:(MobFoxBannerView *)banner;
#property (nonatomic, retain) MobFoxBannerView *bannerView;
and set my publisher ID in the the bottom of GameOverUIController.m
- (NSString *)publisherIdForMobFoxBannerView:(MobFoxBannerView *)banner
{
return #"xxxxxxxxxxxx";
}
Then... STILL get a crash so then I tried to set it to nil hoping that would work... but I'm not sure about the syntax...
On GameOverUIController.m
- (void)viewDidUnload
{
[super viewDidUnload];
self.bannerView = nil;
// or ? bannerView.delegate = nil;
}
Niether of those lines works..... I'm at a total loss. Going to move onto another ads platform unless I can figure this out before the weekend is over. Bane of my existence right now. Any help would be greatly appreciated. Thank you very much
Edit:
Figured it out...
Had nothing to do with ViewUnload....
- (void)dealloc
{
bannerView.delegate = nil;
[super dealloc];
}
Was adding it to the wrong location... Works perfectly now.
I guess your problem is in
bannerView.delegate = self;
when add is loaded (because of bad internet it can be very slow for instance) - delegate method is being called, but "self" is released - so runtime exception is thrown.
By the way, I don't know a thing about MobFox framework. It's just a common anti-pattern.

MPMoviePlayerViewController Black Screen issue!

I have this code trying to run a video on the iPhone 4 Simulator.
The problem is that looks like it loads the player but half a second later loads a back screen on top of the whole application disabling touch and everything, and it looks like it does not play the video also, because I can't hear anything.
Any ideas?!
MPMoviePlayerViewController *mp =
[[MPMoviePlayerViewController alloc] initWithContentURL:videoUrl];
if (mp) {
mp.moviePlayer.scalingMode = MPMovieScalingModeFill;
mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[mp.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated:mp];
[mp release];
}
I believe the problem is caused by releasing the MPMoviePlayerViewController. Simply retain the controller until you are done with it.
Prior to the "[mp release];" add this line to save the value away.
self.moviePlayerViewController = mp;
Then update your dealloc method to do the release:
- (void)dealloc {
[_moviePlayerViewController release], _moviePlayerViewController = nil;
[super dealloc];
}
Add the synthesize to the top of your .m file:
#synthesize moviePlayerViewController = _moviePlayerViewController;
Add the defination to the #interface of your .h file:
MPMovieViewController *_moviePlayerViewController;
Add the property to your .h file:
#property (readwrite, retain) MPMovieViewController *moviePlayerViewController;
You may need some headers in your header:
#import <MediaPlayer/MediaPlayer.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>
You may also need to balance your "presentMoviePlayer" call with the dismiss somewhere:
[self dismissMoviePlayerViewControllerAnimated];
Phew, code everywhere. Anyway, if you are finished with the resource early, you may be able to release it sooner by using NotificationManager to watch for MPMoviePlayerPlaybackDidFinishNotification. There are many examples of that, so I won't repeat it.
Hope this helps.
This is the code I'm using:
MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:contentUrl]];
movieViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:movieViewController];
[movieViewController release];
It seems to work fine for me. Two notes:
Some simulators (like the current iOS 5.0) crash when playing a movie, but it works on a real device
if you leave out the movieSourceType part, a black screen is shown for about a second before the movie starts