2

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.

1 Answers1

1

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.

byJeevan
  • 3,728
  • 3
  • 37
  • 60
  • I am not authorised to make any change in the web page. It is client organisation's web page. Is there any way to detect the picker only in the app side logics? – Siddharth Eswaramoorthy Jul 01 '17 at 05:07
  • Okey, then can you please share the `html` lines that related button actions? – byJeevan Jul 01 '17 at 05:28
  • Also don't forget to try [Jockey.js](https://github.com/tcoulter/jockeyjs) may be helpful to solve your problem. – byJeevan Jul 01 '17 at 05:30
  • 1
    @RikudouSennin you don't need to change the source of the web page, simply inject JavaScript into the web view when it loads (i.e. in `webViewDidFinishLoad`). Use [stringByEvaluatingJavaScript](https://developer.apple.com/documentation/uikit/uiwebview/1617963-stringbyevaluatingjavascript) to inject the JavaScript. – paulvs Jul 02 '17 at 19:22