IOS URL Schemes App

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.