I'm catching an url in shouldStartLoadWithRequest.
How may I know if it's one of my project resources that is trying to be opened (for example with [self.webView loadHTMLString:htmlContentFinal baseURL:[NSURL fileURLWithPath:bundlePath]];), or something else ?
You can check if URL string starts with file://:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (request.URL.isFileURL) {
// do some stuff
}
}
How may I ensure that I am the one who loaded that file? The user could wrote that url in the url field himself.
You can implement -textFieldDidBeginEditing or -textFieldDidEndEditing and set some boolean properties to YES:
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([[textField.text substringToIndex:7] isEqualToString:#"file://"]) {
self.fileUrlEnteredManually = YES;
} else {
self.fileUrlEnteredManually = NO;
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (request.URL.isFileURL) {
if (self.fileUrlEnteredManually) {
// user entered "file://" manually
} else {
// user didn't
}
}
}
- (void)viewDidload {
// ...
fileUrlEnteredManually = NO;
[webView loadRequest:yourLocalRequest];
}
Check the [URL schema]. If it's file, then it's a local file. It won't be necessarily a bundle resource, though - could be a file in your app's Documents, for example.
To make sure it's in the bundle, match the beginning of the path with the bundle's path, available from the [NSBundle mainBundle]. Although I don't see why.
Related
I am trying to run the code below in webview's shouldStartLoadWithRequest delegate method but it doesn't make any changes.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(#"webView shouldStartLoadingWithRequest");
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"document.execCommand('bold', false,null)"]];
return TRUE;
}
There is no error, it prints the NSLog and everything in the method works great except "stringByEvaluatingJavaScriptFromString" method.
But if I try to make text bold in another function, for example an IBAction method, works fine.
-(IBAction)boldClick:(id)sender
{
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"document.execCommand('bold', false,null)"]];
}
Actually, this is my company's special application and this UIWebView will not show the web pages. I am using it to show some custom HTML pages.
I need to make everything in "shouldStartLoadWithRequest" because I am trying to run an objective-c method from javascript.
UPDATE
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
// Break apart request URL
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:#":"];
// Check for your protocol
if ([components count]==3)
{
[self makeBoldText];
return NO;
}
else
{
return TRUE;
}
}
-(void)makeBoldText
{
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"document.execCommand('bold', false,null)"]];
}
The doc says that method webView:shouldStartLoadWithRequest: is sent before a web view begins loading a frame. After you return YES in this method, web view starts loading a request. So any javascript you execute will have no effect, because a new page will be loaded after your JS call.
You can probably use webViewDidFinishLoad: method to execute javascript after page finishes to load. Or if you want to trigger JS by clicking on a link, you can use shouldStartLoadWithRequest but return NO from it.
I'm listening for url loads from my UIWebView. Is there a way to check if the request has a target of "_blank"?:
- (BOOL) webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([request targetIs:#"_blank"]) {
// do something.
}
}
Don't know about iOS, but as for OS X, it works perfectly, if you use policy delegate. I've placed my answer in this thread, mentioned by Russ above.
Maybe you can use [UIWebView -request] to get the NSURLRequest, and use [NSURLRequest URL] to check if the url contains anything you want.
Short question: Is it possible to detect window.open() in a UIWebView using the UIWebViewDelegate or is there another way to reach this? I need the the url when a window.open()-Event is fired to show a UIAlertView.
You need to overwrite window.open() using JavaScript:
[webView stringByEvaluatingJavaScriptFromString:#"window.open = function (open) { return function (url, name, features) { window.location.href = url; return window; }; } (window.open);"];
Try using this delegate methods. Hope this helps.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)webViewDidStartLoad:(UIWebView *)webView;
This seems like a strange one. Not even sure if it is possible!!
I have a UIWebView that loads a local html page. On this html page I have a button.
I want to click on the button and then call an IBAction in Xcode.
How would I go about this? Or can you even do this???
Thanks guys,
Stefan.
You can do it by using a custom protocol. In you html file, you can link to something like myProtocol://callSomeAction.
Then on your UIWebViewDelegate (probably your UIViewController) you have to implement the method called:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
(Docs here)
The idea is that on that code, you detect the protocol based on the data in the request parameter. If it is myProtocol, you can call your IBAction and return NO. If it's something else, you fallback to have the web view load the page, and just return YES.
The code would look something like this:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString* scheme = [[request URL] scheme];
if ([#"myProtocol" isEqual:scheme]) {
// Call your method
return NO;
} else {
return YES;
}
}
Have the button on the HTML page open a custom url such as myapp://buttonclick.
In your web view delegate, implement webView:shouldStartLoadWithRequest:navigationType:. Check whether the request includes your custom URL and if it does, call any Obj-C method you want.
I have similar kind of situation, but its an imageclick , href and handling this method of webview,
- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener{
NSString *host = [[request URL] host];
//if (host!=nil)
{
WebNavigationType eActionType = (WebNavigationType)[[actionInformation valueForKey:WebActionNavigationTypeKey] intValue];
NSURL *pOrignalURL;
if(eActionType == WebNavigationTypeLinkClicked)// == [actionInformation valueForKey:WebActionNavigationTypeKey])
{
/* we will handle it */
pOrignalURL = [actionInformation valueForKey:WebActionOriginalURLKey];
NSString *pElementName = [actionInformation valueForKey:WebActionElementKey];
if([[pOrignalURL absoluteString] hasPrefix:#"app:"]){
[listener ignore];
return;
}
}
//[[NSWorkspace sharedWorkspace] openURL:pOrignalURL];
NSArray* urls = [ NSArray arrayWithObject:
[ NSURL URLWithString:[pOrignalURL absoluteString]]];
[[ NSWorkspace sharedWorkspace ]
openURLs:urls
withAppBundleIdentifier:nil
/* use default system bindings */
options:NSWorkspaceLaunchWithoutActivation
additionalEventParamDescriptor:nil
launchIdentifiers:nil ];
/* default behavior */
[listener download];
return;
}
}
I want to disable hyperlinks in UIWebVIew after the initial page loaded without disabling the scrolling feature. That is, I should have user interaction enabled.
You can work with webView shouldStartLoadWithRequest like this:
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *loadURL = [[request URL]retain];
//change next line to whatever condition you need, e.g.
//[[loadURL relativeString] ....] contains a certain substring
//or starts with certain letter or ...
if([[loadURL scheme] isEqualToString: #"file"])
{
[loadURL release];
return TRUE;
}
[loadURL release];
return FALSE;
}
You also have to set the webViews delegate an object of class where this method is implemented in: [webView setDelegate:my...];
By the implementation above, no url is loaded except those for which the condition is true. At least for the url of the first site it has to be true. The code above works for a web view initially loaded with contents of a file, containing only links to 'http://' or 'https://' or ...