From 74e542128209b19936994f38dc3e5382cc687349 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Thu, 21 Nov 2019 20:16:48 +0800 Subject: [PATCH] feat:add network plugin to iOS --- .../java/pub/doric/plugin/NetworkPlugin.java | 4 -- demo/src/NetworkDemo.ts | 4 +- iOS/Pod/Classes/DoricRegistry.m | 2 + iOS/Pod/Classes/Plugin/DoricNetworkPlugin.h | 24 ++++++++ iOS/Pod/Classes/Plugin/DoricNetworkPlugin.m | 59 +++++++++++++++++++ 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 iOS/Pod/Classes/Plugin/DoricNetworkPlugin.h create mode 100644 iOS/Pod/Classes/Plugin/DoricNetworkPlugin.m diff --git a/Android/doric/src/main/java/pub/doric/plugin/NetworkPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/NetworkPlugin.java index e9d65fed..004412ce 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/NetworkPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/NetworkPlugin.java @@ -16,9 +16,7 @@ package pub.doric.plugin; import android.text.TextUtils; -import android.util.Log; -import com.github.pengfeizhou.jscore.ArchiveException; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSObject; @@ -26,11 +24,9 @@ import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JavaValue; import org.jetbrains.annotations.NotNull; -import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; -import java.util.Iterator; import java.util.Set; import java.util.concurrent.TimeUnit; diff --git a/demo/src/NetworkDemo.ts b/demo/src/NetworkDemo.ts index bc1adb39..da218364 100644 --- a/demo/src/NetworkDemo.ts +++ b/demo/src/NetworkDemo.ts @@ -14,9 +14,9 @@ class NetworkDemo extends Panel { textColor: Color.WHITE, layoutConfig: layoutConfig().exactly(), onClick: () => { - network(context).get('https://doric.pub').then( + network(context).get('https://m.baidu.com').then( e => { - modal(context).toast(JSON.stringify(e)) + modal(context).alert(JSON.stringify(e)) } ).catch(e => { modal(context).toast('Catched:' + JSON.stringify(e)) diff --git a/iOS/Pod/Classes/DoricRegistry.m b/iOS/Pod/Classes/DoricRegistry.m index 44334ace..609eba0c 100644 --- a/iOS/Pod/Classes/DoricRegistry.m +++ b/iOS/Pod/Classes/DoricRegistry.m @@ -22,6 +22,7 @@ #import "DoricRegistry.h" #import "DoricModalPlugin.h" +#import "DoricNetworkPlugin.h" #import "DoricShaderPlugin.h" #import "DoricStackNode.h" #import "DoricVLayoutNode.h" @@ -56,6 +57,7 @@ - (instancetype)init { - (void)innerRegister { [self registerNativePlugin:DoricModalPlugin.class withName:@"modal"]; + [self registerNativePlugin:DoricNetworkPlugin.class withName:@"network"]; [self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"]; [self registerViewNode:DoricStackNode.class withName:@"Stack"]; diff --git a/iOS/Pod/Classes/Plugin/DoricNetworkPlugin.h b/iOS/Pod/Classes/Plugin/DoricNetworkPlugin.h new file mode 100644 index 00000000..3598bd80 --- /dev/null +++ b/iOS/Pod/Classes/Plugin/DoricNetworkPlugin.h @@ -0,0 +1,24 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by pengfei.zhou on 2019/11/21. +// + +#import +#import "DoricNativePlugin.h" + +@interface DoricNetworkPlugin : DoricNativePlugin +@end \ No newline at end of file diff --git a/iOS/Pod/Classes/Plugin/DoricNetworkPlugin.m b/iOS/Pod/Classes/Plugin/DoricNetworkPlugin.m new file mode 100644 index 00000000..2d03c35f --- /dev/null +++ b/iOS/Pod/Classes/Plugin/DoricNetworkPlugin.m @@ -0,0 +1,59 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by pengfei.zhou on 2019/11/21. +// + +#import "DoricNetworkPlugin.h" + + +@implementation DoricNetworkPlugin +- (void)request:(NSDictionary *)dic withPromise:(DoricPromise *)promise { + NSString *url = dic[@"url"]; + NSString *method = dic[@"method"]; + NSDictionary *headers = dic[@"headers"]; + NSNumber *timeout = dic[@"timeout"]; + NSString *data = dic[@"data"]; + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; + request.HTTPMethod = method.uppercaseString; + if (timeout) { + request.timeoutInterval = [timeout floatValue] / 1000; + } + if (headers) { + [headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { + [request setValue:obj forHTTPHeaderField:key]; + }]; + } + if (data) { + [request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]]; + } + [[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]] + dataTaskWithRequest:request + completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if (!error) { + NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + NSDictionary *resDic = @{ + @"status": @(((NSHTTPURLResponse *) response).statusCode), + @"headers": ((NSHTTPURLResponse *) response).allHeaderFields, + @"data": dataStr, + }; + [promise resolve:resDic]; + } else { + [promise reject:error.description]; + } + }] resume]; +} +@end