iOS的URLScheme

一直都有接触要设置app的url scheme,从最早的facebook開始。
当时的理解是SSO用的,当授权成功之后,facebook app或者safari能够利用给定的url scheme来回调程序。依照Facebook dev页的指南,在Info.plist中增加url scheme。


怎样在info.plist中增加url scheme

直接将info.plist用源代码打开,增加例如以下代码:

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

当然也能够在xCode中增加URL Type的row,只是遇到过xCode不自己主动提示补全,自己写的话,会无效,所以还是比較信任源代码~

直接在xCode中操作加入URL Types

再后来xCode的project属性Info页签以下有专门的URL Types条目加入URL Types。在Identifier中填入BundleID,URL Schemes中填入相应的Scheme。


Scheme的作用在之前facebook的使用中已经领悟了一部分,可是还有其它强大的功能,比方你想要在iOS设备的safari中输入一个URL(后面你会知道。就是你自定义的URL Schemes)就能直接打开你的设备。就和启动众多系统应用一样!

!没错,你的DIAO丝程序也能像挂着苹果老爹光环的“高帅富”应用一样启动。
系统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=NOTIFICATIONS_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


想要实现上面的功能,在应用中只增加scheme是不够的,还须要针对该scheme做相应的处理,有点类似广播通讯的原理,输入scheme后发出广播。你须要写接收该广播以及处理的代码。

  1. 不要实现applicationDidFinishLaunching:方法
  2. 实现application:didFinishLaunchingWithOptions:方法。并在当中检查url。假设能处理该url则返回YES。否则不做处理返回NO。
  3. 实现application:handleOpenURL:,处理url。成功返回YES,否则返回NO。

在iOS4中。URL分下面两种方式传入app中

  1. 假设是启动程序。则application:didFinishLaunchingWithOptions:被运行,返回YES当且仅当application:handleOpenURL:运行返回YES。

  2. 假设程序由后台挂起变为活动。则application:didFinishLaunchingWithOptions:不会被运行,可是application:handleOpenURL:会被运行。


处理代码例如以下:

- (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:

注意:

在iOS4.2之后,application:handleOpenURL:将被DEPRECATED
能够使用

- (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即为调用者发送给接收者的Bundle ID,比方MobileSafari就是com.apple.mobilesafari

annotation
A property-list object supplied by the source application to communicate information to the receiving application.

调用者发给接收者的plist对象


假设该程序没有被安装。那么在Safari中打开我们希望能跳转到下载界面。须要实现这个功能,我们能够利用JS的window.location和setTimeout函数。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<script>
window.location = "URL Scheme:";
setTimeoutfunction(){ window.location="http://App Store地址";   } , 1500);
</script>
</body>
</html>

将该代码保存为index.html。放于站点上,利用Safari打开该文件。则可以实现假设安装了该应用,则打开。否则可以直接跳转到App Store的App下载页面。

注意

JS中的URL Scheme不要包括非转义字符,比方'-./'等,假设有非转义字符则须要转义后输入到Safari,直接在Safari中输入URL Scheme打开应用,假设提示

Safari打不开该网页,由于这是一个本地文件

那么非常有可能由于你的URL Scheme中包括了非转义字符,最好全为英文字母数字最好。


代码怎样调用

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

posted on 2017-05-13 15:33  yjbjingcha  阅读(1921)  评论(0编辑  收藏  举报

导航