Lớp GNSPermission
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Lớp này cho phép bạn quản lý trạng thái quyền sử dụng tính năng Chia sẻ lân cận của ứng dụng trên thiết bị hiện tại.
Người dùng phải cấp quyền thì các ấn bản và gói thuê bao mới hoạt động được. Lần đầu tiên ứng dụng tạo ấn bản hoặc gói thuê bao, hộp thoại cấp quyền ở gần đây sẽ tự động hiển thị để người dùng có cơ hội chọn sử dụng tính năng này. Nếu người dùng từ chối, một cảnh báo sẽ hiển thị mỗi khi người dùng tạo ấn bản hoặc gói thuê bao sau đó, giúp họ có thêm cơ hội chọn sử dụng tính năng này.
Ứng dụng của bạn nên cung cấp cho người dùng một phương thức để bật hoặc tắt quyền; ví dụ: bằng cách sử dụng nút bật/tắt trên trang cài đặt. Khi người dùng bật/tắt quyền này, ứng dụng sẽ gọi phương thức isGranted
và setGranted
bên dưới để chuyển đổi trạng thái cấp quyền.
Để theo dõi trạng thái quyền, hãy tạo một đối tượng GNSPermission
và truyền vào một trình xử lý đã thay đổi. Trình xử lý sẽ được gọi bất cứ khi nào trạng thái quyền được thay đổi, cho phép giao diện người dùng của ứng dụng luôn đồng bộ với trạng thái hiện tại.
Kế thừa NSObject.
- (instancetype) initWithChangedHandler: |
|
(GNSPermissionHandler) |
changedHandler |
|
Khởi động đối tượng quyền bằng một trình xử lý được gọi mỗi khi trạng thái quyền thay đổi.
Trình xử lý cho phép ứng dụng đồng bộ hoá giao diện người dùng với trạng thái cấp quyền. Trình xử lý sẽ không được gọi cho đến khi người dùng đã phê duyệt hoặc từ chối cấp quyền.
- Parameters
-
handler | The permission granted handler |
Liệu quyền Lân cận hiện có được cấp cho ứng dụng trên thiết bị này hay không.
YES có nghĩa là người dùng đã cấp quyền sử dụng tính năng Chia sẻ lân cận và NO có nghĩa là người dùng đã từ chối cấp quyền. Nếu chưa được cấp quyền, các ấn bản và gói thuê bao sẽ không hoạt động.
+ (void) setGranted: |
|
(BOOL) |
granted |
|
Thay đổi trạng thái của quyền sử dụng tính năng Chia sẻ lân cận.
Lệnh này chỉ được gọi để phản hồi hành động của người dùng là thay đổi trạng thái quyền.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-13 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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."]]