美文网首页
项目问题杂记

项目问题杂记

作者: woniu | 来源:发表于2021-06-20 16:57 被阅读0次

    一、gitlab的SSH问题

    Gitlab上出现“You won't be able to pull or push project code via SSH until you add an SSH key to you”
    

    解决方式参考

    二、flutter XCode版本问题

    The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 7.0, but the range ...
    

    将下述内容添加在PodFile的最下方,重新执行Pod install,修改的版本号和项目中的一致,否则报错。

    post_install do |installer|
        installer.pods_project.targets.each do |target|
            target.build_configurations.each do |config|
                config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
            end
        end
    end
    
    ————————————————
    版权声明:本文为CSDN博主「Eric_HYD」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/haha223545/article/details/105165247
    

    三、JS和web交互
    iOS 与JS交互之WKScriptMessageHandler协议
    1、iOS调用JS:

    iOS调用JS方式是通过WKWebView的-evaluateJavaScript:completionHandler:方法来实现的。
    

    2、JS调用iOS
    JS代码:

    //! 登录按钮
    <button onclick = "login()" style = "font-size: 18px;">登录</button>
     
    //! 登录
    function login() {
      var token = "js_tokenString";
      var action = "loginSucceed";
      window.webkit.messageHandlers.jsToOc.postMessage(action, token);
    }
    

    iOS代码:

    //! 第一步:导入WebKit框架头文件
    #import <WebKit/WebKit.h>
     
    //! 第二步:WKWebViewWKScriptMessageHandlerController遵守WKScriptMessageHandler协议
    @interface WKWebViewWKScriptMessageHandlerController () <WKScriptMessageHandler>
     
     
    //! 第三步:为userContentController添加ScriptMessageHandler,并指明name
    WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    [userContentController addScriptMessageHandler:self name:@"jsToOc"];
     
    //! 第四步:使用添加了ScriptMessageHandler的userContentController配置configuration
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    configuration.userContentController = userContentController;
     
    //! 第五步:使用configuration对象初始化webView
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
    
    //! 第六步:WKWebView收到ScriptMessage时回调此方法
    #pragma mark - WKScriptMessageHandler
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {    
        if ([message.name caseInsensitiveCompare:@"jsToOc"] == NSOrderedSame) {
            [WKWebViewWKScriptMessageHandlerController showAlertWithTitle:message.name message:message.body cancelHandler:nil];
        }
    } 
    
    • 实现原理:
      1、JS与iOS约定好jsToOc方法,用作JS在调用iOS时的方法;
      2、iOS使用WKUserContentController的-addScriptMessageHandler:name:方法监听name为jsToOc的消息;
      3、JS通过window.webkit.messageHandlers.jsToOc.postMessage()的方式对jsToOc方法发送消息;
      4、iOS在-userContentController:didReceiveScriptMessage:方法中读取name为jsToOc的消息数据message.body。

    注意:[userContentController addScriptMessageHandler:self name:@"jsToOc"]会引起循环引用问题。一般来说,在合适的时机removeScriptMessageHandler可以解决此问题。比如:在-viewWillAppear:方法中执行add操作,在-viewWillDisappear:方法中执行remove操作。

    - (void)viewWillAppear:(BOOL)animated {
        
        [super viewWillAppear:animated];
        
        [_webView.configuration.userContentController addScriptMessageHandler:self name:@"jsToOc"];
    }
     
    - (void)viewWillDisappear:(BOOL)animated {
        
        [super viewWillDisappear:animated];
        
        [_webView.configuration.userContentController removeScriptMessageHandlerForName:@"jsToOc"];
    }
    

    资料来源

    相关文章

      网友评论

          本文标题:项目问题杂记

          本文链接:https://www.haomeiwen.com/subject/vegyyltx.html