IOS URLScheme and iOSURLScheme

Source: Internet
Author: User
Tags call back

IOS URLScheme and iOSURLScheme

There has always been access to the url scheme of the app to be set, starting from the earliest facebook.
At that time, it was understood that SSO was used. After successful authorization, facebook app or safari can use the given url scheme to call back and forth the program. Follow the guide on the Facebook dev page to add url scheme to Info. plist.

How to add url scheme to info. plist

Open info. plist directly with the source code and add the following code:

<key>CFBundleURLTypes</key>
   <array>
       <dict>
           <key>CFBundleURLName</key>
           <string>com.xxxx.xxxx</string>
           <key>CFBundleURLSchemes</key>
           <array>
               <string>yourURLScheme</string>
           </array>
       </dict>
   </array>

Of course, you can also add the URL Type row in xCode. However, if xCode does not automatically prompt completion, it will not work if you write it yourself, so you still trust the source code ~

Add URL Types directly in xCode

Then, under the project property Info tab of xCode, a special URL Types entry is added to URL Types, BundleID in Identifier, and Schemes in URL.

The role of Scheme has been understood in facebook's previous use, but there are other powerful features, such as entering a URL in safari on iOS devices (you will know later, your own defined URL Schemes) can directly open your device, just like starting many system applications !! Yes, your DIAO silk program can also be started like the "handsome rich" app with the aura of Apple's father.
System URL Schemes

Stock
Some of these URL schemes doesn' t work anymore in the latest iOS. Let's hope they get reintroduced.

Itms-apps: //-Open the App Store
Maps: //-Open the Maps app
Sms: //-Open the compose window of the Messages app
Music: //-Go to the currently playing song in the Music app
Youtube: //-Open the YouTube app
Itms-books: //-Open the iBooks app
Facetime ://
Prefs: root = General & path = Bluetooth
Prefs: root = General & path = AUTOLOCKS
Prefs: root = icationications_id
Prefs: root = General & path = USAGE
Prefs: root = General & path = Bluetooth
Prefs: root = AIRPLANE_MODE
Prefs: root = Brightness
Prefs: root = Wallpaper
Prefs: root = INTERNET_TETHERING
Prefs: root = CASTLE
Prefs: root = CASTLE & path = STORAGE_AND_BA
Prefs: root = General & path = About
Prefs: root = General & path = USAGE/CELLULAR_USAGE
Prefs: root = MUSIC & path = EQ
Prefs: root = General & path = Network
Prefs: root = LOCATION_SERVICES
Prefs: root = Phone & path = CallerID
Prefs: root = Phone & path = CallForwarding
Prefs: root = Safari
Prefs: root = General & path = Assistant
Prefs: root = General & path = Keyboard
Prefs: root = Sounds
Prefs: root = General & path = Network/VPN
Prefs: root = WIFI

To implement the above functions, it is not enough to add scheme to the application. It also needs to process the scheme, which is similar to the principle of broadcast communication. After the scheme is input, broadcast is sent, you need to write the code to receive and process the broadcast.

  1. Do not implement applicationDidFinishLaunChing: Method
  2. Implement application: didFinishLaunchingWithOpTions: method, and check the url. If the url can be processed, YES is returned. Otherwise, NO is returned.
  3. Implement application: handleOpenURL:, process the url. If YES is returned successfully, otherwise NO is returned.

In iOS4, the URL is transmitted to the app in two ways:

  1. If the program is started, application: didFinishLaunchingWithOpTions: executed. YES is returned. if and only when application: handleOpenURL: executed, YES is returned.
  2. Application: didFinishLaunchingWithOpTions: will not be executed, but application: handleOpenURL: will be executed.

The processing code is as follows:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
   NSLog(@"%@", [url absoluteString]);
   if ([[url host] isEqualToString:@"yourURLScheme"]) {
       return YES;
   }
   return NO;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   ...
   NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
   if ([self application:application handleOpenURL:url]) {
       return YES;
   }
   return NO;
// End of application:didFinishLaunchingWithOptions:
Note:

After iOS4.2, application: handleOpenURL: Will be DEPRECATED
Available

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
       sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

SourceApplication
The bundle ID of the application that is requesting your application to open the URL (url ).

SourceApplication is the Bundle ID sent by the caller to the receiver. For example, MobileSafari is com. apple. mobilesafari.

Annotation
A property-list object supplied by the source application to communicate information to the processing application.

The plist object sent by the caller to the receiver.

If the program is not installed, open the download interface in Safari. To implement this function, we can use the window. location and setTimeout functions of JS.

<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd">
<Html>
<Body>
<Script>
Window. location="URL Scheme :";
SetTimeout (Function(){Window. location = "http: // App Store address "; },1500 );
</Script>
</Body>
</Html>

Save the code as index.html and Store it on the website. open the file using Safari. If the application is installed, open it. Otherwise, you can directly jump to the App download page of the App Store.

Note:

Do not include non-escape characters in URL Scheme in JS, such '-. /'. If there are non-escape characters, escape them and enter them in Safari. directly enter the URL Scheme in Safari to open the application.

Safari cannot open the webpage because it is a local file

It is very likely that your URL Scheme contains non-escape characters. It is best to have all English letters and numbers.


How to call code
     NSURL *url = [NSURL URLWithString:@"URL Scheme://"];
[[UIApplication sharedApplication] openURL:url];

Want to know where to find the url scheme of an ios app?

In the plist file of this program, set:
URL Schemes is defined under URL Types. You can search for URL Scheme. There are many introductions on the Internet.

The third-party IOS app does not provide URL Scheme for other programs to call. What method can I use to call it in code?

Import the header file, and
Delegate
 

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.