IOS9 那些坑

网络请求

webView 中 调用的NSURL 为 http 时,webView将无法正常显示

因为安全原因 http 无法正常加载,IOS9以上必须使用https (https = http + (安全机制) TSL/SSL)

解决方法:
1. Server https

服务器端更改http为https

2. App 安全机制
打开Info.plist加入以下代码(Source Code)

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
NSAppTransportSecurity:  修改当前App的安全机制
NSAllowsArbitraryLoads:允许使用以前的非安全的http机制加载

定位功能

IOS9中可以允许一个App使用多个LocationManager, 多个LocationManager可以分类处理,一部分LocationManager前台处理,一部分LocationManager后台处理。

import UIKit
import CoreLocation

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        if #available(iOS 9.0, *) {
            locationManager.allowsBackgroundLocationUpdates = true
        } else {
            //#available(iOS 8.0, *)
            locationManager.requestAlwaysAuthorization()
        }

        locationManager.startUpdatingLocation()
    }

}

以上代码在IOS9中会崩溃,需要加入在info.plist中加入以下代码

<key>NSLocationAlwaysUsageDescription</key>
    <string>LocationDemo</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>location</string>
    </array>
<think>好的,我现在需要帮用户解决iOS页面适配安全区的代码实现问题。用户提到了使用Safe Area Layout Guides,所以我要围绕这个主题展开。 首先,我得回忆一下iOS中安全区域的概念。安全区域是iOS 11引入的,用来处理刘海屏等设备的布局问题。之前的版本可能需要用topLayoutGuide和bottomLayoutGuide,但现在苹果推荐用Safe Area Layout Guides。不过用户可能还需要适配旧系统,比如iOS 9iOS 8,所以得考虑兼容性问题。 接下来,我需要确定如何在代码中使用Safe Area Layout Guides。通常,这涉及到NSLayoutConstraint的约束设置,或者使用UIView的safeAreaLayoutGuide属性。可能还要处理导航栏隐藏的情况,因为隐藏导航栏会影响安全区域的顶部边距。 然后,用户需要代码示例。我应该提供两种方式:纯代码实现和Storyboard结合代码的示例。比如,如何以编程方式添加约束到安全区域,或者如何在Storyboard中设置约束后,在代码中调整。 还要注意,对于iOS 11之前的版本,可能需要使用topLayoutGuide和bottomLayoutGuide,但用户现在可能不需要支持那么旧的版本了,但为了全面性,可以提一下兼容性处理的方法,比如检查是否存在safeAreaLayoutGuide属性。 另外,用户提供的引用中提到了UILayoutGuide和Xcode9的变化,需要确保代码示例符合这些信息。比如,在Storyboard中使用Safe Area,或者在代码中激活约束时使用safeAreaLayoutGuide的锚点。 可能还需要提到常见的,比如当视图控制器的edgesForExtendedLayout设置为默认值时,安全区域的行为,或者如何正确处理滚动视图的内容插入。 最后,生成相关问题的时候,要考虑用户可能进一步的问题,比如动态调整安全区域、处理键盘出现时的布局变化,或者与ScrollView的结合使用。这些问题可以帮助用户更深入地理解安全区的应用。</think>### iOS安全区适配代码实现 根据苹果官方文档和开发实践,iOS安全区适配的核心是使用`safeAreaLayoutGuide`属性,以下是三种典型场景的代码示例: #### 1. 纯代码布局实现 ```swift // 创建红色背景视图 let redView = UIView() redView.backgroundColor = .red view.addSubview(redView) // 禁用自动转换 redView.translatesAutoresizingMaskIntoConstraints = false // 获取安全区域布局指南 let safeGuide = view.safeAreaLayoutGuide // 设置约束 NSLayoutConstraint.activate([ redView.leadingAnchor.constraint(equalTo: safeGuide.leadingAnchor, constant: 20), redView.trailingAnchor.constraint(equalTo: safeGuide.trailingAnchor, constant: -20), redView.topAnchor.constraint(equalTo: safeGuide.topAnchor, constant: 15), redView.bottomAnchor.constraint(equalTo: safeGuide.bottomAnchor, constant: -15) ]) ``` #### 2. Storyboard结合代码适配 ```swift override func viewDidLoad() { super.viewDidLoad() // 创建蓝色视图 let blueView = UIView() blueView.backgroundColor = .blue view.addSubview(blueView) // 自动布局设置 blueView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ blueView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), blueView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor), blueView.heightAnchor.constraint(equalToConstant: 100), blueView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor) ]) } ``` #### 3. 兼容iOS11以下版本 ```swift extension UIView { var compatibleSafeArea: UILayoutGuide { if #available(iOS 11.0, *) { return self.safeAreaLayoutGuide } else { // 创建自定义布局指南 let guide = UILayoutGuide() self.addLayoutGuide(guide) NSLayoutConstraint.activate([ guide.topAnchor.constraint(equalTo: self.topAnchor, constant: topLayoutGuide.length), guide.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -bottomLayoutGuide.length), guide.leadingAnchor.constraint(equalTo: self.leadingAnchor), guide.trailingAnchor.constraint(equalTo: self.trailingAnchor) ]) return guide } } } // 使用示例 let greenView = UIView() greenView.backgroundColor = .green view.addSubview(greenView) greenView.translatesAutoresizingMaskIntoConstraints = false let guide = view.compatibleSafeArea NSLayoutConstraint.activate([ greenView.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 30), greenView.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -30), greenView.topAnchor.constraint(equalTo: guide.topAnchor), greenView.bottomAnchor.constraint(equalTo: guide.bottomAnchor) ]) ``` ### 关键配置说明 1. **视图控制器配置**: ```swift // 禁用自动边距调整(默认已启用) if #available(iOS 11.0, *) { self.additionalSafeAreaInsets = .zero } else { self.edgesForExtendedLayout = [] } ``` 2. **导航栏适配**: ```swift // 隐藏导航栏时需调整安全区 navigationController?.setNavigationBarHidden(true, animated: false) if #available(iOS 11.0, *) { additionalSafeAreaInsets.top = -view.safeAreaInsets.top } ``` 3. **滚动视图适配**: ```swift tableView.contentInsetAdjustmentBehavior = .always // 自动调整内容边距 collectionView.contentInset = UIEdgeInsets( top: view.safeAreaInsets.top, left: 0, bottom: view.safeAreaInsets.bottom, right: 0 ) ``` ### 注意事项 1. 在Xcode 9+的Interface Builder中: - 拖动约束时选择"Safe Area"作为参照物 - 检查视图层次结构中是否存在安全区域指南 - 避免同时使用`topLayoutGuide`和`safeAreaLayoutGuide` 2. 调试技巧: ```swift // 打印安全区域信息 override func viewSafeAreaInsetsDidChange() { super.viewSafeAreaInsetsDidChange() print("更新后的安全区域:\(view.safeAreaInsets)") } ``` [^1]: iOS安全区域适配机制最早在WWDC2017中提出,支持iPhone X及后续全面屏设备 [^2]: 苹果开发者文档建议所有新项目都应使用安全区域布局指南进行界面设计
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值