GNSPermission 类
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
通过该类,您可以管理应用在当前设备上的“附近”权限状态。
用户必须授予权限,才能发布发布内容和订阅。当应用首次创建发布内容或订阅时,系统会自动显示“附近”权限对话框,以便用户选择启用。如果用户拒绝,则每次后续创建发布内容或订阅时都会显示提醒,让用户再次选择加入此计划。
建议您的应用为用户提供一种用于开启或关闭权限的方法,例如使用设置页面中的切换开关。当用户切换权限状态时,应用应调用下面的 isGranted
和 setGranted
方法来切换权限状态。
如需跟踪权限状态,请创建一个 GNSPermission
对象,并传入已更改的处理程序。每当权限状态发生变化时,系统都会调用处理程序,以便应用的界面与当前状态保持同步。
此类继承 NSObject。
- (instancetype) initWithChangedHandler: |
|
(GNSPermissionHandler) |
changedHandler |
|
使用每当权限状态发生变化时调用的处理程序来初始化权限对象。
该处理程序可让应用将其界面与权限状态保持同步。在用户批准或拒绝权限之前,系统不会调用处理程序。
- Parameters
-
handler | The permission granted handler |
当前是否已为此设备上的应用授予“附近”权限。
“是”表示用户已授予“附近”权限,“否”表示用户已拒绝该权限。如果未获得授权,则无法启动发布内容和订阅。
+ (void) setGranted: |
|
(BOOL) |
granted |
|
更改“附近分享”权限状态。
应仅在响应更改权限状态的用户操作时调用此方法。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):2025-08-13。"],[[["\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."]]