Objective C中PubNub获取在线用户列表始终为nil问题求助
Hey there, I’ve tackled this exact headache before—let’s walk through the fixes that usually get hereNowForChannel working properly again.
First, let’s align on your setup to make sure we’re troubleshooting the right pieces:
- You’ve got Pub/Sub keys with Presence enabled, registered your client successfully
- Subscribed to "My channel" and can send/receive messages without issues
- Spun up 2 more clients via the PubNub debug console, cross-client messaging works
- Confirmed Presence is toggled on in the admin dashboard
- But when calling
[client hereNowForChannel:@"My channel" withVerbosity:PNHereNowState completion:^(PNHereNowResult *result, PNErrorStatus *status) { ... }], the result comes back nil every time
Let’s break down the most common fixes step by step:
1. Double-Check Presence’s Admin Dashboard Configuration
Even though you said you enabled it, let’s rule out tiny oversights:
- Make sure the Presence toggle is definitely ON for your app in the PubNub admin panel
- Confirm you haven’t restricted Presence to specific channels (check channel-level settings if you have custom configurations)
- Verify the "Presence Duration" is set to a reasonable value (the default 300 seconds works for most cases)
2. Ensure Your Client Subscribes With Presence Enabled
This is the #1 mistake I see! When subscribing to your channel, you need to explicitly enable Presence—just subscribing alone isn’t enough. Use this syntax:
[self.client subscribeToChannels:@[@"My channel"] withPresence:YES];
If you’re on a newer SDK version, the syntax might shift a bit, but the core rule stays: you have to opt into Presence during subscription. Without this, your client won’t send presence events, and hereNow can’t pick it up.
3. Test With a Simpler Verbosity Setting
You’re using PNHereNowState (which fetches user state along with the UUID list), but let’s narrow things down by testing with PNHereNowUUIDs first:
[self.client hereNowForChannel:@"My channel" withVerbosity:PNHereNowUUIDs completion:^(PNHereNowResult *result, PNErrorStatus *status) { if (!status) { NSLog(@"Connected UUIDs: %@", result.data.uuids); } else { NSLog(@"Here Now Error: %@", status.errorData.info); } }];
If this still returns nil, the issue isn’t with state—it’s that presence isn’t being registered for the channel at all. If it returns data, then the problem might be that your clients don’t have state set.
4. Wait for Subscription Confirmation Before Calling hereNow
PubNub subscriptions are asynchronous. If you call hereNow right after subscribeToChannels, it might run before the subscription is fully confirmed. Wait for the subscription callback to fire first:
[self.client subscribeToChannels:@[@"My channel"] withPresence:YES]; [self.client addListener:self]; // In your listener delegate method: - (void)client:(PubNub *)client didReceiveSubscriptionStatus:(PNSubscriptionStatus *)status { if (status.category == PNConnectedCategory) { // Now it's safe to call hereNow [self.client hereNowForChannel:@"My channel" withVerbosity:PNHereNowState completion:^(PNHereNowResult *result, PNErrorStatus *status) { // Handle your result here }]; } }
5. Validate Debug Console Clients
When you created clients via the debug console, did you enable Presence for them? The console has a toggle for Presence when connecting a client—if you left that off, those clients won’t show up in hereNow results. Double-check that toggle for each debug client.
6. Update Your PubNub SDK
Older versions of the Objective-C SDK had occasional bugs with hereNow calls. Try updating to the latest stable version (via CocoaPods or manual download from the PubNub repo) to rule out version-related issues.
7. Inspect the Error Status Object
Don’t ignore the PNErrorStatus in your completion block! Even if the result is nil, the status will have clues:
completion:^(PNHereNowResult *result, PNErrorStatus *status) { if (status) { NSLog(@"Error code: %ld", (long)status.category); NSLog(@"Error details: %@", status.errorData.info); } }
Common issues here include typos in the channel name (PubNub channels are case-sensitive!) or key restrictions that block Presence access.
Most of the time, the fix is either forgetting to enable Presence during subscription or calling hereNow too early. Give these steps a shot—let me know if any of them get your user list showing up!
内容的提问来源于stack exchange,提问作者Mục Đồng

