Detecting UIImagePicker opened or not from a web view - ios

In my project I am having a web view. The page loaded in the web view contains an file (Image) uploader. On clicking the choose image button on the file uploader, then the phone's image picker opens. I am not authorised to make changes in the web page, because it is client organisations web page. Is there any way to detect the opening of UIImagePicker in the app.
<input id=“fileUpload" name=“fileUpload" type="file" data-bind="value: UploadedFile">
This is the html used for the file picker. On clicking the file picker the UIImage Picker View pops up. I want to detect it.

In the web page, for ‘Choose Image’ Button write a Java Script function . iOS supports custom URL schemes provided with syntax below
scheme://host/path?query
Example Java script function in your .html :
<p class="btn-upload" onclick="onUploadButtonClick()" >
<span> Upload File </span>
</p>
<script>
function onUploadButtonClick() {
window.location = "ios://button-upload”;
}
</script>
In your class .m file, inside Webview delegate method webView:shouldStartLoadWithRequest:request:navigationType compare the host. if it matches invoke your objective c function to open UIImagePicker in the app.
#pragma mark - UIWebView Delegates
- (void) webViewDidFinishLoad:(UIWebView *)webView {
//Do after web view load.
}
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType;
{
// get the action from the path
NSString *actionType = request.URL.host;
if ([actionType isEqualToString:#“button-upload”]) {
// do something in response to your javascript action
// if you used an action parameters dict, deserialize and inspect it here
[self openImagePicker]; //Cal your method to open UIImagePicker
}
// make sure to return NO so that your webview doesn't try to load your made-up URL
return NO;
}
Bonus : Make sure you have added App Transport Security Key in your info.plist.

Related

moving data from webview into ios app

I am not sure of how I would achieve the following functionality and was looking for suggestions. I have an Objective-C iOS app that loads a WebView. The user has a way to download data within the webview running a JS application. When the user triggers this download, I would like to use the downloaded data and execute a method call in the native code.
Looks like you have to use some kind of Hybrid calls. Once Javascript downloads the file, you should invoke a native app call (with a special schema say iosbridgecall:// from Javascript) and check this schema in shouldStartLoadWithRequest,
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] scheme] isEqualToString:#"iosbridgecall"]) {
//here invoke your Native Method, ex:
processDownloadedData();
return NO;
} else {
return YES;
}
}

Detect when url containing local anchor tag is clicked on UIWebView?

I want to detect when a url with local anchor tag is clicked on html page which is current been rendered on UIWebView. I have done lots of searches around and implemented various workarounds but no success. The ultimate suggestion from all around always comes to this delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
The biggest problem what I concluded was that this method simply doesn't get called for local anchor tags if already in a rendered HTML page. What i mean is if fresh new page URL (anchor tag) included is clicked that method gets fired. Here is an example. If i my UIWebView has already rendered www.xyz.com, now if i press some url which is like www.xyz.com#amazing (link present on that same page) then delegate method doesn't get fired.
I was developing a simple in-app browser. I had to show URL of every page been rendered/visible at address box of my in-app browser. Only obstacle now is i cannot show every anchor tags/javascript links that just brings new content but same URL domain.
There were solutions regarding usage of this
`[webView stringByEvaluatingJavaScriptFromString:#"window.location.hash"]`
Problem is that until and unless if I can't get delegate method called/fired than there is no benefit of this code.
Is there any workaround or shall I just give up on this? I hope I am clear with explaining the problem.
Note: Best TestCase Scenario is mobile version pinterest.com on UIWebView
Thanks.
I don't exactly follow what you're trying to do, perhaps you could clarify a bit more.
In the meantime, here's how to get your UIWebView to handle every link click in the web page it has loaded. It's looking for a URL Scheme that begins with "bloodwing". If it finds it, it will stop the request. Otherwise, it will work as expected.
So http://www.yahoo.com will work, and bloodwing://www.yahoo.com will fail.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
if ([url.scheme isEqualToString: #"bloodwing"]) {
[webView stringByEvaluatingJavaScriptFromString: #"doSomething();"];
return NO;
} else {
return YES; // Return YES to make sure regular navigation works as expected.
}
}
Hopefully this helps!

iOS - Check if webView is loaded with PDF content on non-'pdf' file extension

I would like to Check (boolean value) if PDF content is loaded in the UIWebView. This question already exists, but not this case.
It's not as simple as just checking the path extension of the webView Url because this PDF content is generated via ASPX. The extension will return 'aspx'. Also not just looking in the url, the string 'pdf' will always be found in the url since I requesting a PDF file. I need to check if the server really returns a PDF file as it should.
Ok, The webView is loaded with PDF content but not to a specific pdf file on the server. (maybe apache redirect,aspx pdf generation or just something else)
If this url is loaded in the Safari Application (to the aspx file), it will detect this PDF content (even if aspx file). It says "open in..." and so on.
How to detect if the UIWebView is loaded with PDF content on non-pdf extention file?
Jonathan
I solved it using this:
Thanks #Guilherme
-(bool)isPDFContentLoadedInWebView:(UIWebView *)webView {
NSString *mime =[[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request].response.MIMEType;
if (!([mime rangeOfString:#"pdf"].location == 0)) {
return YES;
} else {
return NO;
}
return NO;
}

Open PDF link to PDF Reader in iOS

Am getting the PDF file link from my web view. when i click that link, i want to open that PDF file in my iBook application.
i got the contents from server. I showed that content in UIWebview like this.
In this content have a PDF link "Emerging Trends in Real Estate". When i choose this link, i want to open this pdf file in iPhone PDF reader applications like iBook, Adobe Reader. When i click this link it goes to webView Delegate method
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
NSLog(#"PDF URL : %#",url);
if (UIWebViewNavigationTypeLinkClicked == navigationType)
{
if ([url isEqual:#"about:blank"])
{
return YES;
}
else
{
// Initialize Document Interaction Controller
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
// Configure Document Interaction Controller
documentInteractionController.delegate = self;
// Present Open In Menu
[documentInteractionController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];
}
}
return YES;
}
when compiler comes to else part i got an error. i displayed that error :
PDF URL : http://www.uli.org/wp-content/uploads/ULI-Documents/Emerging-Trends-in-Real-Estate-Americas-2014.pdf
2014-01-17 16:44:49.233 ULINewYork[3163:a0b] *** Assertion failure in -[UIDocumentInteractionController setURL:], /SourceCache/UIKit_Sim/UIKit-2903.23/UIDocumentInteractionController.m:1010
2014-01-17 16:44:49.234 ULINewYork[3163:a0b] *** WebKit discarded an uncaught exception in the webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: delegate: <NSInternalInconsistencyException> UIDocumentInteractionController: invalid scheme http. Only the file scheme is supported.
please give me some idea to handle this process.
I tried the same way you have used along with the help of apple documentation. I get the same error you mentioned, then I download the DocInteraction application for understanding the UIDocumentInteractionController In that application they use this class to open the files located in local app sandbox or the files located in the main bundle.
If your Intention is to make the user read the pdf file then leave the handling of request to the webview itself(take out the -webView: shouldStartLoadWithRequest: delegate method)
or else if you want to show some other options the ios device could do(print,preview,mail etc) with the file then you have to download that pdf file to local then set the url property of UIDocumentInteractionController object to the local path url you have saved the file before presenting.

Call alert view from website

I'm looking for a possibility to invoke an alert view from a website.
I'm pretty sure that this works somehow because if go through configuring your Apple ID and stuff like that in the App Store you are navigating through webviews and not a native environment (prior to iOS 7!).
Apple uses alert view and action sheets and date picker there so there has to be a way to do so.
I wasn't able to find anything useful on the web nor in the docs.
Cheers
Constantin
To achieve this you can use redirects, set javascript onClick function to some DOM element.
f.e
javascript function callNativeAlert(message) {
window.location = "showAlert://"+message;
}
On UIWebView delegate's you can catch such redirect then show your UIAlertView and ignore loading this page by returning NO
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[request.URL scheme] isEqualToString:#"showAlert"]) {
//TODO: Show your alert here, or execute any native method
NSLog(#"The message is %#", [request.URL host])
...
// Always return NO not to allow `UIWebView` process such links
return NO;
}
....
}
Note: this code has been written from memory
Note2: of course it works if you can modify both javascript and native application
You can use simple JS function alert('message').

Resources