In my webView's delegate method I am checking for the condition when webview loading fully completes by using this code:
if ([[webView stringByEvaluatingJavaScriptFromString:#"document.readyState"] isEqualToString:#"complete"]) {
//Code to handle other things
//Sometimes after this block shouldStartLoadWithRequest is called
}
Sometimes even control goes to if block even then -shouldStartLoadWithRequest this method is being called and my code breaks.
Is their any other alternative? I also tried webview.isLoading but this also doesn't work.
You could use javascript and the userContentController as a way for your page to message back to your native code that it is loaded and ready. When you setup your WkWebView, setup the handler like this:
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *controller = [[WKUserContentController alloc] init];
[controller addScriptMessageHandler:self name:#"observe"];
theConfiguration.userContentController = controller;
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect frame = self.view.frame;
frame.origin.y = statusBarFrame.size.height;
frame.size.height -= statusBarFrame.size.height;
self.primaryWebView = [[WKWebView alloc] initWithFrame:frame configuration:theConfiguration];
Then add a method to your ViewController called userContentController, like this:
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Check the details in message.body for what your javascript said
NSLog(#"javascript message: %#", message.body);
});
}
I have found one workaround to stop any further execution of scripts after didFinishLoading.
I used following condition to stop reqeust:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
//my code
}
}
Related
I am implementing the web view through react-native. Therefore, I use the react-native-webview library. However, "window.open" and "window.close" are not implemented in the react-native-webview.
I need to apply that part of the code for social login. So I found the swift code document. However, I don't know how to change this document to an objective-c code.
object-c partial code of react-native-webview
swift document
// webView list management
var webViews = [WKWebView]()
...
func webView(_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures
) -> WKWebView? {
guard let frame = self.webViews.last?.frame else {
return nil
}
//Creating and returning a web view creates a parent relationship with the current web view.
return createWebView(frame: frame, configuration: configuration)
}
/// ---------- popup close ----------
func webViewDidClose(_ webView: WKWebView) {
destroyCurrentWebView()
}
// Examples of Web View Generation Methods
func createWebView(frame: CGRect, configuration: WKWebViewConfiguration) -> WKWebView {
let webView = WKWebView(frame: frame, configuration: configuration)
// set delegate
webView.uiDelegate = self
webView.navigationDelegate = self
// add view
self.view.addSubview(webView)
self.webViews.append(webView)
return webView
}
// Examples of webview deletion methods
func destroyCurrentWebView() {
// remove from webview lists and screens
self.webViews.popLast()?.removeFromSuperview()
}
How can I apply this code to suit the react-native-webview?
EDIT
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
WKWebView *popUpWebView = [[WKWebView alloc] initWithFrame: navigationAction.targetFrame configuration: configuration];
popUpWebView.uiDelegate = self;
popUpWebView.navigationDelegate = self;
[_webView addSubview:popUpWebView];
return nil;
}
- (void)webViewDidClose:(WKWebView *)webView {
[_webView removeFromSuperview];
}
I looked at the document and changed it as follows. However, an error occurs when building. I don't know what the problem is.
As I mentioned in my comments, I am not sure if this will work for React Native but this Obj-C code is the same as your swift code and should compile
In your .h file
Your .h file will probably need to be the same as RNCWebView.h and you might need to remove anything unwanted / unused
In your .m file
Similarly, your .m will be similar to RNCWebView.m and remove what you don't use.
Then as per your swift code, these are the updated Obj C versions of those functions
- (WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
forNavigationAction:(WKNavigationAction *)navigationAction
windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
if ([webViews count] == 0) {
return nil;
}
WKWebView *currentWebView = [webViews lastObject];
WKWebView *popUpWebView = [[WKWebView alloc] initWithFrame: currentWebView.frame
configuration: configuration];
popUpWebView.UIDelegate = self;
popUpWebView.navigationDelegate = self;
[webView addSubview:popUpWebView];
return popUpWebView;
}
- (void)webViewDidClose:(WKWebView *)webView
{
[webView removeFromSuperview];
}
Update
If the webViews variable from the original swift code is unused / not needed, you probably need to update the webView createWebViewWithConfiguration as follows:
- (WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
forNavigationAction:(WKNavigationAction *)navigationAction
windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
WKWebView *popUpWebView = [[WKWebView alloc] initWithFrame: webView.bounds
configuration: configuration];
popUpWebView.UIDelegate = self;
popUpWebView.navigationDelegate = self;
[webView addSubview:popUpWebView];
return popUpWebView;
}
Finally, just to clarify:
The header does not need to be the same as mine, I just gave you an example if you were subclassing a UIViewController. You probably need to follow the header and implementation file defined here
My goal was to convert your swift code into Obj C code that would compile, I cannot say if it is right for React Native however.
I want to load a URL on a UIViewController with an UIWebView and UIActivityIndicatorView, but UIActivityIndicator never appears and UIWebView never loads the URL.
This is my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = #"Web";
[self displayURL:[NSURL URLWithString:#"(Website)"]];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[self.loadView stopAnimating];
self.loadView.hidden = YES;
}
-(void) displayURL:(NSURL *) aURL {
self.web.delegate = self;
self.loadView.hidden = NO;
[self.loadView startAnimating];
[self.web loadRequest:[NSURLRequest requestWithURL:aURL]];
}
If you add your webView by code, you need add
[self.view addSubview: web];
after you created webView, and of course you need to add UIActivityIndicatorView as a subView of webView or superView, so you need to add [web addSubview: loadView] or [self.view insertSubview: loadView aboveSubview: web];.
And about the URL, if your website URL is like www.google.com, you need to change #"(Website)" to the real URL. If your website is a file copied in your project, you need to change [NSURL URLWithString: ]; to [NSURL fileURLWithPath:[NSBundle mainBundle][pathForResource: ofType:]];.
Really poorly formulated question.
UIWebViewController does not work
UIWebViewController does not exists, you are talking about a UIWebView in a UIViewController
Code is very incomplete
I have a standalone UIViewController with a UIWebView. I need to check if a certain URL has been hit, and if so, present a new UIViewController inside of a UINavigationController.
Here is my code:
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
//Check if special link
if ( [ urlString isEqualToString: #"http://www.mysite.com/store" ] ) {
//Here present the new view controller
StoreViewController *controller = [[StoreViewController alloc] init];
[self presentViewController:controller animated:YES completion: nil];
return NO;
}
return YES;
}
However, when I run the app, I get the following error:
WebKit threw an uncaught exception in the method
webView:decidePolicyForNavigationAction:request:frame:decisionListener:
delegate:
<'NSInvalidArgumentException'> * -[__NSArrayM insertObject:atIndex:]:
object cannot be nil
If I try and open the UINavigationController itself it works fine...but then how do I pass data to the StoreViewController itself?
Let's try these solutions:
Destroy webview's delegate in your view controller:
-(void)dealloc{ self.yourWebView.delegate = nil}
Stop request from webView:
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
StoreViewController *controller = [[StoreViewController alloc] init];
[self presentViewController:controller animated:YES completion: nil];
//stop loading here
[webview stopLoading];
return NO;
}
Double check in new view controller, do you have any array which it's element is assigned to nil
Hope this helps you
Lets go through the problems one by one:
WebKit Exception: You can resolve this by stopping the webview loading. Just call [webView stopLoading];
Pass data: If you declare your required data in the StoreViewController.h you can easily set them when you are initializing the view controller.
StoreViewController *controller = [[StoreViewController alloc] init];
controller.myStringData = #"Required Data";
controller.myDataDictionary = someDictionary;
You will be able to get the data in the viewDidLoad method of StoreViewController
Hope this helps.
I am a real noob in ios dev. Now i am working on my study project using Zxing.
I make my own project which included dependency third party libraries(Zxing).
Once I scan QRCode which contains with a URL inside, my project will call a class in Zxing library then alert an alertView.
After that, once I click open button on pop-up alertView, it would open that URL by activating Safari.
the code in Zxing looks like this:
//============================================================
- (void)openURL {
[[UIApplication sharedApplication] openURL:self.URL]; //<===== open URL by Safari browser.
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex]) {
// perform the action
[self openURL];
}
}
//========================================================
However, my wish is I would like to open that URL in my own webView which constructed by Interface Builder in my "own project" NOT by Safari. Do you have any suggestions? What I have to code in (void)openURL {} ? I got stuck with this issue for 3 days and I now seem to be crazy [p]
Thank you very much for your advance help.
Cheers,
What i did to do this is that I make a new view controller which has webView in it. A property URLstring which get set when the OpenURL method starts. You have to implement delegate methods of webView if you need. The code that I am using is
in WebViewCOntroller.h
#interface WebViewController : UIViewController <UIWebViewDelegate>{
IBOutlet UIWebView *webView;
UIView *activityView;
}
#property (nonatomic, retain) NSString *buyProductLink;
#end
in WebViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
webView.scalesPageToFit = YES;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.buyProductLink]];
[webView loadRequest:request];
[request release];
}
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[activityView removeFromSuperview];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView {
[activityView removeFromSuperview];
[activityView release]; activityView = nil;
}
In openURL method use
WebViewController *webViewVC = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
webViewVC.buyProductLink = [NSString stringWithFormat:#"%#",result];
[self.navigationController pushViewController:webViewVC animated:YES];
I've using shouldStartLoadWithRequest very successfully in one of my programs, but the whole project was a proof of concept and scruffy and I'm starting afresh with a new project.
However shouldStartLoadWithReqest is no longer being invoked for me but I can't see where the important difference between the two projects is (however one difference is the first is using .nibs, in the 2nd I'm not using them).
To get things started I'm using a controller with the UIWebView as its view:
#interface IMSRootController : UIViewController <UIWebViewDelegate> {
UIWebView* webView;
}
(webView is declared as a #property and #synthesized)
- (void)loadView {
[super loadView];
webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = webView;
[webView release]; }
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:YES animated:NO];
[self displayPage]; }
-(void) displayPage { ... [webView loadHTMLString:self.htmlString baseURL:baseURL]; }
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
...
What's wrong?
Thanks
Your object is not being set as a delegate of the UIWebView object, hence you will not receive any delegate messages. At some point, either in loadView or even displayPage (but before the call to loadHTMLString:baseURL:), do:
webView.delegate = self;