feat:add network plugin to iOS

This commit is contained in:
pengfei.zhou 2019-11-21 20:16:48 +08:00
parent 632e4b98b9
commit 74e5421282
5 changed files with 87 additions and 6 deletions

View File

@ -16,9 +16,7 @@
package pub.doric.plugin; package pub.doric.plugin;
import android.text.TextUtils; 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.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
@ -26,11 +24,9 @@ import com.github.pengfeizhou.jscore.JSValue;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;

View File

@ -14,9 +14,9 @@ class NetworkDemo extends Panel {
textColor: Color.WHITE, textColor: Color.WHITE,
layoutConfig: layoutConfig().exactly(), layoutConfig: layoutConfig().exactly(),
onClick: () => { onClick: () => {
network(context).get('https://doric.pub').then( network(context).get('https://m.baidu.com').then(
e => { e => {
modal(context).toast(JSON.stringify(e)) modal(context).alert(JSON.stringify(e))
} }
).catch(e => { ).catch(e => {
modal(context).toast('Catched:' + JSON.stringify(e)) modal(context).toast('Catched:' + JSON.stringify(e))

View File

@ -22,6 +22,7 @@
#import "DoricRegistry.h" #import "DoricRegistry.h"
#import "DoricModalPlugin.h" #import "DoricModalPlugin.h"
#import "DoricNetworkPlugin.h"
#import "DoricShaderPlugin.h" #import "DoricShaderPlugin.h"
#import "DoricStackNode.h" #import "DoricStackNode.h"
#import "DoricVLayoutNode.h" #import "DoricVLayoutNode.h"
@ -56,6 +57,7 @@ - (instancetype)init {
- (void)innerRegister { - (void)innerRegister {
[self registerNativePlugin:DoricModalPlugin.class withName:@"modal"]; [self registerNativePlugin:DoricModalPlugin.class withName:@"modal"];
[self registerNativePlugin:DoricNetworkPlugin.class withName:@"network"];
[self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"]; [self registerNativePlugin:DoricShaderPlugin.class withName:@"shader"];
[self registerViewNode:DoricStackNode.class withName:@"Stack"]; [self registerViewNode:DoricStackNode.class withName:@"Stack"];

View File

@ -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 <Foundation/Foundation.h>
#import "DoricNativePlugin.h"
@interface DoricNetworkPlugin : DoricNativePlugin
@end

View File

@ -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 <NSString *, NSString *> *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