


Detailed explanation and implementation code of interaction between javascript and IOS ObjectC
This article mainly introduces the detailed explanation of the interaction between IOS OC and js and the relevant information on the implementation code. Friends in need can refer to
Detailed explanation of the interaction between IOS OC and js
JS injection: Inject JS code with OC into the web page
JS injection is also called the interaction between OC and JS
The interaction between OC and JS Interaction requires a bridge (intermediary), which is the proxy method of UIWebView
Loading the initial content of the web page
#import "ViewController.h" @interface ViewController ()<UIWebViewDelegate> @property (weak, nonatomic) IBOutlet UIWebView *webView; @end - (void)viewDidLoad { [super viewDidLoad]; // 设置webView的代理 self.webView.delegate = self; // 加载网页数据 NSURL *URL = [NSURL URLWithString:@"http://m.dianping.com/tuan/deal/5501525"]; // NSURL *URL = [NSURL URLWithString:@"https://www.hao123.com/?tn=93321723_hao_pg"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; [self.webView loadRequest:request]; }
Use js to change the native web page in the proxy method of UIWebView
/// 网页加载完成之后调用的代理方法 : JS注入 : OC调用JS代码 - (void)webViewDidFinishLoad:(UIWebView *)webView { // 用于拼接JS代码的字符串 NSMutableString *stringM = [NSMutableString string]; // 拼接移除顶部导航的JS代码 [stringM appendString:@"var headerTag = document.getElementsByTagName('header')[0]; headerTag.parentNode.removeChild(headerTag);"]; // 拼接移除橙色按钮的JS代码 [stringM appendString:@"var footerBtnTag = document.getElementsByClassName('footer-btn-fix')[0]; footerBtnTag.parentNode.removeChild(footerBtnTag);"]; // 拼接移除底部布局的JS代码 [stringM appendString:@"var footerTag = document.getElementsByClassName('footer')[0]; footerTag.parentNode.removeChild(footerTag);"]; // 拼接给img标签添加点击事件的JS代码 [stringM appendString:@"var imgTag = document.getElementsByTagName('figure')[0].children[0]; imgTag.onclick = function(){window.location.href='https://www.baidu.com'};"]; // 这个方法就是UIWebView提供的.专门做JS注入的方法 [webView stringByEvaluatingJavaScriptFromString:stringM]; }
Interception of native network request webpage jump
imgTag.onclick = function(){window.location.href='https://www.baidu.com‘}
When clicking imgTag, actively send network requests
The purpose of actively sending network requests: is to allow UIWebView to intercept my customization URL
Use the customized URL to determine/distinguish whether the label I clicked is the label I designed
Customize a unique URL, indicating that the clicked one Unique tag
In summary, it is two steps
The first step: JS injects the click event of the tag and actively sends a custom URL request
Step 2: In UIWebView, intercept the request for the custom URL, and then determine the request
JS indirectly calls OC: Interaction between JS and OC
The proxy method called when the web page is about to start loading: can intercept all network requests on the webView
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // 获取拦截到的所有的请求 NSString *URLString = request.URL.absoluteString; //https://m.baidu.com/?from=1015143h // NSLog(@"%@",URLString); if ([URLString isEqualToString:@"https://m.baidu.com/?from=1015143h"]) { NSLog(@"我点击的是imgTag"); // 当我知道点击的是imgTag时,自动push //http://www.csdn.net/ NSURL *URL = [NSURL URLWithString:@"http://www.csdn.net/"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; [self.webView loadRequest:request]; // TestViewController *testVC = [[TestViewController alloc] init]; // [self.navigationController pushViewController:testVC animated:YES]; // 因为这个地址是无效地址.不需要加载的 return NO; } // 返回YES的作用 : 表示你拦截到的请求,允许正常的发送出去;反之,不允许拦截到的请求发送出去 return YES; }
The above is the detailed content of Detailed explanation and implementation code of interaction between javascript and IOS ObjectC. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The annual WWDC has ended, and iOS18 is undoubtedly the focus of everyone's attention. Currently, many iPhone users are rushing to upgrade to iOS18, but various system bugs are making people uncomfortable. Some bloggers said that you should be cautious when upgrading to iOS18 because "there are so many bugs." The blogger said that if your iPhone is your main machine, it is recommended not to upgrade to iOS18 because the first version has many bugs. He also summarized several system bugs currently encountered: 1. Switching icon style is stuck, causing the icon not to be displayed. 2. Flashlight width animation is often lost. 3. Douyin App cannot upload videos. 4. WeChat message push is delayed by about 10 seconds. 5 . Occasionally, the phone cannot be made and the screen is black. 6. Severe fever.

Thanks to netizens Ji Yinkesi, xxx_x, fried tomatoes, Terrence, and spicy chicken drumsticks for submitting clues! According to news on July 27, Apple today re-released the iOS/iPadOS 18 Beta 4 update for developers. The internal version number was upgraded from 22A5316j to 22A5316k. It is currently unclear the difference between the two Beta 4 version updates. Registered developers can open the "Settings" app, enter the "Software Update" section, click the "Beta Update" option, and then toggle the iOS18/iPadOS18 Developer Beta settings to select the beta version. Downloading and installing the beta version requires an Apple ID associated with a developer account. Reported on July 24, iO

According to news on July 31, Apple issued a press release yesterday (July 30), announcing the launch of a new open source Swift package (swift-homomorphic-encryption) for enabling homomorphic encryption in the Swift programming language. Note: Homomorphic Encryption (HE) refers to an encryption algorithm that satisfies the homomorphic operation properties of ciphertext. That is, after the data is homomorphically encrypted, specific calculations are performed on the ciphertext, and the obtained ciphertext calculation results are processed at the same time. The plaintext after state decryption is equivalent to directly performing the same calculation on the plaintext data, achieving the "invisibility" of the data. Homomorphic encryption technology can calculate encrypted data without leaking the underlying unencrypted data to the operation process.

Update: Saunders Tech has uploaded a tutorial to his YouTube channel (video embedded below) explaining how to install Fortnite and the Epic Games Store on an iPad outside the EU. However, not only does the process require specific beta versions of iO

According to news on June 1, technology media AppleInsider published a blog post today, stating that Apple will launch a new navigation function of "Scenes" for the "Freeform" application extension in the iOS18 system, and add new options for object alignment. Introduction to the "Wubianji" application First, let's briefly introduce the "Wubianji" application. The application will be launched in 2022 and has currently launched iOS, iPadOS, macOS15 and visionOS versions. Apple’s official introduction is as follows: “Boundless Notes” is an excellent tool for turning inspiration into reality. Sketch projects, design mood boards, or start brainstorming on a flexible canvas that supports nearly any file type. With iCloud, all your boards

[Click here to go directly to the upgrade tutorial] According to news on May 8, Apple pushed the iOS17.5RC update (internal version number: 21F79) to iPhone users today. This update is 70 days away from the last release. How to upgrade iOS/iPadOS/watchOS/macOS development version and public beta version? To upgrade the iOS/iPadOS17 developer preview version and public beta version, you can refer to the experience shared by friends: Experience Post 1||Experience Post 2||Experience Post 3||Experience Post 4. Starting from the iOS/iPadOS 16.4 Developer Preview Beta 1, you need to register for the Apple Developer Program. After registration, open the system [Settings] [Software Update] to see the upgrade option. Please note that your iPhone or IP

Thanks to netizens Spicy Chicken Leg Burger, Soft Media New Friends 2092483, Handwritten Past, DingHao, Xiaoxing_14, Wowotou Eat Big Kou, Feiying Q, Soft Media New Friends 2168428, Slades, Aaron212, Happy Little Hedgehog, Little Earl, Clues for the little milk cat that eats fish! [Click here to go directly to the upgrade tutorial] According to news on July 24, Apple today pushed the iOS/iPadOS18 developer preview version Beta4 update (internal version number: 22A5316j) to iPhone and iPad users. This update is 15 days after the last release. . Carplay Wallpaper Apple has added wallpapers to CarPlay, covering light and dark modes. Its wallpaper style is similar to iPhone

In the just-concluded European Cup final, did you cheer crazily for the team you supported? In the upcoming Paris Olympics, are you also looking forward to perfectly capturing the highlight moments of each event? Among them, having a high-quality viewing equipment is crucial. The Haqu K2 projector is well-deserved to be a good choice for watching games due to its high cost performance and excellent performance. It not only has high brightness and clear picture quality, but also provides an immersive viewing experience, making every exciting moment of the game feel as if it is close at hand. Are you already attracted by such a device? It will definitely allow you to enjoy the passion and dreams of the Olympic Games at home. The most intimate highlight of Haqu K2 is its 210° super angle adjustment, which makes it convenient to watch movies whether on the ceiling or on the wall.
