diff --git a/examples/hello_services/ios/HelloServices.xcodeproj/project.pbxproj b/examples/hello_services/ios/HelloServices.xcodeproj/project.pbxproj index d693476c13..b8740853b0 100644 --- a/examples/hello_services/ios/HelloServices.xcodeproj/project.pbxproj +++ b/examples/hello_services/ios/HelloServices.xcodeproj/project.pbxproj @@ -13,6 +13,8 @@ 9740EEB41CF90195004384FC /* Flutter.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Flutter.xcconfig */; }; 9740EEB51CF90195004384FC /* Generated.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB31CF90195004384FC /* Generated.xcconfig */; }; 9740EEBB1CF902C7004384FC /* app.flx in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB71CF902C7004384FC /* app.flx */; }; + 977505191CFDF23500BC28DA /* LocationProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 977505181CFDF23500BC28DA /* LocationProvider.m */; }; + 97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 97A38A341CFDEC880099F1B4 /* AppDelegate.m */; }; 97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; }; 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; }; @@ -40,6 +42,10 @@ 9740EEB71CF902C7004384FC /* app.flx */ = {isa = PBXFileReference; lastKnownFileType = file; name = app.flx; path = Flutter/app.flx; sourceTree = ""; }; 9740EEB81CF902C7004384FC /* app.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = app.so; path = Flutter/app.so; sourceTree = ""; }; 9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = ""; }; + 977505171CFDF21E00BC28DA /* LocationProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocationProvider.h; sourceTree = ""; }; + 977505181CFDF23500BC28DA /* LocationProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationProvider.m; sourceTree = ""; }; + 97A38A331CFDEC680099F1B4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; + 97A38A341CFDEC880099F1B4 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 97C146EE1CF9000F007C117D /* HelloServices.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HelloServices.app; sourceTree = BUILT_PRODUCTS_DIR; }; 97C146F21CF9000F007C117D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 97C146FB1CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -97,6 +103,10 @@ 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */, 97C147021CF9000F007C117D /* Info.plist */, 97C146F11CF9000F007C117D /* Supporting Files */, + 97A38A331CFDEC680099F1B4 /* AppDelegate.h */, + 97A38A341CFDEC880099F1B4 /* AppDelegate.m */, + 977505171CFDF21E00BC28DA /* LocationProvider.h */, + 977505181CFDF23500BC28DA /* LocationProvider.m */, ); path = HelloServices; sourceTree = ""; @@ -200,7 +210,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */, 97C146F31CF9000F007C117D /* main.m in Sources */, + 977505191CFDF23500BC28DA /* LocationProvider.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/examples/hello_services/ios/HelloServices/AppDelegate.h b/examples/hello_services/ios/HelloServices/AppDelegate.h new file mode 100644 index 0000000000..5dae783b0a --- /dev/null +++ b/examples/hello_services/ios/HelloServices/AppDelegate.h @@ -0,0 +1,12 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import + +@interface AppDelegate : UIResponder + +@property (strong, nonatomic) UIWindow *window; + +@end + diff --git a/examples/hello_services/ios/HelloServices/AppDelegate.m b/examples/hello_services/ios/HelloServices/AppDelegate.m new file mode 100644 index 0000000000..d42c82e29a --- /dev/null +++ b/examples/hello_services/ios/HelloServices/AppDelegate.m @@ -0,0 +1,28 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "AppDelegate.h" + +#import +#import "LocationProvider.h" + +@implementation AppDelegate { + LocationProvider* _locationProvider; +} + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration]; + self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; + FlutterViewController* controller = [[FlutterViewController alloc] initWithProject:project + nibName:nil + bundle:nil]; + _locationProvider = [[LocationProvider alloc] init]; + [controller addMessageListener: _locationProvider]; + + self.window.rootViewController = controller; + [self.window makeKeyAndVisible]; + return YES; +} + +@end diff --git a/examples/hello_services/ios/HelloServices/Base.lproj/LaunchScreen.storyboard b/examples/hello_services/ios/HelloServices/Base.lproj/LaunchScreen.storyboard index 2e721e1833..ebf48f6039 100644 --- a/examples/hello_services/ios/HelloServices/Base.lproj/LaunchScreen.storyboard +++ b/examples/hello_services/ios/HelloServices/Base.lproj/LaunchScreen.storyboard @@ -1,7 +1,8 @@ - + - + + @@ -15,7 +16,6 @@ - diff --git a/examples/hello_services/ios/HelloServices/LocationProvider.h b/examples/hello_services/ios/HelloServices/LocationProvider.h new file mode 100644 index 0000000000..e646fdf4fc --- /dev/null +++ b/examples/hello_services/ios/HelloServices/LocationProvider.h @@ -0,0 +1,9 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import + +@interface LocationProvider : NSObject + +@end diff --git a/examples/hello_services/ios/HelloServices/LocationProvider.m b/examples/hello_services/ios/HelloServices/LocationProvider.m new file mode 100644 index 0000000000..e0d823dc69 --- /dev/null +++ b/examples/hello_services/ios/HelloServices/LocationProvider.m @@ -0,0 +1,28 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "LocationProvider.h" + +@implementation LocationProvider + +@synthesize messageName = _messageName; + +- (instancetype) init { + self = [super init]; + if (self) + self->_messageName = @"getLocation"; + return self; +} + +- (NSString*)didReceiveString:(NSString*)message { + NSDictionary* response = @{ + @"latitude": @3.29334, + @"longitude": @8.2492492 + }; + + NSData* data = [NSJSONSerialization dataWithJSONObject:response options:0 error:nil]; + return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; +} + +@end diff --git a/examples/hello_services/ios/HelloServices/main.m b/examples/hello_services/ios/HelloServices/main.m index f43358c696..ca7065527c 100644 --- a/examples/hello_services/ios/HelloServices/main.m +++ b/examples/hello_services/ios/HelloServices/main.m @@ -4,11 +4,12 @@ #import #import +#import "AppDelegate.h" int main(int argc, char * argv[]) { FlutterInit(argc, (const char**)argv); @autoreleasepool { return UIApplicationMain(argc, argv, nil, - NSStringFromClass([FlutterAppDelegate class])); + NSStringFromClass([AppDelegate class])); } } diff --git a/examples/hello_services/lib/main.dart b/examples/hello_services/lib/main.dart index fc37338118..7e770ce10f 100644 --- a/examples/hello_services/lib/main.dart +++ b/examples/hello_services/lib/main.dart @@ -1,7 +1,72 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. +// Copyright 2016 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/widgets.dart'; +import 'dart:async'; +import 'dart:convert'; +import 'dart:math'; -void main() => runApp(new Center(child: new Text('Hello, world!'))); +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; + +final Random random = new Random(); + +Future handleGetRandom(String json) async { + Map message = JSON.decode(json); + double min = message['min'].toDouble(); + double max = message['max'].toDouble(); + + double value = (random.nextDouble() * (max - min)) + min; + + Map reply = {'value': value}; + return JSON.encode(reply); +} + +class HelloServices extends StatefulWidget { + @override + _HelloServicesState createState() => new _HelloServicesState(); +} + +class _HelloServicesState extends State { + double _latitude; + double _longitude; + + @override + Widget build(BuildContext context) { + return new Material( + child: new Center( + child: new Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + new Text('Hello from Flutter!'), + new RaisedButton( + child: new Text('Get Location'), + onPressed: _getLocation + ), + new Text('Latitude: $_latitude, Longitude: $_longitude'), + ] + ) + ) + ); + } + + void _getLocation() { + Map message = {'provider': 'network'}; + HostMessages.sendToHost('getLocation', JSON.encode(message)) + .then(_onReceivedLocation); + } + + void _onReceivedLocation(String json) { + Map reply = JSON.decode(json); + setState(() { + _latitude = reply['latitude']; + _longitude = reply['longitude']; + }); + } +} + +void main() { + runApp(new HelloServices()); + + HostMessages.addMessageHandler('getRandom', handleGetRandom); +}