Skip to content

Commit 9287d32

Browse files
authored
Merge pull request #6 from dustturtle/copilot/add-oc-demo-for-sdk
Add Objective-C iOS demo app (UIKit) matching the Swift demo
2 parents 9fc942e + 9f15a59 commit 9287d32

21 files changed

Lines changed: 1775 additions & 3 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ DerivedData/
77
.netrc
88
*.xcodeproj
99
!Examples/iOSDemo/iOSDemo.xcodeproj
10+
!Examples/iOSDemoObjC/iOSDemoObjC.xcodeproj
1011
_codeql_detected_source_root

Examples/iOSDemoObjC/iOSDemoObjC.xcodeproj/project.pbxproj

Lines changed: 408 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// AppDelegate.h
3+
// iOSDemoObjC
4+
//
5+
6+
#import <UIKit/UIKit.h>
7+
8+
@interface AppDelegate : UIResponder <UIApplicationDelegate>
9+
10+
@property (strong, nonatomic) UIWindow *window;
11+
12+
@end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// AppDelegate.m
3+
// iOSDemoObjC
4+
//
5+
6+
#import "AppDelegate.h"
7+
#import "MainViewController.h"
8+
9+
@implementation AppDelegate
10+
11+
- (BOOL)application:(UIApplication *)application
12+
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
13+
14+
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
15+
16+
MainViewController *mainVC = [[MainViewController alloc] initWithStyle:UITableViewStyleInsetGrouped];
17+
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
18+
19+
self.window.rootViewController = nav;
20+
[self.window makeKeyAndVisible];
21+
22+
return YES;
23+
}
24+
25+
@end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"colors" : [
3+
{
4+
"idiom" : "universal"
5+
}
6+
],
7+
"info" : {
8+
"author" : "xcode",
9+
"version" : 1
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"platform" : "ios",
6+
"size" : "1024x1024"
7+
}
8+
],
9+
"info" : {
10+
"author" : "xcode",
11+
"version" : 1
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// MainViewController.h
3+
// iOSDemoObjC
4+
//
5+
// Navigation hub — equivalent of ContentView.swift in the Swift demo.
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
10+
@interface MainViewController : UITableViewController
11+
12+
@end
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// MainViewController.m
3+
// iOSDemoObjC
4+
//
5+
6+
#import "MainViewController.h"
7+
#import "StreamBufferDemoViewController.h"
8+
#import "SSEParserDemoViewController.h"
9+
#import "UTF8SafetyDemoViewController.h"
10+
#import "SocketConnectionDemoViewController.h"
11+
12+
typedef NS_ENUM(NSInteger, DemoSection) {
13+
DemoSectionCore = 0,
14+
DemoSectionSocket,
15+
DemoSectionCount
16+
};
17+
18+
@implementation MainViewController
19+
20+
- (void)viewDidLoad {
21+
[super viewDidLoad];
22+
self.title = @"NWAsyncSocket ObjC Demo";
23+
}
24+
25+
#pragma mark - UITableViewDataSource
26+
27+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
28+
return DemoSectionCount;
29+
}
30+
31+
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
32+
switch (section) {
33+
case DemoSectionCore: return @"Core Components";
34+
case DemoSectionSocket: return @"Socket";
35+
default: return nil;
36+
}
37+
}
38+
39+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
40+
switch (section) {
41+
case DemoSectionCore: return 3;
42+
case DemoSectionSocket: return 1;
43+
default: return 0;
44+
}
45+
}
46+
47+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
48+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
49+
if (!cell) {
50+
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
51+
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
52+
}
53+
54+
UIListContentConfiguration *config = [UIListContentConfiguration cellConfiguration];
55+
56+
if (indexPath.section == DemoSectionCore) {
57+
switch (indexPath.row) {
58+
case 0:
59+
config.text = @"StreamBuffer";
60+
config.image = [UIImage systemImageNamed:@"arrow.left.arrow.right"];
61+
break;
62+
case 1:
63+
config.text = @"SSE Parser";
64+
config.image = [UIImage systemImageNamed:@"antenna.radiowaves.left.and.right"];
65+
break;
66+
case 2:
67+
config.text = @"UTF-8 Safety";
68+
config.image = [UIImage systemImageNamed:@"textformat"];
69+
break;
70+
}
71+
} else {
72+
config.text = @"Socket Connection";
73+
config.image = [UIImage systemImageNamed:@"network"];
74+
}
75+
76+
cell.contentConfiguration = config;
77+
return cell;
78+
}
79+
80+
#pragma mark - UITableViewDelegate
81+
82+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
83+
[tableView deselectRowAtIndexPath:indexPath animated:YES];
84+
85+
UIViewController *vc = nil;
86+
if (indexPath.section == DemoSectionCore) {
87+
switch (indexPath.row) {
88+
case 0:
89+
vc = [[StreamBufferDemoViewController alloc] initWithStyle:UITableViewStyleGrouped];
90+
break;
91+
case 1:
92+
vc = [[SSEParserDemoViewController alloc] initWithStyle:UITableViewStyleGrouped];
93+
break;
94+
case 2:
95+
vc = [[UTF8SafetyDemoViewController alloc] initWithStyle:UITableViewStyleGrouped];
96+
break;
97+
}
98+
} else {
99+
vc = [[SocketConnectionDemoViewController alloc] initWithStyle:UITableViewStyleGrouped];
100+
}
101+
102+
if (vc) {
103+
[self.navigationController pushViewController:vc animated:YES];
104+
}
105+
}
106+
107+
@end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// SocketManager.h
3+
// iOSDemoObjC
4+
//
5+
// Encapsulates GCDAsyncSocket operations — equivalent of SocketManager.swift
6+
// in the Swift demo.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "GCDAsyncSocket.h"
11+
#import "GCDAsyncSocketDelegate.h"
12+
13+
NS_ASSUME_NONNULL_BEGIN
14+
15+
/// Notification posted when SocketManager state changes (connection, logs, data).
16+
extern NSNotificationName const SocketManagerDidUpdateNotification;
17+
18+
@interface SocketManager : NSObject
19+
20+
@property (nonatomic, readonly) BOOL isConnected;
21+
@property (nonatomic, readonly, copy) NSArray<NSString *> *logs;
22+
@property (nonatomic, readonly, copy) NSData *receivedData;
23+
@property (nonatomic, readonly, copy) NSString *receivedText;
24+
@property (nonatomic, readonly, copy) NSArray<NWSSEEvent *> *sseEvents;
25+
26+
- (void)connectToHost:(NSString *)host
27+
port:(uint16_t)port
28+
useTLS:(BOOL)useTLS
29+
enableSSE:(BOOL)enableSSE
30+
enableStreaming:(BOOL)enableStreaming;
31+
32+
- (void)sendText:(NSString *)text;
33+
- (void)sendData:(NSData *)data;
34+
- (void)readData;
35+
- (void)disconnect;
36+
- (void)clearAll;
37+
38+
@end
39+
40+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)