From 8343ba4f9eaad4d123f973f6a55503e9a8eeb6d3 Mon Sep 17 00:00:00 2001 From: Yurk <67189406+realYurkOfGitHub@users.noreply.github.com> Date: Fri, 12 Nov 2021 19:17:28 +0800 Subject: [PATCH] =?UTF-8?q?Delete=207.=20=E5=9F=BA=E6=9C=AC=E6=9D=83?= =?UTF-8?q?=E9=99=90=E4=B8=8Eurl=E8=AE=BF=E9=97=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../7. 基本权限与url访问.md | 92 ------------------- 1 file changed, 92 deletions(-) delete mode 100644 IOS/Task01:基础插件与功能/7. 基本权限与url访问.md diff --git a/IOS/Task01:基础插件与功能/7. 基本权限与url访问.md b/IOS/Task01:基础插件与功能/7. 基本权限与url访问.md deleted file mode 100644 index 518572d..0000000 --- a/IOS/Task01:基础插件与功能/7. 基本权限与url访问.md +++ /dev/null @@ -1,92 +0,0 @@ -# 基本权限与url访问 - -## 分享图片 - -我们的 app 肯定会用到各种权限,权限的申请也是开发者需要学习的一门课程,本章中提供了一些基本的权限请求方式,在后面的基础功能部分你将学习更多的内容。 - -此部分提供了分享照片的基本权限,我们可以通过一个 Button 来分享照片: - -```swift -@IBOutlet var imageView: UIView! -@IBAction func shareButton(_ sender: Any) { - guard let image = imageView else { return } - let activityController = UIActivityViewController(activityItems:[image],applicationActivities: nil) - activityController.popoverPresentationController?.sourceView = sender as? UIView present(activityController, animated: true, completion: nil) -} -``` - - - -## 网页链接 - -如果想要 `Button` 连接到浏览器,则需要对网页做出请求,在此前需要导入 `Safari Services` 头文件: - -```swift -import SafariServices - -@IBAction func Safari(_ sender: Any) { - if let url = URL(string: "http://www.bilibili.com") { - let safariViewController = SFSafariViewController(url:url) - present(safariViewController, animated: true,completion: nil) - } -} -``` - - - -## 提示与弹窗 - -如果想要获得照片权限,你需要一个弹窗,不仅如此,在选择头像的功能中,你也需要弹窗提示:从照片中选择、拍摄照片和取消三个部分: - -```swift -let alertController = UIAlertController(title: "Choose Image Source", message: nil, preferredStyle: .actionSheet) - -let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) -let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: {action in print("User selected Camera action") }) - -let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { action in print("User selected Photo Library action") }) - -alertController.addAction(cancelAction) -alertController.addAction(cameraAction) alertController.addAction(photoLibraryAction) - -alertController.popoverPresentationController?.sourceView = sender as? UIView present(alertController, animated: true, completion: nil) - -let alertController = UIAlertController(title: "Choose Image Source", message: nil, preferredStyle: .actionSheet) - -let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) -let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: { action in print("User selected Camera action") }) -let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { action in print("User selected Photo Library action") }) - -alertController.addAction(cancelAction) -alertController.addAction(cameraAction) alertController.addAction(photoLibraryAction) alertController.popoverPresentationController?.sourceView = sender as? UIView present(alertController, animated: true, completion: nil) -``` - - - -## 请求相机权限 - -iOS对相机以及其他各种权限的设置较为复杂,目前通用有两种手段获取相机权限:第一种通过 `ImagePickerController` 来实现,智能识别部分用到的就是这个,但是设置起来十分繁琐。不过好在还有第二种,通过 `Info` 中设置相关参数,直接访问即可,在 `Info.plist` 文件中添加: - -```swift -Privacy - Photo Library Additions Usage Description -``` - -赋值 `value`: - -```swift -To share photos from camera or photo library -``` - -相机请求: - -```swift -@IBAction func photosButtonTapped(_ sender: UIButton) {...} - -func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { - if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { - imageView.image = selectedImage - dismiss(animated: true, completion: nil) - } -} -``` -