|
| 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 | +#import <UIKit/UIKit.h> |
| 5 | +#import <dlfcn.h> |
| 6 | +#import "runtime.h" |
| 7 | +#include <TargetConditionals.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 *summaryLabel; |
| 28 | +UITextView* logLabel; |
| 29 | + |
| 30 | +@implementation ViewController |
| 31 | + |
| 32 | +- (void)viewDidLoad { |
| 33 | + [super viewDidLoad]; |
| 34 | + |
| 35 | + CGRect applicationFrame = [[UIScreen mainScreen] bounds]; |
| 36 | + logLabel = [[UITextView alloc] initWithFrame: |
| 37 | + CGRectMake(2.0, 50.0, applicationFrame.size.width - 2.0, applicationFrame.size.height - 50.0)]; |
| 38 | + logLabel.font = [UIFont systemFontOfSize:9.0]; |
| 39 | + logLabel.backgroundColor = [UIColor blackColor]; |
| 40 | + logLabel.textColor = [UIColor greenColor]; |
| 41 | + logLabel.scrollEnabled = YES; |
| 42 | + logLabel.alwaysBounceVertical = YES; |
| 43 | +#ifndef TARGET_OS_TV |
| 44 | + logLabel.editable = NO; |
| 45 | +#endif |
| 46 | + logLabel.clipsToBounds = YES; |
| 47 | + |
| 48 | + summaryLabel = [[UILabel alloc] initWithFrame: CGRectMake(10.0, 0.0, applicationFrame.size.width - 10.0, 50)]; |
| 49 | + summaryLabel.textColor = [UIColor whiteColor]; |
| 50 | + summaryLabel.font = [UIFont boldSystemFontOfSize: 12]; |
| 51 | + summaryLabel.numberOfLines = 2; |
| 52 | + summaryLabel.textAlignment = NSTextAlignmentLeft; |
| 53 | +#if !TARGET_OS_SIMULATOR || FORCE_AOT |
| 54 | + summaryLabel.text = @"Loading..."; |
| 55 | +#else |
| 56 | + summaryLabel.text = @"Jitting..."; |
| 57 | +#endif |
| 58 | + [self.view addSubview:logLabel]; |
| 59 | + [self.view addSubview:summaryLabel]; |
| 60 | + |
| 61 | + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 62 | + void *del = dlsym (RTLD_DEFAULT, "exposed_managed_method"); |
| 63 | + NSAssert(del != NULL, @"'exposed_managed_method' not found"); |
| 64 | + mono_ios_runtime_init (); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +@end |
| 69 | + |
| 70 | +// called from C# |
| 71 | +void |
| 72 | +invoke_external_native_api (void (*callback)(void)) |
| 73 | +{ |
| 74 | + if (callback) |
| 75 | + callback(); |
| 76 | +} |
| 77 | + |
| 78 | +// can be called from C# to update UI |
| 79 | +void |
| 80 | +mono_ios_set_summary (const char* value) |
| 81 | +{ |
| 82 | + NSString* nsstr = [NSString stringWithUTF8String:value]; |
| 83 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 84 | + summaryLabel.text = nsstr; |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +// can be called from C# to update UI |
| 89 | +void |
| 90 | +mono_ios_append_output (const char* value) |
| 91 | +{ |
| 92 | + NSString* nsstr = [NSString stringWithUTF8String:value]; |
| 93 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 94 | + logLabel.text = [logLabel.text stringByAppendingString:nsstr]; |
| 95 | + CGRect caretRect = [logLabel caretRectForPosition:logLabel.endOfDocument]; |
| 96 | + [logLabel scrollRectToVisible:caretRect animated:NO]; |
| 97 | + [logLabel setScrollEnabled:NO]; |
| 98 | + [logLabel setScrollEnabled:YES]; |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +int main(int argc, char * argv[]) { |
| 103 | + @autoreleasepool { |
| 104 | + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); |
| 105 | + } |
| 106 | +} |
0 commit comments