系统调用

获取设备信息

UIDevice *device = [UIDevice currentDevice];
// 设备名称
NSLog(@"name: %@", device.name);
// 设备的类型
NSLog(@"model:%@", device.model);
NSLog(@"localizedModel: %@", device.localizedModel);
// 操作系统版本号
NSLog(@"systemVersion: %@", device.systemVersion);
// 获取设备UUID
NSLog(@"UUID: %@", device.identifierForVendor.UUIDString);
// 电池电量
device.batteryMonitoringEnabled=YES;
NSLog(@"battery: %@", device.batteryLevel);

监听屏幕方向

- (void)viewDidLoad {
    [super viewDidLoad];
    UIDevice *device = [UIDevice currentDevice];
    //开启方向改变通知
    [device beginGeneratingDeviceOrientationNotifications];
    //注册方向改变通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];   
}

-(void) orientationChange{
    UIDevice *device = [UIDevice currentDevice];    
    switch (device.orientation) {
        case UIDeviceOrientationPortrait:
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            break; 
        case UIDeviceOrientationLandscapeLeft:
            break;
        case UIDeviceOrientationLandscapeRight:
            break;
        case UIDeviceOrientationFaceUp:
            break;
        case UIDeviceOrientationFaceDown:
            break;           
        default:
            break;
    }
}

-(void)dealloc {
    //移除监听
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

UIDevice类中,可通过orientation属性获知设备六个方向的信息,除了横竖屏四个方向之外,还可以获取到正反放置的信息:

/* UIDeviceOrientation 枚举*/
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // 竖屏,正常状态
    UIDeviceOrientationPortraitUpsideDown,  // 竖屏,倒置
    UIDeviceOrientationLandscapeLeft,       // 向左横屏
    UIDeviceOrientationLandscapeRight,      // 向右横屏
    UIDeviceOrientationFaceUp,              // 正面朝上
    UIDeviceOrientationFaceDown             // 正面朝下
}

不同APP间跳转

什么是 URL Scheme

简单的说,由于苹果选择使用沙盒机制来保障用户的隐私和安全,APP只能访问自己沙盒数据,但同时也阻碍了应用间合理的信息共享。因此苹果提供了一种可以在APP之间跳转的方式:URL Scheme

如果我们的APP需要被其他APP访问某些功能或数据,那么就需要在我们的APP中定义一个相应的URL Scheme,当其他APP使用URL Scheme进行访问时,系统会根据URL Scheme进行匹配,从而来拉起我们的APP。

拉起短信页面:

UIApplication *app = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:@"sms://10086"];
if ([app canOpenURL:url]) {
    float deviceVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    // @available(iOS 10.0,*)
    if (deviceVersion - 10 < 0) {
        [[UIApplication sharedApplication] openURL:url];
    }else{
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}

这里completionHandler回调会携带一个布尔参数success,表示是否成功打开URL。关于iOS系统提供的Scheme,可查看官方文档 Apple URL Schemes

这里有几个问题需要注意:

  • 自iOS9之后,canOpenURL方法受到限制。为防止APP利用该接口大量检测当前系统安装了哪些应用,开发者需要事先配置检测名单(系统Schemes不受影响,如系统设置页面App-Prefs:root)。详细请参见文档 canOpenURLLSApplicationQueriesSchemes

    例如,在Info.plist添加配置:

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>alipay</string>
        <string>weibo</string>
        <string>mqq</string>
        <string>baidumap</string>
        <string>wechat</string>
        <string>weixin</string>
    </array>
    
  • iOS10之后,不带参数的openURL方法弃用,应当使用openURL:options:completionHandler:方法。

自定义 URL Scheme

总共分两个步骤:

  1. 注册URL Scheme
  2. 处理调用请求

这里注册Scheme又可以通过两种方式

  • 在Xcode中,选择【TARGETS 】-> 【Info 】->【URL Types】进行添加
  • 直接在info.plist中添加
<key>CFBundleURLTypes</key>
  <array>
    <dict>
      <key>CFBundleTypeRole</key>
      <string>Editor</string>
      <key>CFBundleURLName</key>
      <string>[ANY_URL_NAME]</string>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>[YOUR_SCHEME]</string>
      </array>
    </dict>
  </array>

iOS以URL types为单位管理URL schemes(相当于协议头),注册URL schemes,实际上就是注册URL types。

必须
CFBundleURLSchemes 字符串数组,一个字符串代表一个scheme,可以有多个scheme
CFBundleURLName URL类型的抽象名称,必须唯一。一般域名倒置,如com.host.my
CFBundleURLIconFile 此URL类型的图标文件名称
CFBundleTypeRole 指定App在 URL 类型方面的角色。值可以是EditorViewerShell,或None

参考官方文档: CFBundleURLTypes

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLName</key>
        <string>com.demo.test</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>wb2522720237</string>
            <string>navigation</string>
        </array>
    </dict>
    <!--其他URL type...-->
<array>

自iOS9之后,实现如下方法处理调用请求:

// AppDelegatete
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    if ([[url absoluteString] hasPrefix:@"schema://"]) {
         return YES;
     }
}
  • url是调用者传入的url,利用NSURL解析,可获取所需信息

  • options是字典,可以获取调用者的Bundle ID

URL Scheme 的缺点

  • URL Scheme可能会被劫持带来安全隐患。多个App注册了同一种 URL Scheme 的时候,启动哪个App是不确定的
  • 做deeplink 时,浏览器无法判断是否安装某个App,若未安装会弹出对话框

因此,iOS9之后,推出了 Universal Link

常用第三方Scheme

可以通过捷径社区网站查询一些常见应用的URL Scheme


公众号“编程之路从0到1”

20190301102949549

Copyright © Arcticfox 2021 all right reserved,powered by Gitbook文档修订于: 2022-05-01 12:20:20

results matching ""

    No results matching ""