Swift学习笔记26-使用第三方库

找到一个SDK,按照其文档安装第三方库。以高德地图SDK为例。

https://lbs.amap.com/api/ios-sdk/gettingstarted

获取 CocoaPods(第三方库管理器):使用Homebrew安装CocoaPods

需要注意网络状态是否畅通。

https://juejin.cn/post/7199540426242605112

  • 终端相关命令:创建文件

    cd 命令更改到想要创建文件的目录

    复制代码
    cd /Users/sakiko/Desktop/lianxi1/lianxi1/class3/class4/class6/clss8/UIkit_lianxi/lianxi2026_5_08

    torch创建文件

    复制代码
    touch Podfile

    用nano编辑文件内容,然后按control+x退出,记得保存

    复制代码
    nano Podfile

    用cat查看文件内容

    复制代码
    cat Podfile
安装好SDK之后,获取Key。获取好之后,要允许 HTTPS访问

注意,只要安装了第三方库,以后就只能从workplace访问该APP了。

然后,允许 HTTPS访问,按照以下步骤添加配置:

  1. 添加一个新的键,选择 "App Transport Security Settings"(如果使用的是 Xcode 的可视化编辑器,可以右键单击,然后选择"添加行")。
  2. 在此设置中,添加一个新的子键 "Allow Arbitrary Loads" ,将其值设置为 YES

这些库都是OC代码,需要创建一个Cocoa Touch Class,来让系统自动生成头文件。

再把刚创建的这两个文件删掉,留下头文件lianxi2026_5_08-Bridging-Header

这些内容要写在头文件里

objectivec 复制代码
#import <AMapFoundationKit/AMapFoundationKit.h>

输入Key的代码是OC,将其转换为Swift代码

objectivec 复制代码
 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
    [AMapServices sharedServices].apiKey = @"您的Key";
     
    ......
}

↓这个最好在Appdelegate里配置

Swift 复制代码
AMapServices.shared().apiKey = "你的key"

使用sdk,创建地图,示例代码是OC,将它转成Swift

Swift 复制代码
#import <MAMapKit/MAMapKit.h>

-(void) viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

///初始化地图
MAMapView *_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    
///把地图添加至view
    [self.view addSubview:_mapView];
}

#import <MAMapKit/MAMapKit.h>也要写在头文件里

创建新的一页地图页来管理地图,添加地图组件。

Swift 复制代码
import UIKit
class MapViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(mapView)
        // Do any additional setup after loading the view.
    }
    lazy var mapView: MAMapView = {
        let mapView = MAMapView(frame: .init(origin: .zero, size: .init(width: SCREENWIDTH, height: SCREENHEIGHT - 200)))
        return mapView
    }()
}

设置索引为2-位置时的事件,跳转进该页面。

Swift 复制代码
    lazy var moreView: MoreView = {
        let moreView = MoreView(frame: .init(x: 0, y: SCREENHEIGHT - 98 - bottomSafeArea, width: SCREENWIDTH, height: 98 + bottomSafeArea))
        moreView.backgroundColor = .lightGray
        moreView.btnClick = { [weak self] idx in
            print(idx)
            if idx == 0 {
                self?.openPhotoLib()
            } else if idx == 1 {
                self?.openCamera()
            } else if idx == 2 {
                let mapVC = MapViewController()
                self?.navigationController?.pushViewController(mapVC, animated: true)
                
            }
        }

这时候会发现有个错误:内容如下

Sandbox: bash(73442) deny(1) file-write-create /Users/sakiko/Desktop/lianxi1/ lianxi1/class3/class4/class6/clss8/Ulkit_lianxi/lianxi2026_5_08/Pods/resources-to- copy-lianxi2026_5_08

这是因为系统的安全沙盒(Sandbox)阻止了脚本在指定的目录下创建或写入文件。

解决方法:

  • 在 Xcode 左侧的工程导航栏中,点击最顶部的蓝色工程文件
  • 在右侧的主视图中,选择 Build Settings 选项卡。
  • 在搜索框(Search)中输入:ENABLE_USER_SCRIPT_SANDBOXING
  • 你会看到一个名为 "User Script Sandboxing" 的设置项。
  • 将它的值从 Yes 改为 No
相关推荐
东坡肘子5 天前
Swift 还让你 Excited 吗?-- 肘子的 Swift 周报 #141
人工智能·swiftui·swift
LinXunFeng6 天前
Obsidian - 使用 Share Note 分享笔记并自部署
前端·笔记·github
sweet丶8 天前
Swift 元编程-Macro
swift
通信小呆呆11 天前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人
H__Rick11 天前
自动对焦学习-3
人工智能·学习·计算机视觉
Daisy Lee11 天前
量化学习-第1章-什么是量化金融
学习·金融·datawhale
Alsn8611 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
YM52e11 天前
买菜计算器小应用 - HarmonyOS ArkUI 开发实战-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
小雨下雨的雨11 天前
HarmonyOS ArkUI训练营入门-组件掌握系列-Animation 动画效果实现-PC版本
学习·华为·harmonyos·鸿蒙
闪闪发亮的小星星11 天前
高斯光以及高斯光公式解释
笔记