|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +#import <UIKit/UIKit.h> |
| 7 | +#import "runtime.h" |
| 8 | + |
| 9 | +@interface ViewController : UIViewController |
| 10 | +@end |
| 11 | + |
| 12 | +@interface AppDelegate : UIResponder <UIApplicationDelegate> |
| 13 | +@property (strong, nonatomic) UIWindow *window; |
| 14 | +@property (strong, nonatomic) ViewController *controller; |
| 15 | +@end |
| 16 | + |
| 17 | +@implementation AppDelegate |
| 18 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
| 19 | + self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| 20 | + self.controller = [[ViewController alloc] initWithNibName:nil bundle:nil]; |
| 21 | + self.window.rootViewController = self.controller; |
| 22 | + [self.window makeKeyAndVisible]; |
| 23 | + return YES; |
| 24 | +} |
| 25 | +@end |
| 26 | + |
| 27 | +UILabel *label; |
| 28 | +void (*clickHandlerPtr)(void); |
| 29 | +void (*clickHandlerApplyUpdatePtr)(void); |
| 30 | + |
| 31 | +@implementation ViewController |
| 32 | + |
| 33 | +- (void)viewDidLoad { |
| 34 | + [super viewDidLoad]; |
| 35 | + |
| 36 | + label = [[UILabel alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| 37 | + label.textColor = [UIColor greenColor]; |
| 38 | + label.font = [UIFont boldSystemFontOfSize: 30]; |
| 39 | + label.numberOfLines = 2; |
| 40 | + label.textAlignment = NSTextAlignmentCenter; |
| 41 | + label.text = @"Hello, wire me up!\n(dllimport ios_set_text)"; |
| 42 | + [self.view addSubview:label]; |
| 43 | + |
| 44 | + UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark]; |
| 45 | + [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; |
| 46 | + [button setFrame:CGRectMake(50, 250, 200, 50)]; |
| 47 | + [button setTitle:@"Click me (wire me up)" forState:UIControlStateNormal]; |
| 48 | + [button setExclusiveTouch:YES]; |
| 49 | + [self.view addSubview:button]; |
| 50 | + |
| 51 | + UIButton *apply_button = [UIButton buttonWithType:UIButtonTypeInfoDark]; |
| 52 | + [apply_button addTarget:self action:@selector(applyUpdateButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; |
| 53 | + [apply_button setFrame:CGRectMake(50, 300, 200, 50)]; |
| 54 | + [apply_button setTitle:@"ApplyUpdate" forState:UIControlStateNormal]; |
| 55 | + [apply_button setExclusiveTouch:YES]; |
| 56 | + [self.view addSubview:apply_button]; |
| 57 | + |
| 58 | + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 59 | + mono_ios_runtime_init (); |
| 60 | + }); |
| 61 | +} |
| 62 | +-(void) buttonClicked:(UIButton*)sender |
| 63 | +{ |
| 64 | + if (clickHandlerPtr) |
| 65 | + clickHandlerPtr(); |
| 66 | +} |
| 67 | + |
| 68 | +-(void) applyUpdateButtonClicked:(UIButton*)sender |
| 69 | +{ |
| 70 | + if (clickHandlerApplyUpdatePtr) |
| 71 | + clickHandlerApplyUpdatePtr(); |
| 72 | +} |
| 73 | + |
| 74 | +@end |
| 75 | + |
| 76 | +// called from C# sample |
| 77 | +void |
| 78 | +ios_register_button_click (void* ptr) |
| 79 | +{ |
| 80 | + clickHandlerPtr = ptr; |
| 81 | +} |
| 82 | + |
| 83 | +// called from C# sample |
| 84 | +void |
| 85 | +ios_register_applyupdate_click (void* ptr) |
| 86 | +{ |
| 87 | + clickHandlerApplyUpdatePtr = ptr; |
| 88 | +} |
| 89 | + |
| 90 | +// called from C# sample |
| 91 | +void |
| 92 | +ios_set_text (const char* value) |
| 93 | +{ |
| 94 | + NSString* nsstr = [NSString stringWithUTF8String:strdup(value)]; |
| 95 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 96 | + label.text = nsstr; |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +int main(int argc, char * argv[]) { |
| 101 | + @autoreleasepool { |
| 102 | + setenv("DOTNET_MODIFIABLE_ASSEMBLIES", "Debug", 1); |
| 103 | + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments