คลาส GNSPermission
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คลาสนี้ช่วยให้คุณจัดการสถานะสิทธิ์ใกล้เคียงสำหรับแอปในอุปกรณ์ปัจจุบันได้
ผู้ใช้ต้องให้สิทธิ์ก่อนจึงจะเผยแพร่และการสมัครใช้บริการได้ ในครั้งแรกที่แอปพลิเคชันสร้างสื่อเผยแพร่หรือการสมัครใช้บริการ กล่องโต้ตอบสิทธิ์ใกล้เคียงจะปรากฏขึ้นโดยอัตโนมัติ เพื่อเปิดโอกาสให้ผู้ใช้ได้เลือกรับ หากผู้ใช้ปฏิเสธ ระบบจะแสดงการแจ้งเตือนทุกครั้งที่มีการสร้างสื่อเผยแพร่หรือการสมัครใช้บริการในเวลาต่อมา เพื่อให้ผู้ใช้มีโอกาสเลือกรับอีกครั้ง
ขอแนะนำให้แอประบุวิธีการเปิดหรือปิดสิทธิ์ให้กับผู้ใช้ เช่น โดยใช้สวิตช์เปิด/ปิดในหน้าการตั้งค่า เมื่อผู้ใช้สลับดู แอปควรเรียกใช้เมธอด isGranted
และ setGranted
ด้านล่างเพื่อสลับสถานะของสิทธิ์
หากต้องการติดตามสถานะสิทธิ์ ให้สร้างออบเจ็กต์ GNSPermission
แล้วส่งในเครื่องจัดการที่เปลี่ยนแปลง ระบบจะเรียกเครื่องจัดการเมื่อใดก็ตามที่สถานะของสิทธิ์มีการเปลี่ยนแปลง ซึ่งจะทำให้ UI ของแอปซิงค์กับสถานะปัจจุบันอยู่เสมอ
รับค่า NSObject
- (instancetype) initWithChangedHandler: |
|
(GNSPermissionHandler) |
changedHandler |
|
เริ่มต้นออบเจ็กต์สิทธิ์ด้วยตัวแฮนเดิลที่จะเรียกใช้เมื่อมีการเปลี่ยนแปลงสถานะสิทธิ์
ตัวแฮนเดิลจะอนุญาตให้แอปซิงค์ UI กับสถานะสิทธิ์ ระบบจะไม่เรียกเครื่องจัดการจนกว่าผู้ใช้จะอนุมัติหรือปฏิเสธสิทธิ์
- Parameters
-
handler | The permission granted handler |
สิทธิ์ "ใกล้เคียง" ได้รับอนุญาตสำหรับแอปในอุปกรณ์นี้หรือไม่
"ใช่" หมายความว่าผู้ใช้ได้ให้สิทธิ์ที่อยู่ใกล้เคียง แต่ "ไม่" หมายความว่าผู้ใช้ได้ปฏิเสธการให้สิทธิ์ หากยังไม่ได้รับสิทธิ์ สื่อเผยแพร่และการสมัครใช้บริการจะไม่ทำงาน
+ (void) setGranted: |
|
(BOOL) |
granted |
|
เปลี่ยนสถานะสิทธิ์สถานที่ใกล้เคียง
ซึ่งควรเรียกเพื่อตอบสนองต่อการดำเนินการของผู้ใช้เมื่อมีการเปลี่ยนสถานะสิทธิ์เท่านั้น
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-13 UTC
[null,null,["อัปเดตล่าสุด 2025-08-13 UTC"],[[["\u003cp\u003eThe \u003ccode\u003eGNSPermission\u003c/code\u003e class manages the Nearby permission state for your app, enabling features like publications and subscriptions.\u003c/p\u003e\n"],["\u003cp\u003eUsers must grant Nearby permission for these features to function; the system automatically requests this permission when needed.\u003c/p\u003e\n"],["\u003cp\u003eYou can track and react to permission state changes using a \u003ccode\u003eGNSPermission\u003c/code\u003e object with a change handler.\u003c/p\u003e\n"],["\u003cp\u003eProvide a UI element, like a toggle switch, allowing users to control the Nearby permission and update the state using \u003ccode\u003eisGranted\u003c/code\u003e and \u003ccode\u003esetGranted\u003c/code\u003e methods.\u003c/p\u003e\n"],["\u003cp\u003eThe initial permission request is automatic, but subsequent requests depend on the user's previous choice and your app's UI for toggling the permission.\u003c/p\u003e\n"]]],[],null,["# GNSPermission Class\n\n[Instance Methods](#pub-methods) \\| [Class Methods](#pub-static-methods) \nGNSPermission Class Reference \n\nOverview\n--------\n\nThis class lets you manage the Nearby permission state for the app on the current device.\n\nThe user must grant permission before publications and subscriptions will work. The first time the application creates a publication or subscription, the Nearby permission dialog is automatically displayed, giving the user the opportunity to opt in. If the user denies, an alert will be displayed each time a publication or subscription is subsequently created, giving them another chance to opt in.\n\nIt is recommended that your app provide the user a method for switching the permission on or off; for instance, by using a toggle switch in a settings page. When the user toggles it, the app should call the `isGranted` and `setGranted` methods below to toggle the permission state.\n\nTo track the permission state, create a [GNSPermission](/nearby/messages/ios/reference/interface_g_n_s_permission \"This class lets you manage the Nearby permission state for the app on the current device...\") object and pass in a changed handler. The handler will be called whenever the permission state is changed, allowing your app's UI to be kept in sync with the current state.\n\nInherits NSObject.\n\n|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Instance Method Summary ----------------------- ||\n| (instancetype) | - [initWithChangedHandler:](/nearby/messages/ios/reference/interface_g_n_s_permission#a28317128a0274fd2f24c8e21456f2276) |\n| | Initializes the permission object with a handler that is called whenever the permission state changes. [More...](#a28317128a0274fd2f24c8e21456f2276) |\n| ||\n\n|--------|--------------------------------------------------------------------------------------------------------------------------|\n| Class Method Summary -------------------- ||\n| (BOOL) | + [isGranted](/nearby/messages/ios/reference/interface_g_n_s_permission#aef0ec5cb0489597c5e90a7af81b4b574) |\n| | Whether Nearby permission is currently granted for the app on this device. [More...](#aef0ec5cb0489597c5e90a7af81b4b574) |\n| ||\n| (void) | + [setGranted:](/nearby/messages/ios/reference/interface_g_n_s_permission#a115c4964d2f1b5317bf888cb40b6e0bf) |\n| | Changes the Nearby permission state. [More...](#a115c4964d2f1b5317bf888cb40b6e0bf) |\n| ||\n\nMethod Detail\n-------------\n\n|------------------------------------------|---|------------------------|------------------|---|\n| - (instancetype) initWithChangedHandler: | | (GNSPermissionHandler) | *changedHandler* | |\n\nInitializes the permission object with a handler that is called whenever the permission state changes.\n\nThe handler lets the app keep its UI in sync with the permission state. The handler is not called until the user has approved or denied permission.\n\nParameters\n:\n\n |---------|--------------------------------|\n | handler | The permission granted handler |\n\n\n|--------------------|---|---|---|\n| + (BOOL) isGranted | | | |\n\nWhether Nearby permission is currently granted for the app on this device.\n\nYES means the user has granted Nearby permission, and NO means the user has denied permission. If permission has not been granted, publications and subscriptions will not work. \n\n|----------------------|---|--------|-----------|---|\n| + (void) setGranted: | | (BOOL) | *granted* | |\n\nChanges the Nearby permission state.\n\nThis should be called only in response to the user action of changing the permission state."]]