The application scenarios for URL schemes in iOS development are inter-app jumps, UIWebView interacting with JavaScript, respectively.
I. Inter-application Jumps
To set up an inter-app jump:
The URL that you added in your home app->info->url types schemes, and the URL schemes to include in your home app.plist file support jump whitelist.
Assume that the URL schemes is weixin,identifier to com.wx.test.
The jump code for your home app is as follows:
@" weixin://com.wx.test?viewid=letters " ; *url = [Nsurl urlwithstring:str]; if ([[[UIApplication sharedapplication] canopenurl:url]) {[ [uiapplication sharedapplication] openurl:url]; }
In the same vein, go back to your own app and get your own app URL schemes, with the URL schemes of your own app in the whitelist.
Two. UIWebView interacting with JavaScript
UIWebView calling the hellowworld function in javascript
[Self.webview stringbyevaluatingjavascriptfromstring:@ "helloWorld (' Invoke JS HelloWorld (msg) function from iOS object ') "];
Index.html
<!DOCTYPE HTML><HTML> <Body> <Buttononclick= ' Showandroiddialog ("JScall iOS object ") '>Click</Button> </BR> <DivID= ' message '></Div> <Script> functionHelloWorld (msg) {document.getElementById ('message'). InnerHTML=msg; } functionShowandroiddialog (msg) {varMyjsonobject= NewObject (); Myjsonobject.title= 'Forr'; Myjsonobject.message=msg;/*JS calling iOS object*/ /*generic Object identifier URI **<scheme> gap ***/ varjsonstring=json.stringify (Myjsonobject); varURI= 'gap://xxxclass.xxxmethod#'+jsonstring; Window.location=uri;//redirect URL}</Script> </Body></HTML>To intercept custom protocol gap in UIWebView proxy method:, if this agreement isJavaScriptWhat you want to do, call the native app's methods, which are agreed in advance and block this link's jump.
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest*Request Navigationtype: (uiwebviewnavigationtype) navigationtype{//Get JS InformationNSString *actiontype =request. Url.host; NSString*scheme =request. Url.scheme; NSString*fragment = [Request. Url.fragment urldecodedstring];//URL decodingNSData *responsedata =[Fragment datausingencoding:nsutf8stringencoding]; if([Scheme isequaltostring:@"Gap"]) { if([ActionType isequaltostring:@"Xxxclass.xxxmethod"]) {Nserror*error; Nsdictionary*json = [Nsjsonserialization jsonobjectwithdata:responsedata options:nsjsonreadingallowfragments error:&ERROR]; NSLog (@"title =%@,message =%@", [JSON Objectforkey:@"title"],[json Objectforkey:@"message"]); }
return NO; } returnYES;}
IOS URL Schemes App