{"id":7230,"date":"2025-05-31T12:16:21","date_gmt":"2025-06-05T04:37:20","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/uncategorized\/https-badgameshow-com-steven-288\/"},"modified":"2025-06-05T12:16:21","modified_gmt":"2025-06-05T04:37:20","slug":"https-badgameshow-com-steven-288","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-288\/","title":{"rendered":"Swift 2025 \u6700\u65b0\u7248\uff1a\u5be6\u73fe\u4e8c\u7dad\u78bc\u8207\u689d\u78bc\u8b80\u53d6\u7684\u5b8c\u6574\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u4e8c\u7dad\u78bc, \u689d\u78bc\u8b80\u53d6, AVFoundation, iOS \u958b\u767c\"><\/p>\n<h1>Swift 2025 \u6700\u65b0\u7248\uff1a\u5be6\u73fe\u4e8c\u7dad\u78bc\u8207\u689d\u78bc\u8b80\u53d6\u7684\u5b8c\u6574\u6559\u5b78<\/h1>\n<p>\u8fd1\u5e74\u4f86\uff0c\u4e8c\u7dad\u78bc\u548c\u689d\u78bc\u8b80\u53d6\u5728\u5404\u7a2e\u61c9\u7528\u4e0a\u8d8a\u4f86\u8d8a\u666e\u53ca\uff0c\u4f8b\u5982\u5546\u54c1\u6383\u63cf\u3001\u8eab\u4efd\u8b49\u8b80\u53d6\u7b49\u3002Swift \u4f5c\u70ba\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\u4f86\u5be6\u73fe\u9019\u4e9b\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u5be6\u73fe\u4e8c\u7dad\u78bc\u548c\u689d\u78bc\u7684\u8b80\u53d6\u529f\u80fd\u3002<\/p>\n<h2>\u4f7f\u7528 AVFoundation \u5be6\u73fe\u4e8c\u7dad\u78bc\u8207\u689d\u78bc\u8b80\u53d6<\/h2>\n<p>AVFoundation \u662f Apple \u63d0\u4f9b\u7684\u6846\u67b6\uff0c\u53ef\u4ee5\u7528\u4f86\u8655\u7406\u5f71\u50cf\u548c\u97f3\u8a0a\uff0c\u540c\u6642\u4e5f\u652f\u63f4\u4e8c\u7dad\u78bc\u548c\u689d\u78bc\u7684\u8b80\u53d6\u3002\u6211\u5011\u5c07\u901a\u904e\u4ee5\u4e0b\u6b65\u9a5f\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u8a2d\u5b9a Info.plist<\/h3>\n<p>\u9996\u5148\uff0c\u5728\u60a8\u7684 Xcode \u5c08\u6848\u4e2d\uff0c\u6253\u958b <strong>info.plist<\/strong> \u6587\u4ef6\uff0c\u4e26\u52a0\u5165 <strong>Privacy &#8211; Camera Usage Description<\/strong>\uff0c\u4ee5\u4fbf\u8b93\u4f7f\u7528\u8005\u77e5\u9053\u6211\u5011\u7684 App \u5c07\u6703\u4f7f\u7528\u76f8\u6a5f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/K6yV6VU.png\" alt=\"info.plist\" title=\"\u8a2d\u5b9a Info.plist \u4ee5\u4f7f\u7528\u76f8\u6a5f\"><\/p>\n<h3>\u6b65\u9a5f 2\uff1a\u5efa\u7acb AVCaptureSession<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b <strong>AVCaptureSession<\/strong>\uff0c\u9019\u662f\u6574\u500b\u8b80\u53d6\u904e\u7a0b\u7684\u6838\u5fc3\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let captureSession = AVCaptureSession()\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 3\uff1a\u8a2d\u7f6e\u76f8\u6a5f\u8f38\u5165<\/h3>\n<p>\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b <strong>AVCaptureDevice<\/strong>\uff0c\u4ee5\u4fbf\u53d6\u5f97\u76f8\u6a5f\u7684\u8cc7\u8a0a\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }\nlet videoInput: AVCaptureDeviceInput\n\ndo {\n    videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)\n} catch {\n    return\n}\n\nif captureSession.canAddInput(videoInput) {\n    captureSession.addInput(videoInput)\n} else {\n    failed()\n    return\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 4\uff1a\u8a2d\u7f6e\u8f38\u51fa\u5143\u6578\u64da<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u8a2d\u7f6e <strong>AVCaptureMetadataOutput<\/strong>\uff0c\u4ee5\u4fbf\u5c07\u8b80\u53d6\u5230\u7684\u4e8c\u7dad\u78bc\u548c\u689d\u78bc\u8cc7\u8a0a\u8f38\u51fa\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let metadataOutput = AVCaptureMetadataOutput()\n\nif captureSession.canAddOutput(metadataOutput) {\n    captureSession.addOutput(metadataOutput)\n\n    metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)\n    metadataOutput.metadataObjectTypes = [.qr, .ean13, .ean8, .code128]\n} else {\n    failed()\n    return\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 5\uff1a\u555f\u52d5 AVCaptureSession<\/h3>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u555f\u52d5 <strong>AVCaptureSession<\/strong>\uff0c\u958b\u59cb\u8b80\u53d6\u4e8c\u7dad\u78bc\u548c\u689d\u78bc\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">captureSession.startRunning()\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 6\uff1a\u8655\u7406\u8b80\u53d6\u7d50\u679c<\/h3>\n<p>\u7576 <strong>AVCaptureMetadataOutput<\/strong> \u6210\u529f\u8b80\u53d6\u5230\u4e8c\u7dad\u78bc\u6216\u689d\u78bc\u6642\uff0c\u5c07\u89f8\u767c <strong>didOutput<\/strong> \u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u9019\u500b\u65b9\u6cd5\u4e2d\u7372\u53d6\u8b80\u53d6\u5230\u7684\u8cc7\u8a0a\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {\n    captureSession.stopRunning()\n\n    if let metadataObject = metadataObjects.first {\n        guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }\n        guard let stringValue = readableObject.stringValue else { return }\n        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))\n        found(code: stringValue)\n    }\n\n    dismiss(animated: true)\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff0c\u4f8b\u5982\u76f8\u6a5f\u6b0a\u9650\u672a\u7372\u5f97\u3001\u6216\u662f\u7121\u6cd5\u8b58\u5225\u689d\u78bc\u7b49\u3002\u8acb\u78ba\u4fdd\uff1a<br \/>\n&#8211; \u60a8\u5df2\u5728 <strong>info.plist<\/strong> \u4e2d\u6b63\u78ba\u8a2d\u5b9a\u76f8\u6a5f\u4f7f\u7528\u6b0a\u9650\u3002<br \/>\n&#8211; \u78ba\u4fdd\u689d\u78bc\u6216\u4e8c\u7dad\u78bc\u6e05\u6670\u53ef\u898b\uff0c\u4e14\u5728\u76f8\u6a5f\u8996\u91ce\u7bc4\u570d\u5167\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u5c07\u8b80\u53d6\u5230\u7684\u4e8c\u7dad\u78bc\u6216\u689d\u78bc\u8cc7\u8a0a\u8207\u5f8c\u7aef\u4f3a\u670d\u5668\u9032\u884c\u4ea4\u4e92\uff0c\u6216\u662f\u5c07\u5176\u5132\u5b58\u5230\u672c\u5730\u8cc7\u6599\u5eab\u4e2d\uff0c\u4ee5\u4fbf\u5f8c\u7e8c\u67e5\u8a62\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift 2025 \u6700\u65b0\u8a9e\u6cd5\u5be6\u73fe\u4e8c\u7dad\u78bc\u548c\u689d\u78bc\u7684\u8b80\u53d6\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u500b\u8a73\u7d30\u7684\u6b65\u9a5f\u6d41\u7a0b\u3002\u5982\u679c\u60a8\u60f3\u8981\u66f4\u8a73\u7d30\u7684\u7bc4\u4f8b\uff0c\u6b61\u8fce\u53c3\u8003 <a href=\"https:\/\/github.com\/appcoda\/QRCodeReader\" target=\"_blank\" rel=\"noopener\">AppCoda \u7684 QRCodeReader \u7bc4\u4f8b<\/a>\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u8655\u7406\u76f8\u6a5f\u6b0a\u9650\u672a\u7372\u5f97\u7684\u60c5\u6cc1\uff1f<\/h3>\n<p>\u78ba\u4fdd\u5728 <strong>info.plist<\/strong> \u4e2d\u52a0\u5165\u76f8\u6a5f\u4f7f\u7528\u63cf\u8ff0\uff0c\u4e26\u5728\u61c9\u7528\u7a0b\u5f0f\u555f\u52d5\u6642\u8acb\u4f7f\u7528\u8005\u6388\u6b0a\u76f8\u6a5f\u5b58\u53d6\u3002<\/p>\n<h3>2. \u8b80\u53d6\u689d\u78bc\u6642\u7121\u6cd5\u8b58\u5225\uff0c\u5982\u4f55\u89e3\u6c7a\uff1f<\/h3>\n<p>\u78ba\u4fdd\u689d\u78bc\u6216\u4e8c\u7dad\u78bc\u7684\u6e05\u6670\u5ea6\uff0c\u4e26\u6aa2\u67e5\u76f8\u6a5f\u662f\u5426\u6b63\u78ba\u5c0d\u6e96\u689d\u78bc\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u64f4\u5c55\u6b64\u529f\u80fd\u55ce\uff1f<\/h3>\n<p>\u7576\u7136\u53ef\u4ee5\uff0c\u60a8\u53ef\u4ee5\u5c07\u8b80\u53d6\u5230\u7684\u8cc7\u8a0a\u5132\u5b58\u5230\u8cc7\u6599\u5eab\u6216\u8207\u5f8c\u7aef\u9032\u884c\u4ea4\u4e92\uff0c\u751a\u81f3\u5be6\u73fe\u652f\u4ed8\u529f\u80fd\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5be6\u73fe\u4e8c\u7dad\u78bc-\u689d\u78bc\u8b80\u53d6-\ud83d\udd0d_1675409412.355361.webp\" alt=\"Swift \u5be6\u73fe\u4e8c\u7dad\u78bc\/\u689d\u78bc\u8b80\u53d6 \ud83d\udd0d\" title=\"Swift \u4e8c\u7dad\u78bc\u8b80\u53d6\u3001Swift \u689d\u78bc\u8b80\u53d6\u3001Swift \u8b80\u53d6\u529f\u80fd\u3001Swift methods() \u51fd\u6578\u3001Swift \u4e8c\u7dad\u78bc\u689d\u78bc\u8b80\u53d6\" width=\"1200\" height=\"628\"\/><br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4f86\u8b80\u53d6\u4e8c\u7dad\u78bc\/\u689d\u78bc\uff0c\u4e26\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684methods()\u51fd\u6578\u4f86\u5be6\u73fe\u8b80\u53d6\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":7229,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5be6\u73fe\u4e8c\u7dad\u78bc-\u689d\u78bc\u8b80\u53d6-\ud83d\udd0d_1675409412.355361.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1SC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=7230"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7230\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7229"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}