You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ac...@apache.org on 2018/03/12 03:20:09 UTC

incubator-weex git commit: + [ios] more enhanced about web

Repository: incubator-weex
Updated Branches:
  refs/heads/master 939bc5454 -> 42aa12665


+ [ios] more enhanced about web

+ [ios] change to postmessage

+ [ios] Code Style Guidelines

+ [ios] according to the W3C specification

+ [ios] send event like web

+[ios] support component method and more stabilize

+[ios] more stabilize about source

+[ios] more stabilize about initsource
close #1047


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/42aa1266
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/42aa1266
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/42aa1266

Branch: refs/heads/master
Commit: 42aa12665e960fef28608e09a8868ac5188388f6
Parents: 939bc54
Author: Tw93 <tw...@qq.com>
Authored: Tue Feb 27 18:47:26 2018 +0800
Committer: acton393 <zh...@gmail.com>
Committed: Mon Mar 12 11:19:01 2018 +0800

----------------------------------------------------------------------
 .../WeexSDK/Sources/Component/WXWebComponent.h  |   2 +
 .../WeexSDK/Sources/Component/WXWebComponent.m  | 103 +++++++++++++++++--
 .../WeexSDK/Sources/Module/WXWebViewModule.m    |   8 ++
 3 files changed, 103 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/42aa1266/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.h b/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.h
index 9741ed0..97800a6 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.h
+++ b/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.h
@@ -23,6 +23,8 @@
 
 - (void)notifyWebview:(NSDictionary *) data;
 
+- (void)postMessage:(NSDictionary *) data;
+
 - (void)reload;
 
 - (void)goBack;

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/42aa1266/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.m
index c0bd9f4..35c95fa 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXWebComponent.m
@@ -49,6 +49,11 @@
 
 @property (nonatomic, strong) NSString *url;
 
+@property (nonatomic, strong) NSString *source;
+
+// save source during this initialization
+@property (nonatomic, strong) NSString *inInitsource;
+
 @property (nonatomic, assign) BOOL startLoadEvent;
 
 @property (nonatomic, assign) BOOL finishLoadEvent;
@@ -61,6 +66,7 @@
 
 @implementation WXWebComponent
 
+WX_EXPORT_METHOD(@selector(postMessage:))
 WX_EXPORT_METHOD(@selector(goBack))
 WX_EXPORT_METHOD(@selector(reload))
 WX_EXPORT_METHOD(@selector(goForward))
@@ -69,6 +75,11 @@ WX_EXPORT_METHOD(@selector(goForward))
 {
     if (self = [super initWithRef:ref type:type styles:styles attributes:attributes events:events weexInstance:weexInstance]) {
         self.url = attributes[@"src"];
+        
+        if(attributes[@"source"]){
+            self.inInitsource = attributes[@"source"];
+        }
+        
     }
     return self;
 }
@@ -88,12 +99,39 @@ WX_EXPORT_METHOD(@selector(goForward))
     _webview.opaque = NO;
     _jsContext = [_webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
     __weak typeof(self) weakSelf = self;
+
+    // This method will be abandoned slowly.
     _jsContext[@"$notifyWeex"] = ^(JSValue *data) {
         if (weakSelf.notifyEvent) {
             [weakSelf fireEvent:@"notify" params:[data toDictionary]];
         }
     };
-    
+
+    //Weex catch postMessage event from web
+    _jsContext[@"postMessage"] = ^() {
+
+        NSArray *args = [JSContext currentArguments];
+
+        if (args && args.count < 2) {
+            return;
+        }
+
+        NSDictionary *data = [args[0] toDictionary];
+        NSString *origin = [args[1] toString];
+
+        if (data == nil) {
+            return;
+        }
+
+        NSDictionary *initDic = @{ @"type" : @"message",
+                                   @"data" : data,
+                                   @"origin" : origin
+        };
+
+        [weakSelf fireEvent:@"message" params:initDic];
+    };
+
+    self.source = _inInitsource;
     if (_url) {
         [self loadURL:_url];
     }
@@ -104,6 +142,11 @@ WX_EXPORT_METHOD(@selector(goForward))
     if (attributes[@"src"]) {
         self.url = attributes[@"src"];
     }
+
+    if (attributes[@"source"]) {
+        self.inInitsource = attributes[@"source"];
+        self.source = self.inInitsource;
+    }
 }
 
 - (void)addEvent:(NSString *)eventName
@@ -135,6 +178,21 @@ WX_EXPORT_METHOD(@selector(goForward))
     }
 }
 
+- (void) setSource:(NSString *)source
+{
+    NSString *newSource=[source copy];
+    if(!newSource || _url){
+        return;
+    }
+    if(![newSource isEqualToString:_source]){
+        _source=newSource;
+        if(_source){
+            [_webview loadHTMLString:_source baseURL:nil];
+        }
+    }
+    
+}
+
 - (void)loadURL:(NSString *)url
 {
     if (self.webview) {
@@ -162,6 +220,7 @@ WX_EXPORT_METHOD(@selector(goForward))
     }
 }
 
+// This method will be abandoned slowly, use postMessage
 - (void)notifyWebview:(NSDictionary *) data
 {
     NSString *json = [WXUtility JSONString:data];
@@ -169,6 +228,30 @@ WX_EXPORT_METHOD(@selector(goForward))
     [_jsContext evaluateScript:code];
 }
 
+// Weex postMessage to web
+- (void)postMessage:(NSDictionary *)data {
+    WXSDKInstance *instance = [WXSDKEngine topInstance];
+
+    NSString *bundleUrlOrigin = @"";
+
+    if (instance.pageName) {
+        NSString *bundleUrl = [instance.scriptURL absoluteString];
+        NSURL *url = [NSURL URLWithString:bundleUrl];
+        bundleUrlOrigin = [NSString stringWithFormat:@"%@://%@%@", url.scheme, url.host, url.port ? [NSString stringWithFormat:@":%@", url.port] : @""];
+    }
+
+    NSDictionary *initDic = @{
+        @"type" : @"message",
+        @"data" : data,
+        @"origin" : bundleUrlOrigin
+    };
+
+    NSString *json = [WXUtility JSONString:initDic];
+
+    NSString *code = [NSString stringWithFormat:@"(function (){window.dispatchEvent(new MessageEvent('message', %@));}())", json];
+    [_jsContext evaluateScript:code];
+}
+
 #pragma mark Webview Delegate
 
 - (NSMutableDictionary<NSString *, id> *)baseInfo
@@ -200,15 +283,15 @@ WX_EXPORT_METHOD(@selector(goForward))
         NSMutableDictionary *data = [self baseInfo];
         [data setObject:[error localizedDescription] forKey:@"errorMsg"];
         [data setObject:[NSString stringWithFormat:@"%ld", (long)error.code] forKey:@"errorCode"];
-		
-		NSString * urlString = error.userInfo[NSURLErrorFailingURLStringErrorKey];
-		if (urlString) {
-			// webview.request may not be the real error URL, must get from error.userInfo
-			[data setObject:urlString forKey:@"url"];
-			if (![urlString hasPrefix:@"http"]) {
-				return;
-			}
-		}
+        
+        NSString * urlString = error.userInfo[NSURLErrorFailingURLStringErrorKey];
+        if (urlString) {
+            // webview.request may not be the real error URL, must get from error.userInfo
+            [data setObject:urlString forKey:@"url"];
+            if (![urlString hasPrefix:@"http"]) {
+                return;
+            }
+        }
         [self fireEvent:@"error" params:data];
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/42aa1266/ios/sdk/WeexSDK/Sources/Module/WXWebViewModule.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Module/WXWebViewModule.m b/ios/sdk/WeexSDK/Sources/Module/WXWebViewModule.m
index dee8f86..20a3be3 100644
--- a/ios/sdk/WeexSDK/Sources/Module/WXWebViewModule.m
+++ b/ios/sdk/WeexSDK/Sources/Module/WXWebViewModule.m
@@ -27,6 +27,7 @@
 @synthesize weexInstance;
 
 WX_EXPORT_METHOD(@selector(notifyWebview:data:))
+WX_EXPORT_METHOD(@selector(postMessage:data:))
 WX_EXPORT_METHOD(@selector(reload:))
 WX_EXPORT_METHOD(@selector(goBack:))
 WX_EXPORT_METHOD(@selector(goForward:))
@@ -60,6 +61,13 @@ WX_EXPORT_METHOD(@selector(goForward:))
     }];
 }
 
+- (void)postMessage:(NSString *)elemRef data:(NSDictionary *)data {
+    [self performBlockWithWebView:elemRef block:^void (WXWebComponent *webview) {
+        [webview postMessage:data];
+    }];
+}
+
+
 - (void)reload:(NSString *)elemRef {
     [self performBlockWithWebView:elemRef block:^void (WXWebComponent *webview) {
         [webview reload];