{"id":271,"date":"2016-02-04T12:29:17","date_gmt":"2016-02-04T04:29:17","guid":{"rendered":"https:\/\/antijava.prjlife.com\/?p=271"},"modified":"2016-02-04T12:37:11","modified_gmt":"2016-02-04T04:37:11","slug":"custom-url-schemes-practice","status":"publish","type":"post","link":"https:\/\/antijava.prjlife.com\/?p=271","title":{"rendered":"Custom URL schemes practice"},"content":{"rendered":"<p>Let&#8217;s define something in this scenario :<\/p>\n<p>1.Receiver app &#8211; to be invoked via custom URL<\/p>\n<p>2.Sender app &#8211; who wants to invoke the app<\/p>\n<p>3.Custom URL scheme &#8211; define as &#8220;happygo&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>For Receiver app, here&#8217;s what to do:<\/p>\n<p>1.Define the url scheme in Project\/Targets\/Info\/URL Types as follow<\/p>\n<p><a href=\"https:\/\/antijava.prjlife.com\/wp-content\/uploads\/2016\/02\/\u87a2\u5e55\u5feb\u7167-2016-02-04-\u4e0b\u534812.20.51.png\" rel=\"attachment wp-att-272\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-272\" src=\"https:\/\/antijava.prjlife.com\/wp-content\/uploads\/2016\/02\/\u87a2\u5e55\u5feb\u7167-2016-02-04-\u4e0b\u534812.20.51-300x117.png\" alt=\"\u87a2\u5e55\u5feb\u7167 2016-02-04 \u4e0b\u534812.20.51\" width=\"300\" height=\"117\" srcset=\"https:\/\/antijava.prjlife.com\/wp-content\/uploads\/2016\/02\/\u87a2\u5e55\u5feb\u7167-2016-02-04-\u4e0b\u534812.20.51-300x117.png 300w, https:\/\/antijava.prjlife.com\/wp-content\/uploads\/2016\/02\/\u87a2\u5e55\u5feb\u7167-2016-02-04-\u4e0b\u534812.20.51.png 716w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>2.AppDelegate.m &#8211; Two methods to be implemented.<\/p>\n<pre>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\r\n return YES;\u00a0\u00a0\u00a0 \/\/must return YES\r\n }<\/pre>\n<pre>- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {\r\n \/\/Check if the scheme was valid\r\n if ([[url scheme] isEqualToString:@\"happygo\"]) {\r\n NSString *query = [url query];\r\n NSArray *queryComponents = [query componentsSeparatedByString:@\"&amp;\"];\r\n\r\n for (int i=0; i&lt;[queryComponents count]; i++) {\r\n NSArray *oneParam = [[queryComponents objectAtIndex:i] componentsSeparatedByString:@\"=\"];\r\n NSString *sValue = @\"\";\r\n NSString *sKey = [oneParam objectAtIndex:0];\r\n if (oneParam.count==2) {\r\n sValue = [oneParam objectAtIndex:1];\r\n }\r\n }\r\n \/\/Here is what you want to do, write here.\r\n return TRUE;\u00a0\u00a0 \/\/return TRUE because of valid scheme\r\n }\r\n return FALSE;\u00a0\u00a0 \/\/invalid scheme, skip process\r\n }<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>For Sender app, here&#8217;s what to do :<\/p>\n<p>1.Add the following to info.plist<\/p>\n<pre>&lt;key&gt;LSApplicationQueriesSchemes&lt;\/key&gt;\r\n &lt;array&gt;\r\n &lt;string&gt;happygo&lt;\/string&gt;\r\n &lt;\/array&gt;<\/pre>\n<p>2.Invoke the receiver app via URL form, sample source code as follow<\/p>\n<pre>NSString *sURL = [NSString stringWithFormat:@\"happygo:\/\/www.domain.com?name=Good&amp;phone=123123\"];\r\n NSURL *myURL = [NSURL URLWithString:sURL];\r\n\r\n if ([[UIApplication sharedApplication] canOpenURL:myURL]) {\r\n [[UIApplication sharedApplication] openURL:myURL];\r\n } else {\r\n NSLog(@\"--bad url--\");\r\n }<\/pre>\n<p>Actually, you can even invoke the app via Safari Browser.<\/p>\n<p>Just input the URL in Safari, press GO, and the receiver app will be invoked.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s define something in this scenario : 1.Receiver app &#8211; to be invoked via custom URL 2.Sender app &#8211; who wants to invoke the app 3.Custom URL scheme &#8211; define as &#8220;happygo&#8221; &nbsp; &nbsp; For Receiver app, here&#8217;s what to &hellip; <a href=\"https:\/\/antijava.prjlife.com\/?p=271\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-271","post","type-post","status-publish","format-standard","hentry","category-ios-development"],"_links":{"self":[{"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=271"}],"version-history":[{"count":0,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/posts\/271\/revisions"}],"wp:attachment":[{"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}