Click on links within a UIWebView while browsing m.youtube.com on an iOS device

Utilizing uiwebview within my iOS application has been a great experience. I have successfully integrated a textbox above it that allows for seamless navigation of the web content. One particular example involves loading m.youtube.com, conducting a search on youtube.com, and smoothly transitioning to the video page upon selection. However, I am faced with the challenge of capturing the URL link of the video page in order to watch the selected video: m.youtube.com/watch?v=sa+2DFdfd . The question remains - how can this be achieved?

Key observations:

Upon testing this functionality on an iPhone 5 using Safari, I observed that the navigation bar dynamically adjusts based on the current webpage. For instance, when visiting m.youtube.com/watch?v=34124S on Safari, the updated URL is clearly visible in the browser. Unfortunately, replicating this behavior within the uiwebview has proven challenging as I struggle to retrieve the latest loaded webpage link.

The ShouldLoadStart method seems to function only during the initial page load. Subsequent clicks on the webpage, especially those involving JavaScript links, tend to go unnoticed by the delegates. This limitation prevents me from efficiently capturing URLs such as m.youtube.com/watch?v=34124S as seamlessly as Safari does on YouTube pages. Any assistance in resolving this issue would be greatly appreciated.

Answer №1

To store the URL, you can use the following code snippet:

NSString *url = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];

Alternatively, you can also access the URL like this:

url = currentWebView.request.URL.absoluteString;

These methods can be implemented in the following delegate method:

-(void)webViewDidFinishLoad:(UIWebView *)webView

Another way to achieve this is by using the following code within the webViewDidFinishLoad method:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
  NSLog(@"%@",webView.request.mainDocumentURL);

 [_activityindicator stopAnimating];
}

Answer №2

You should add the following code snippet to your webview delegate class:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{
    //RECORD USER CLICKS ON LINKS.
    NSURL *url = [request URL];
    NSLog(@"clicked link: %@",[url absoluteString]);

    return YES;
}

Answer №3

Through the central notification feature, I was able to obtain the link. When the video is displayed in full view, I seize that specific event and extract the inner HTML of the loaded pages to retrieve the unique link.

If you have any other suggestions, feel free to share them below!

Answer №4

I encountered a similar problem as well. One possible solution is to switch to using WKWebView.

With UIWebView, the only way I could get the current URL was by using JavaScript:

    [webView stringByEvaluatingJavaScriptFromString: @"location.href"];

Another issue I faced was that shouldStartLoadWithRequest wasn't being triggered. To work around this, you can intercept requests with NSURLProtocol (though this isn't possible with WKWebView) or simply check the current URL every second using a timer :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to refreshing the modal component with updated properties

In my application, I have created a modal component for managing recipes. This modal allows users to save recipes to a list. let modal = <Modal saveRecipe={this.saveRecipe} closeModal={this.toggleModal}/> However, I also want to utilize the same m ...

Issues involving the handleChange function in React can be a frustrating and common hurdle

I am encountering an issue with the handleChange function in my React project. Upon loading data from JSONPlaceholder and clicking on the 'Edit' button, a form is displayed with the selected user's information. However, when trying to edit ...

Polygons that were deleted on Google Maps will reappear once you zoom out

I am working on a map project where I draw polygons. My goal is to remove the previously drawn polygon whenever a new one is being drawn. Initially, everything seems to work fine. However, when I use the scroll function on the map, the previously drawn po ...

Can Child Component Changes in React Checkbox Cause Parent Node to Re-Renders?

Struggling with creating checkboxes for my project, I've spent an entire day on it without finding a solution. The issue involves a main checkbox that controls all child checkboxes. The desired functionality is to have the parent node's "allChec ...

Is it possible to configure npm install to install "bin" executables directly into the local node_modules directory (rather than the .bin directory)?

In my Node project, I am able to package it into a tarball using npm pack, and then install it in another directory for testing purposes. This allows the files specified in the "bin" section of my package.json file to be symlinked correctly into the node_m ...

Executing multiple HTTP requests in parallel with AXIOS and retrieving the responses even if one of the requests fails

I am currently working on optimizing server get requests to run concurrently. To achieve this, I have developed the following function. Issue The problem arises when one request fails, causing me to lose track of the responses from the other requests. e ...

Progress bar status displayed while uploading multiple files

I'm currently working on a Django project where I have set up a page to input data information about a file and then upload the file itself. https://i.sstatic.net/jB5cr.png Whenever the user clicks on the 'More datasets' button, it dyna ...

The mapStateToProps function is returning an undefined value

import React, { Component, Fragment } from "react"; import { connect } from "react-redux"; import { login, logout } from "./redux/actions/accounts"; import Home from "./Home"; import Login from "./Login"; class ToggleButton extends Component { render() ...

React components receive props as an empty array when they are being passed to the component

I am passing a state to a component as a prop in the following way: componentDidMount() { axios.get("path/to/data") .then(result => { this.setState({ receivedData: result.data, }); ...

Can all xcodes logs be searched through?

XCode has a useful feature of storing logs from previous runs, making it convenient for users. Is there a way to search through all of the stored logs? My situation is that I have encountered a specific error but can't recall the exact run it was in ...

Implementing the adding functionality within an ejs file

I'm currently working on a project that involves fetching data from an API using a simple JavaScript file upon clicking a button. Initially, everything was functioning properly when both the HTML file and JS file were in the same folder, and I could a ...

After the rendering process, the React Component member goes back to a state of

One issue I encountered is related to a component that utilizes a separate client for making HTTP requests. Specifically, when trying to use the client within a click event handler, the call to this.client.getChannel() fails due to this.client being undefi ...

Executing an Ajax SPARQL request in Firefox

I've been attempting to run an asynchronous Ajax sparql query on dbpedia using Firefox, but I encountered a strange issue that I can't seem to figure out. Surprisingly, the query works perfectly fine in Chrome, Edge, and Internet Explorer, but wh ...

What is the easiest method to design an email subscription form that remains fixed on the top right corner of the screen?

Looking for advice on setting up a sleek email signup bar that remains at the top of the browser while users scroll and navigate through different pages. I am working with WordPress and have jquery already loaded, but have not yet worked with either. Up ...

Emails sent through an HTML form submission should always include a valid "from"

I am currently in the process of creating a feedback form that allows individuals to provide anonymous feedback. I have explored nodemailer and node-sendmail, but I have encountered an issue with both requiring a from address. While I am aware that this ca ...

Using HTML and JavaScript to automatically update one input date based on changes made to another

Currently, I am developing an Angular application and encountered a challenge with two date input fields: <div class="col-lg-3"> <div> {{ dataInizioLabel }} </div> <input required type="datetime-local" ...

Looking to trigger the closing of a q-popup-proxy by clicking a button from a separate component

I am currently working with a q-popup-proxy component in my project. <q-btn label="Add Fault" class="addFaultButton" dense @click="openPopUp()"> <q-popup-proxy position="center" v-if="openFaults" ...

Errors can arise in Discord.js when encountering "undefined" before the first array object in an embed

I am currently in the process of creating a Discord bot command that allows users to construct their own city. I am encountering an issue with a list command that is supposed to display all the roads and places built within the city. However, each list kee ...

Loading Java Script files in real-time

Is there a method to dynamically load JS files before "$(document).ready" is triggered, while still having them available in the ready event handler? Is there a feature in jQuery that allows for this process? The challenge I am facing involves loading di ...

Having trouble adding a div in React due to the error "Objects are not allowed as a React child"?

While I am rendering the data and displaying it between divs, I keep getting this error: Objects are not valid as a React child (found: Wed Dec 09 1998 00:00:00 GMT+0530 (India Standard Time)). If you meant to render a collection of children, use an ...