Skip to content

Commit b36e335

Browse files
committed
Added AuthRequestBuilder examples to iOS example apps
1 parent f477f77 commit b36e335

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

iOS Example Obj-C/iOS Example Obj-C/ViewController.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88

99
#import "ViewController.h"
1010

11+
@interface AuthRequestBuilder : NSObject <AuthRequestBuilderProtocol>
12+
13+
- (NSMutableURLRequest*)requestForSocketID:(NSString *)socketID channel:(PusherChannel *)channel;
14+
15+
@end
16+
17+
@implementation AuthRequestBuilder
18+
19+
- (NSMutableURLRequest*)requestForSocketID:(NSString *)socketID channel:(PusherChannel *)channel {
20+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [[NSURL alloc] initWithString:@"http://localhost:9292/pusher/auth"]];
21+
NSString *dataStr = [NSString stringWithFormat: @"socket_id=%@&channel_name=%@", socketID, [channel name]];
22+
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
23+
request.HTTPBody = data;
24+
request.HTTPMethod = @"POST";
25+
return request;
26+
}
27+
28+
@end
29+
1130
@interface ViewController ()
1231

1332
@end
@@ -20,6 +39,10 @@ - (void)viewDidLoad {
2039
OCAuthMethod *authMethod = [[OCAuthMethod alloc] initWithSecret:@"YOUR_APP_SECRET"];
2140
PusherClientOptions *options = [[PusherClientOptions alloc] initWithAuthMethod:authMethod];
2241

42+
// Use this if you want to try out your auth Endpoint
43+
// OCAuthMethod *endpointAuthMethod = [[OCAuthMethod alloc] initWithAuthRequestBuilder:[[AuthRequestBuilder alloc] init]];
44+
// PusherClientOptions *optionsWithEndpoint = [[PusherClientOptions alloc] initWithAuthMethod:endpointAuthMethod];
45+
2346
self.client = [[Pusher alloc] initWithAppKey:@"YOUR_APP_KEY" options:options];
2447
self.client.connection.delegate = self;
2548

@@ -88,3 +111,4 @@ - (void)didReceiveMemoryWarning {
88111
}
89112

90113
@end
114+

iOS Example Swift/iOS Example Swift/ViewController.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ class ViewController: UIViewController, PusherDelegate {
2525

2626
// Only use your secret here for testing or if you're sure that there's
2727
// no security risk
28-
let pusherClientOptions = PusherClientOptions(authMethod: .inline(secret: "daef58559fdd0aba8b63"))
29-
pusher = Pusher(key: "568d5a3851502158a022", options: pusherClientOptions)
28+
let pusherClientOptions = PusherClientOptions(authMethod: .inline(secret: "YOUR_APP_SECRET"))
29+
pusher = Pusher(key: "YOUR_APP_KEY", options: pusherClientOptions)
30+
31+
// Use this if you want to try out your auth endpoint
32+
// let optionsWithEndpoint = PusherClientOptions(
33+
// authMethod: AuthMethod.authRequestBuilder(authRequestBuilder: AuthRequestBuilder())
34+
// )
35+
// pusher = Pusher(key: "YOUR_APP_KEY", options: optionsWithEndpoint)
3036

3137
pusher.delegate = self
3238

@@ -77,3 +83,11 @@ class ViewController: UIViewController, PusherDelegate {
7783
}
7884
}
7985

86+
class AuthRequestBuilder: AuthRequestBuilderProtocol {
87+
func requestFor(socketID: String, channel: PusherChannel) -> URLRequest? {
88+
var request = URLRequest(url: URL(string: "http://localhost:9292/pusher/auth")!)
89+
request.httpMethod = "POST"
90+
request.httpBody = "socket_id=\(socketID)&channel_name=\(channel.name)".data(using: String.Encoding.utf8)
91+
return request
92+
}
93+
}

0 commit comments

Comments
 (0)