低调点现实点 發表於 2020-4-26 14:33:00

ios 开发搭建

<p>&nbsp;</p>
<p>项目地址:https://gitee.com/anan9303/iosFrame</p>
<p>目录:(1)网络请求框架</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(2)控制台输出中文log日志&nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(3)iOS开发中添加PrefixHeader.pch要注意的问题 &nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(4)block使用</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(5)base64、MD5加密解密</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(6)沙盒存储获取</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(7)NSLog全部打印总结</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(8)JSON序列化,以及NSdata、NSString、NSDictionay之间的转换</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(9)&nbsp; 页面切换管理:<strong>UINavigationController</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(10)观察者模式</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(11)</strong><strong>WKWebView以及JS混合开发</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1)、<strong>WKWebViewConfiguration方式</strong></strong></p>
<p>一、网路请求框架:</p>
<p class="p1"><strong>&nbsp; &nbsp; (1)NSURLConnection</strong></p>
<p class="p1"><strong>&nbsp; &nbsp; &nbsp; &nbsp;</strong></p>
<p class="p1">&nbsp;</p>
<p>二、控制台输出中文log日志:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;https://gitee.com/anan9303/iosFrame</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;(1)数组和&nbsp;<strong>NSDictionary</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .h文件:</strong></p>
<div class="cnblogs_code">
<pre>#import &lt;UIKit/UIKit.h&gt;<span style="color: rgba(0, 0, 0, 1)">

@interface AppDelegate : UIResponder </span>&lt;UIApplicationDelegate&gt;<span style="color: rgba(0, 0, 0, 1)">

@end</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.m文件</p>
<div class="cnblogs_code">
<pre>#import <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">AppDelegate.h</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">

@interface AppDelegate ()

@end

@implementation AppDelegate


</span>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *<span style="color: rgba(0, 0, 0, 1)">)launchOptions {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Override point for customization after application launch.</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> YES;
}


</span><span style="color: rgba(0, 0, 255, 1)">#pragma</span> mark - UISceneSession lifecycle


- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *<span style="color: rgba(0, 0, 0, 1)">)options {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Called when a new scene session is being created.
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Use this method to select a configuration to create the new scene with.</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span> [ initWithName:<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">Default Configuration</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> sessionRole:connectingSceneSession.role];
}


</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)application:(UIApplication *)application didDiscardSceneSessions:(NSSet&lt;UISceneSession *&gt; *<span style="color: rgba(0, 0, 0, 1)">)sceneSessions {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Called when the user discards a scene session.
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Use this method to release any resources that were specific to the discarded scenes, as they will not return.</span>
<span style="color: rgba(0, 0, 0, 1)">}


@end</span></pre>
</div>
<p>&nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (2)对象的输出:</strong></p>
<div class="cnblogs_code">
<pre>- (NSString *<span style="color: rgba(0, 0, 0, 1)">)description{
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> , self, _name, _age];
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>三、iOS开发中添加PrefixHeader.pch要注意的问题:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;(1) 是什么:pch是“<span id="more-481">precompiled header”,也就是预编译头文件。</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 所谓的预编译头就是把一个工程中的那一部分代码,预先编译好放在一个文件里(通常是 以.pch为扩展名的),这个文件就称为预编译头文件。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;(2)预编译头的作用:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 你应该把不常变化的框架(Frameworks)头文件添加到前缀头文件(prefix header)中。这样的话,在编译的时候,框架的代码会被预先编译,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;所有的类都将可以使用这些头文件。预编译头文件的作用当然就是提高便宜速度了,有了它你没有必要每次都编译那些不需要经常改变的代码。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;(3)配置:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;在Xcode6.0已经不默认生成PrefixHeader.pch文件了,而PrefixHeader.pch文件对我们开发带来的便利性是不言而喻的,所以我们怎么在</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 工程中添加PrefixHeader.pch文件呢.</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;首先.我们创建一个PrefixHeader.pch</p>
<p class="p1"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/896629/202004/896629-20200426181115842-785242499.png"></strong></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/896629/202004/896629-20200426181138951-100900631.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 然后将这个prefix文件和这个工程关联,</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;首先开启 Precompile Prefix Header 为Yes</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 在Build Settings 中搜索Prefix ,在Apple LLVM 6.0 -Languge 中的Prefix Herder 添加新建 &nbsp; 的Prefix的路径(直接将这个文件拖进去就行);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p class="p1"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/896629/202004/896629-20200426181214278-1443233379.png"></strong></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 这样修改的路径只能在这台电脑上编译通过,想要在其他电脑上编译通过需要将第一遍工程名之前的路径替换成工程的相对路径$(SRCROOT)</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 也就是将:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/Users/Zym/Desktop/LoveLimitFree/LoveLimitFree/PrefixHeader.pch</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 修改为</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(SRCROOT)/LoveLimitFree/PrefixHeader.pch</p>
<p class="p1"><strong>&nbsp;</strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/896629/202004/896629-20200426181331030-644663699.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp;四、block使用:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:objc;gutter:true;">(void (^)(id responseObject))success
</pre>
</div>
<p>  </p>
<p>&nbsp; &nbsp;五、base64、MD5加密解密</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1、 Base64,是可逆加密</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加密</span>
- (NSString *) base64Encode:(NSString *<span style="color: rgba(0, 0, 0, 1)">)str{
    NSData </span>*data=<span style="color: rgba(0, 0, 0, 1)"> ;
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> ;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">解密</span>
- (NSString *) base64Decode:(NSString *<span style="color: rgba(0, 0, 0, 1)">)str{
NSData </span>*data=[ initWithBase64EncodedString:str options:<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">];
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> [ initWithData:data encoding:NSUTF8StringEncoding];
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 2、MD5加密,是不可逆加密,一般用于存储密码</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (1)MD5直接加密之后也容易进行破解&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p>
<div class="cnblogs_code">
<pre>psw=;</pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (2)加盐: 也就是在加密的字符串中加入指定的字符串进行加密&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 例如: "12323"+"123kjW@(*^&amp;%&amp;%"</p>
<div class="cnblogs_code">
<pre>[md5String];</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (3)HMAC:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;原始密码加上一组字符串在进行混合 md5计算,把计算的结果+原密码在进行md5计算</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (4)md5+时间,每次生成的md5值不一样,使用步骤如下:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)一个字符串key&nbsp; md5计算生成一个md5值</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)把原密码和之前生成的md5值在进行hmac加密;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3)从服务器获取当前时间到分钟的字符串</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4)第二步产生的hmac值+时间(截止到分钟)&nbsp; &nbsp; 和第一步产生的md5值进行hmac加密</p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 后台操作流程一样,只不过,会获取两个加密后的值(服务器时间的当前分钟,和前一分钟);</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 如果有一个值相同,则就可以登录成功</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; 六、沙盒存储获取</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">#define</span> NAMEKEY @"name"
<span style="color: rgba(0, 0, 255, 1)">#define</span> userDefaults
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">) save{
    ;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置数据;</span>
    ;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">立即保存</span>
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>) <span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)">{
NSString </span>*name=<span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp;七、NSLog全部打印总结</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#ifdef DEBUG
</span><span style="color: rgba(0, 0, 255, 1)">#define</span> NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,)
<span style="color: rgba(0, 0, 255, 1)">#else</span>
<span style="color: rgba(0, 0, 255, 1)">#define</span> NSLog(...)
<span style="color: rgba(0, 0, 255, 1)">#endif</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp;八、JSON序列化,以及NSdata、NSString、NSDictionay之间的转换</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">   //</span><span style="color: rgba(0, 128, 0, 1)">NSString转NSData</span>
   NSData *aData =<span style="color: rgba(0, 0, 0, 1)"> ;   

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">NSData转为字符串NSString</span>
<span style="color: rgba(0, 0, 0, 1)">   [ initWithData:data encoding:NSUTF8StringEncoding]

   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">字典序列化转NSData</span>
      NSData *jsonData = ;

   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">NSData转为反序列化Json字符串:常用接收返回信息NSData的转换NSData为json形式字符串才可以</span>
   NSDictionary *dict=

   </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*   
      options:解析出来可能是数组或者字典
    NSJSONReadingMutableContainers = (1UL &lt;&lt; 0), 容器可变 一般使用这个

    NSJSONReadingMutableLeaves = (1UL &lt;&lt; 1),

    NSJSONReadingFragmentsAllowed = (1UL &lt;&lt; 2),// 允许不是json形式的字符串,一般请求返回只有一个字符串,一般不使用这个
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">序列化: 把对象转换为json形式的字符串


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">反序列化: 把json形式的字符串转化成OC对象

   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一、拼接NSDictionary转化为序列化Json字符串转为NSData</span>
   NSDictionary *json = @{<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">username</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">18513884422</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">captchaValue</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">1223</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">ee</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">111212</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">};
   NSData </span>*data=<span style="color: rgba(0, 0, 0, 1)">

   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">二、字符串拼接json串转化为序列化Json字符串转为NSData</span>
    NSString *jsonStr=<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">{\</span><span style="color: rgba(128, 0, 0, 1)">"</span>name\<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">:\"张三\",\"age\":\"12\"}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    NSData </span>*data=<span style="color: rgba(0, 0, 0, 1)">;

   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">三、数组转化为序列化Json字符串转为NSData</span>
         NSArray *array=<span style="color: rgba(0, 0, 0, 1)">@[
               @{</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">username</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">18513884422</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span>:@(<span style="color: rgba(128, 0, 128, 1)">13</span><span style="color: rgba(0, 0, 0, 1)">)},
               @{</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">username</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">18513884462</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">pwd</span><span style="color: rgba(128, 0, 0, 1)">"</span>:@(<span style="color: rgba(128, 0, 128, 1)">14</span><span style="color: rgba(0, 0, 0, 1)">)},
               ];
         NSData </span>*data=;

   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">四、 对象序列化</span>
       Person *p1=<span style="color: rgba(0, 0, 0, 1)">[init];

       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">KVC 赋值</span>
       ;
       ;

      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断对象是否能进行json序列化</span>
<span style="color: rgba(0, 0, 0, 1)">       ;

       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对象转化为data</span>
       NSData *data=<span style="color: rgba(0, 0, 0, 1)"> ;      

       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把自定义对象转化为字典,加入数组中转化为NSData   传入需要序列化的参数</span>
       NSDictionary *dictP=];
       NSMutableArray </span>*mArray=<span style="color: rgba(0, 0, 0, 1)">;
       ;
       NSData </span>*data=;

      </span><span style="color: rgba(0, 128, 0, 1)">//json文件转为</span><span style="color: rgba(0, 128, 0, 1)">data数据写入文件</span>
       ;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取文件返回NSdata数据</span>
       ;</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;九、页面切换管理:<strong>UINavigationController</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 设置</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(1)如果首页是用</strong><strong>storyboard</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Appdelegate.m中:</strong></p>
<div class="cnblogs_code">
<pre><strong>@synthesize</strong><span class="s1"><span class="s2"><strong>window</strong><span class="s1"><strong>=</strong><span class="s3"><strong>_window</strong><span class="s1"><strong>;</strong></span></span></span></span></span><br>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *<span style="color: rgba(0, 0, 0, 1)">)launchOptions {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Override point for customization after application launch.</span><span style="color: rgba(0, 0, 0, 1)">
    UIStoryboard </span>*storyBoard = ;
      ViewController </span>*firstVC = ;
      UINavigationController </span>*naviController =<span style="color: rgba(0, 0, 0, 1)"> [ initWithRootViewController:firstVC];
      ;
      self.window.rootViewController </span>=<span style="color: rgba(0, 0, 0, 1)"> naviController;
      self.window.backgroundColor </span>=<span style="color: rgba(0, 0, 0, 1)"> ;
      ;
}</span></pre>
</div>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (2)如果首页是用动态代码添加</strong><strong><br></strong></p>
<div class="cnblogs_code">
<pre>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *<span style="color: rgba(0, 0, 0, 1)">)launchOptions {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Override point for customization after application launch.</span>
   <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
   不使用storyboard的设置
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    ViewController </span>*viewController =<span style="color: rgba(0, 0, 0, 1)"> [init];
    UINavigationController </span>*uiNavigationController=<span style="color: rgba(0, 0, 0, 1)">[initWithRootViewController:viewController];
    ;
    self.window.rootViewController</span>=<span style="color: rgba(0, 0, 0, 1)">uiNavigationController;<br></span></pre>
<div class="line number6 index5 alt1"><code class="objc spaces">&nbsp; &nbsp; &nbsp;&nbsp;</code><code class="objc keyword">self</code><code class="objc plain">.window.backgroundColor = ;</code></div>
<div class="line number7 index6 alt2"><code class="objc spaces">&nbsp; &nbsp; &nbsp;&nbsp;</code><code class="objc plain">[</code><code class="objc keyword">self</code><code class="objc plain">.window makeKeyAndVisible];//让当前UIWindow变成keyWindow,并显示出来</code></div>
<pre><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> YES;
}</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp;十、观察者模式</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;KVO</strong><strong>&nbsp; &nbsp; &nbsp;&nbsp;</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注册监听 </span>
;                        //</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">销毁监听</span>
;</pre>
</div>
<div class="cnblogs_code">
<pre>//重写监听方法<br>- (<span style="color: rgba(0, 0, 255, 1)">void</span>)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)<span style="color: rgba(0, 0, 255, 1)">object</span> change:(NSDictionary *)change context:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)context
{
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (context == &lt;#context#&gt;<span style="color: rgba(0, 0, 0, 1)">) {
      </span>&lt;#code to be executed upon observing keypath#&gt;<span style="color: rgba(0, 0, 0, 1)">
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      ;
    }
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp;首次加载HTML文件</p>
<p>&nbsp; &nbsp; 1.html本地存储必须在加载完页面之后存储;&nbsp; 杀掉进程再次进入该页面,本地存储不会清除;&nbsp;<span class="s1">//JS本地存储必须加载完页面之后存储才有效</span></p>
<p>&nbsp; &nbsp; 2.cookie信息也必须在页面加载完成之后进行存储;&nbsp; cookie信息杀掉进程,再次进入和之前一样,必须在页面加载完成之后才能存储;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp;十一、<strong>WKWebView以及JS混合开发</strong>&nbsp;&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;(1)、<strong>WKWebViewConfiguration方式</strong></p>
<div class="cnblogs_code">
<pre>#import <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">HomeViewController.h</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
#import </span>&lt;WebKit/WebKit.h&gt;<span style="color: rgba(0, 0, 0, 1)">
@interface HomeViewController ()</span>&lt;WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler&gt;<span style="color: rgba(0, 0, 0, 1)">
@property (weak, nonatomic)WKWebView </span>*<span style="color: rgba(0, 0, 0, 1)">webView;
@property (weak, nonatomic)UIProgressView </span>*<span style="color: rgba(0, 0, 0, 1)">progressView;
@property (strong, nonatomic)WKUserContentController </span>*<span style="color: rgba(0, 0, 0, 1)">userContentController;

@end
@implementation HomeViewController
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">)viewDidLoad {
    ;
    ;
    ;
}
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">)initData {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置请求地址</span>
    NSMutableURLRequest *request=]];
    ;
    ;
   
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">观察者观察进度条</span>
    ;
   
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置代理</span>
    self.webView.navigationDelegate=<span style="color: rgba(0, 0, 0, 1)">self;
    self.webView.UIDelegate</span>=<span style="color: rgba(0, 0, 0, 1)">self;

   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加js注入方法:</span>
<span style="color: rgba(0, 0, 0, 1)">   ;
}
</span><span style="color: rgba(0, 0, 255, 1)">#pragma</span>mark ----------存储cookie和html本地信息   oc调用html方法
- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">) setCookie{
   NSString </span>*uicps_useridString =<span style="color: rgba(0, 0, 0, 1)"> ;
   NSString </span>*uicps_userPhoneString =<span style="color: rgba(0, 0, 0, 1)"> ;
   NSString </span>*cookieString = ;
   NSString </span>*cookieStringsl = ;
   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存储cookie</span>
   [self.webView evaluateJavaScript:cookieString completionHandler:^(id result, NSError *<span style="color: rgba(0, 0, 0, 1)">error) {
            NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">cookie1-------%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,result);
      }];
   [self.webView evaluateJavaScript:cookieStringsl completionHandler:</span>^(id result, NSError *<span style="color: rgba(0, 0, 0, 1)">error) {
            NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">cookie2-------%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,result);
      }];
   
   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存储本地</span>
   NSString *jsStringsl = ;
       [self.webView evaluateJavaScript:jsStringsl completionHandler:</span>^(id result, NSError *<span style="color: rgba(0, 0, 0, 1)">error) {
         NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">local-------%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,error);
         
       }];
   NSString </span>*jsStrings = ;
       [self.webView evaluateJavaScript:jsStrings completionHandler:</span>^(id result, NSError *<span style="color: rgba(0, 0, 0, 1)">error) {
         NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">local-------%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,error);
   }];
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">测试调用 oc调用html方法
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    NSString*str=;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">         [self.webView evaluateJavaScript:str completionHandler:^(id result, NSError *error) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">                      NSLog(@"result-------%@",result);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">getcookie方法的返回值
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    }];</span>
<span style="color: rgba(0, 0, 0, 1)">   
}
</span>- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">)initView {
      self.userContentController </span>=<span style="color: rgba(0, 0, 0, 1)"> [ init];
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">配置webView</span>
      WKWebViewConfiguration*config =<span style="color: rgba(0, 0, 0, 1)"> [init];
      config.userContentController </span>=<span style="color: rgba(0, 0, 0, 1)"> self.userContentController;
      WKWebView </span>*webView =<span style="color: rgba(0, 0, 0, 1)"> [initWithFrame:self.view.frame configuration:config];
      self.webView</span>=<span style="color: rgba(0, 0, 0, 1)">webView;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">允许手势,后退前进等操作</span>
      self.webView.allowsBackForwardNavigationGestures = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
      ;
   
         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加进度条</span>
      UIProgressView *progressView= [initWithFrame:CGRectMake(<span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">88</span>, self.view.frame.size.width, <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">)];
      self.progressView</span>=<span style="color: rgba(0, 0, 0, 1)">progressView;
      ;
      ;
}



</span><span style="color: rgba(0, 0, 255, 1)">#pragma</span>mark ------观察者
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
观察者观察
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)<span style="color: rgba(0, 0, 255, 1)">object</span> change:(NSDictionary&lt;NSKeyValueChangeKey,id&gt; *)change context:(<span style="color: rgba(0, 0, 255, 1)">void</span> *<span style="color: rgba(0, 0, 0, 1)">)context{
    self.progressView.progress</span>=<span style="color: rgba(0, 0, 0, 1)">self.webView.estimatedProgress;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
观察者观察销毁
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">)dealloc{
    ;
}

</span><span style="color: rgba(0, 0, 255, 1)">#pragma</span>mark -------------------------UIDelegate协议UI弹窗解决弹窗报错
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(<span style="color: rgba(0, 0, 255, 1)">void</span> (^)(<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">))completionHandler{
    UIAlertController </span>*alertController =<span style="color: rgba(0, 0, 0, 1)"> [UIAlertController alertControllerWithTitle:message
                                                                                 message:nil
                                                                        preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">OK</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                                                            style:UIAlertActionStyleCancel
                                                          handler:</span>^(UIAlertAction *<span style="color: rgba(0, 0, 0, 1)">action) {
                                                            completionHandler();
                                                          }]];
    ;
   
}


</span><span style="color: rgba(0, 0, 255, 1)">#pragma</span>mark -------------------------navigationDelegate协议 加载周期
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
是否加载请求 (scheme拦截,特殊逻辑,js和navigation通讯)
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)webView:(WKWebView *)webView:(WKNavigationAction *)navigationAction decisionHandler:(<span style="color: rgba(0, 0, 255, 1)">void</span> (^<span style="color: rgba(0, 0, 0, 1)">)(WKNavigationActionPolicy))decisionHandle{
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">接受全部地址</span>
<span style="color: rgba(0, 0, 0, 1)">       decisionHandle(WKNavigationActionPolicyAllow);
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
webView完成加载
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *<span style="color: rgba(0, 0, 0, 1)">)navigation{
      showToast(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">加载完毕</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      ;
      NSString </span>*useridString = ;
      [webView evaluateJavaScript:useridString completionHandler:</span>^(id result, NSError *<span style="color: rgba(0, 0, 0, 1)">error) {
          NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">result-------%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,result);
      }];
      NSString </span>*cookieString= ;
       [webView evaluateJavaScript:cookieString completionHandler:</span>^(id result, NSError *<span style="color: rgba(0, 0, 0, 1)">error) {
            NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">resultcookieResult-------%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,result);
      }];
}

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
webView加载失败
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *<span style="color: rgba(0, 0, 0, 1)">)error{

}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
webView Crash回掉,自动重新加载
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)webViewWebContentProcessDidTerminate:(WKWebView *<span style="color: rgba(0, 0, 0, 1)">)webView{

}

</span><span style="color: rgba(0, 0, 255, 1)">#pragma</span>mark -----------js方法注册与销毁------------
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注册js方</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">)addScriptMessage{
   ;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
    销毁js方法
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">)viewDidDisappear:(BOOL)animated{
    ;
    ;
}
</span><span style="color: rgba(0, 0, 255, 1)">#pragma</span>mark -----------js交互------------H5调用ios方法
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
重要的事情说三遍!重要的事情说三遍!重要的事情说三遍!
window.webkit.messageHandlers.finashH5.postMessage(1);//只能传一个参数可以是数组、字典等类型不能不传值
window.webkit.messageHandlers.AppModel.postMessage(NULL或者其他参数),参数messageBody里面不能为空什么都不写,不然不会走代理方法
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
- (<span style="color: rgba(0, 0, 255, 1)">void</span>)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *<span style="color: rgba(0, 0, 0, 1)">)message{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">message.name注册的方法名 message.body参数,可以是数组、字典等类型</span>
   <span style="color: rgba(0, 0, 255, 1)">if</span> () {
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">退出H5页面</span>
<span style="color: rgba(0, 0, 0, 1)">         ;
   }
}
</span><span style="color: rgba(0, 0, 255, 1)">#pragma</span>mark 关闭H5页面,用于退出界面
- (<span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">) finashH5:(id)body{
    ;
}
@end</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>&nbsp;十二、</strong>WKWebView与Vue交互:</p>
<p>&nbsp; &nbsp; &nbsp; 参考地址:https://www.jianshu.com/p/02018627e41e</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;ios 调用Vue方法</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;oc中:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将结果返回给js</span>
      NSString *jsStr = ;
      [self.webView evaluateJavaScript:jsStr completionHandler:</span>^(id _Nullable result, NSError *<span style="color: rgba(0, 0, 0, 1)"> _Nullable error) {
          NSLog(</span><span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">%@resultGetcookie----%@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,result, error);//返回结果: 1111
      }];</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; vue中:</p>
<div class="cnblogs_code">
<pre>       <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">挂载方法</span>
<span style="color: rgba(0, 0, 0, 1)">      mounted(){
                window.getCooke</span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getCooke
      },
      methods: {
            closeHtml(){
                window.webkit.messageHandlers.finashH5.postMessage(</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
            },
            getCooke(msg){
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ios中传来的参数</span>
<span style="color: rgba(0, 0, 0, 1)">                alert(msg);
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回给ios参数</span>
                <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1111</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            },
      }</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/huihuizhang/p/12779329.html
頁: [1]
查看完整版本: ios 开发搭建