I use WKWebView to show a web page. When I touch a button on the page, some javascript code should work and show me an alert view. But it doesn't work. If I open the page on Safari, it works well. Here is my code
WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
configuration.preferences.javaScriptEnabled = YES;
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
self.view = _webView;
NSURLRequest *req = [NSURLRequest requestWithURL:[HPBNetworkBusiness getExamPageURL]];
[_webView loadRequest:req];
[_webView addObserver:self forKeyPath:#"URL" options:NSKeyValueObservingOptionNew context:nil];
WKWebView will not show JavaScript alerts automatically. It will be notified through
webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler: defined on WKUIDelegate. You can use the information passed in to show a native alert using UIAlertController.
Related
In my app the login page is in a WKWebView with an option to login using Google Auth:
https://developers.google.com/identity/sign-in/web/build-button
but the button doesn't respond.
Moreover, I've tried to switch the login URL with the URL above (google docs) because it containts an example for a Google login button and the button doesn't respond as well.
This is my WKWebView configuration with the google's doc link:
- (void)initWebView {
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:webView];
[webView setTranslatesAutoresizingMaskIntoConstraints:NO];
[[webView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:0] setActive:YES];
[[webView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:0] setActive:YES];
[[webView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:0] setActive:YES];
[[webView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:0] setActive:YES];
self.webView = webView;
webView.navigationDelegate = self;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://developers.google.com/identity/sign-in/web/build-button"]];
[request setValue:[self getUserAgent] forHTTPHeaderField:#"User-Agent"];
[self.webView loadRequest:request];
}
When I'm trying to click the button with the safary's web inspector open I don't see any respond as well.
Any help will be appreciated, thanks.
P.S.: Just to be clear none WKWebView delegate is being called.
Apparently, since 2016, Google no longer allows OAuth requests to Google in embedded browsers known as “web-views”, as mentioned here.
I have a master table and detail view controller. The detail VC shows URLs through a web view.
If using the built in navigation of the master/detail setup, if I go back and then press a different table cell, the detail view will update the web view. This is true whether I use a UIWebView or a WKWebView.
However, within the detail view, without having to go back to the table, I want to give the user the option to view more than one url through the same web view. I am trying to do this in viewwillappear by changing the url for the request. However, the UIWebView (or alternatively WKWebView) continue to show the same url. I can't seem to get rid of the first url.
I've tried all kinds of ways to close or stop the existing web view, clear the cache, delete cookies etc. before reloading the new one but still see the same url. Would appreciate any suggestions. Here is my code and some of the things I've tried that don't work:
-(void) changeURL: (NSNumber*)param {
NSURL *testurl1=[NSURL URLWithString:#"http://www.apple.com"];
NSURL *testurl2 =[NSURL URLWithString:#"http://www.google.com"];
//if param is 1 {
_urlToLoad = testurl1;}
else {
_urlToLoad = testurl2;}
}
Edit: Method below is updateInterface, not viewWillAppear
-(void) updateInterface {
UIWebView *webView;
NSURLRequest *request=[NSURLRequest requestWithURL:_urlToLoad];
webView = nil;//This blocks the web view from loading at all for some reason I can't understand. Setting wkWebView to nil does not block loading.
[webView stopLoading];//no effect
[webView loadRequest:request];
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSString *myDomain = #"www.google.com";
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:myDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
//wkwebview version
WKWebView *wkwebView;
WKWebViewConfiguration *wkConfig = [[WKWebViewConfiguration alloc] init];
wkwebView = nil;
wkwebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkConfig];
wkwebView.navigationDelegate = self;
[wkwebView loadRequest:request];
[webView addSubview:wkwebView];
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y = 100.0; // statusbar
webFrame.origin.y -= 100.0; // statusbar 20 px cut from y of frame
webView = [[UIWebView alloc] initWithFrame:webFrame];
}
The UIViewController method "viewWillAppear" has the following signature in Objective-C:
- (void)viewWillAppear:(BOOL)animated
, you implemented it without any arguments.
The result is that, instead of being taken as an override of the original method defined in the superclass UIViewController, it is treated as a brand new, custom method introduced by your subclass; and unless you call it explicitly somewhere, it will never execute.
Source: SDK Reference
Recently I just discovered a little issue.
I'm using UIWebview to load some url some times nothing really fancy, just basic init:
self.webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, navBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-navBar.frame.size.height)];
self.webview.opaque = NO;
self.webview.delegate = self;
self.webview.scalesPageToFit = YES;
[self.view addSubview:self.webview];
and i'm loading the url like this:
if(self.webview)
{
[self.webview loadRequest:[NSURLRequest requestWithURL:url]];
}
The problem that i got is when i try to redirect to tripadvisor mobile page I got a pop up saying that my version is not handled anymore and that i need to update it, the thing is that even with the last update it doesnt work...
And it's the same for ios6, 7 and 8 and only for IPHONE ...
Is there anybody who got the same problem ? and maybe fixed it ?
Thanks !
Here is the image of the popup:
I just solved by change UIWebView's user-agent
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:#"Safari iOS 8.0 - iPhone", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
put it before your webview initialize.
Hope this helps.
Want to how to call an external url in embedded cordova webview in iOS application. Can we escape from giving www folder and start page?
example url: http://www.stackoverflow.com
This is the snippet i tried, but since that checks for the www folder, this wont work.
CDVViewController *viewController = [CDVViewController new];
viewController.wwwFolderName = #"";
viewController.startPage = #"http://www.stackoverflow.com";
viewController.useSplashScreen = YES;
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
is there a way to call such external url from this end?
Thanks a bunch.
This will do the task :
[viewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.stackoverflow.com"]]];
I have an iOS app that needs OAuth authentication of a Facebook account, but I have to use my own server for the API calls.
So I want to open a FBLogin screen (UIWebView), with a login URL, when a certain button is clicked.
I have the following code:
FBWebViewController.h
#import <UIKit/UIKit.h>
#interface FBWebViewController : UIViewController <UIWebViewDelegate>
- (void)initFBWebView;
#end
FBWebViewController.m
- (void)initFBWebView {
NSLog(#"DetailViewController->initUIWebView {");
UIWebView *aWebView;
// init and create the UIWebView
aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//Set the URL to go to for your UIWebView
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the URL into the web view.
[aWebView loadRequest:requestObj];
//add the web view to the content view
[self.view addSubview:aWebView];
}
and in my Authorize.m file, which handles all the API calls etc.
- (void)fbLogin:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
FBWebViewController *webview = [[FBWebViewController alloc] init];
[webview view];
[webview initFBWebView];
}
Nothing happens. No WebView is opened. I put some debug statements in the initWithFbWebView method, and the method is called and run until the end, but no screen is showing up.
What am I doing wrong?