Now more and more apps are H5 and native hybrid development, this is really convenient and fast, but the H5 part of the total avoidance of many and native interaction, the original tune JS function is relatively simple, Native API function Stringbyevaluatingjavascriptfromstring can complete the requirements, but JS tune the native function, the system does not provide the API, so a lot of people companies are using the form of tag bits, our company is no exception, Project development for a long time, the original and JS interaction has been my responsibility, our project this piece also want a lot of interactive operation, random version of the iteration, the problem is more and more, and then think of a lot of ways to solve the problem.
One, my original plan, intercept URL complete
The way to intercept URLs is to intercept the thrown url,url containing key identity fields in the following ways, calling native functions based on these fields so that the corresponding actions in H5 can all throw URLs that contain key fields, and then natively take different actions based on their respective fields, completing H5 and native interactions.
-(BOOL) WebView: (uiwebview *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationtype
Return Yes WebView continue to load the new URL, return No to load, so that any need to take action of the URL can return no, let WebView no longer load, just call the original function on the line.
However, there are too many H5 pages in the project, many public interfaces are fixed, such as pop-up user login, play video, open the image browser, as well as jump products, events, news, notice details, so you need to encapsulate a framework, so that all public interfaces can be the corresponding public interface JS and native Intermodulation, What I'm taking here is in a categorized way, here in the way of classifying, setting up proxies, and there are some tricks:
1, define an agent in the classification, and use the runtime to implement setter and getter, here set the agent's protocol is uiwebviewdelegate the benefit is the outside feel not to proxy conversion, also thought is directly set WebView agent, the less exposed to the outside, The easier it is to use.
@property(nonatomic,weak) ibinspectable ID<uiwebviewdelegate >customDelegate;
-(ID<uiwebviewdelegate>) customdelegate
{
return objc_getassociatedobject(self, @selector(customdelegate));
}
-(void) Setcustomdelegate: (ID<uiwebviewdelegate>) customdelegate
{
objc_setassociatedobject(self, @selector(customdelegate), customdelegate, objc_ Association_assign);
}
2, set your own delegate is yourself, and implement the Proxy method,
Self. Delegate = self;
3, the key to the place, first on the Code bar:
let each webView have a proxy that can still be set and corresponding individual methods
-(BOOL) WebView: (uiwebview *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationtype
{
//h5 The URL thrown
nsstring *absolutestring = Request. URL. Absolutestring;
BOOL action = [self takeactionwith: absolutestring]; // take different actions depending on the URL being thrown
if (!action) {
return NO;
}
// Execute each webView own customdelegate proxy method
if (self. Customdelegate && [self. Customdelegate respondstoselector:@selector(webview:shouldstartloadwithrequest: Navigationtype:)]) {
return [self. Customdelegate webView: WebView shouldstartloadwithrequest:request Navigationtype: Navigationtype];
}
return YES;
}
After intercepting the URL according to the identity field to take their own actions, and determine whether you need to continue to load the current URL, and then call the proxy loading method, in addition to the public interface, but also to the outside of each place to customize their own required actions
4, completed, to provide users with interface
-(void) seturlstring: (nsstring *) urlstring block: (void(^) (nsarray *arr)) postimage Delegate: (ID<uiwebviewdelegate>) delegate;
Then in this method can load the URL, and then set up the proxy, call this classification of the place, if you need to customize the Uiwebviewdelegate proxy method, you can also set delegate, and then implement their own need to complete the operation, External use WebView No slightest change, loading URLs, setting up proxies, implementing proxy methods.
At this point, encapsulating a common interface allows JS and native intermodulation classifications to be completed, which I did at the very beginning. But random version iterations, more and more, more and more key fields, through the string alignment method, it is easy to conflict with other URLs, and then get unwanted effects, and the string alignment is also in order, if a URL contains two key fields, then which string is written in front, The function that corresponds to which identity field is executed first.
Obviously, it's unreasonable, and it's not what we can tolerate.
Specification of a field that specifies the URL to be thrown
So, I think how to solve this problem, but after thinking, or feel that the URL is thrown without any specification, just to determine whether the URL contains certain fields, the other field H5 over there casually equipped, obviously is very unreasonable, so, the Internet to see a lot of solutions, And the URL of the background constraint throws the field specification:
Func://name=leehonn/age=24
Such a field is concise and clear, Func is the function name, followed by parameters, we can through the OC provided by the API method, first through the "//" url into an array, the first element of the array is the function name, and then the following string through the "/" split, to get their own parameters, The advantage of this is that there is no extra field, it is not easy to conflict, the required fields are in, it is convenient to parse.
When the function name is obtained, the function can be executed:
SEL originalselector = nsselectorfromstring(absolutestring);
[self performselector: originalselector];
This is done, optimized, and this block and Android can be universal, Android can also be reflected to perform functions, but it is said that the performance will be reduced, but even without reflection, with the original string alignment, and more than the format of the previous URL, but for the iOS side, it is really more than the previous optimization.
Third, adopt a tripartite framework
Later still feel this form is very lame, will look at the third-party framework, and then found the Webviewjavascriptbridge, the number of stars is also very much, and then take to use, in addition, the framework is to take the way to intercept URLs, but he will be the details of the package up just, And this framework also need JS code side of the match, and this framework is not as you want to intercept JS onclick event, the specific use of the method you can go to GitHub to see the official introduction, but don't forget, this framework is required JS over the code to match, Need JS for iOS specifically adapted to a set, Android side also has its own set of plans, if you do H5 colleagues not too much trouble, you can get two sets
It is also said that the Stringbyevaluatingjavascriptfromstring function can be used to complete the JS and the native intermodulation, there is known that the great God wants to share the next
iOS js and native interaction, function intermodulation