IOS适配及优化
<h4>1:iOS下载适配</h4>
<p><strong>方式一:iosOpenType=0</strong>
此方式会在APP内的webView中跳转至下载页
采用WKWbview,需要在代理方法里加入以下代码,代码如下:</p>
<pre><code class="language-java">(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
//对外链和跳转 appstore、微信等特殊处理
//适配可能出现的需要跳转到 safari 的情况
NSURL *url = [navigationAction.request URL];
if ([url.absoluteString hasPrefix:@"itms-services"]) {
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
} else {
[[UIApplication sharedApplication] openURL:url];
// Fallback on earlier versions
}
}
else if ([url.absoluteString hasPrefix:@"https://itunes.apple.com/"]){
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}];
} else {
[[UIApplication sharedApplication] openURL:url];
// Fallback on earlier versions
}
}
else if ([url.absoluteString containsString:@"openSafari=1"]) {
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}]; } else {
[[UIApplication sharedApplication] openURL:url];
// Fallback on earlier versions
}
}
decisionHandler(WKNavigationActionPolicyAllow);
} </code></pre>
<p><strong>特别注意:</strong>
此方式下,如果APP需要上架至App Store,itms-services、openSafari=1字符串不能直接出现在工程里,否则提审会被拒。
处理方式:
1、本地处理:把字符串做拆分转化处理
2、服务器:字符串通过接口返回,但要注意避免参数名称和字符串重名或类似。
另外,这个功能需要通过接口控制审核期间隐藏。</p>
<p><strong>方式二:iosOpenType=1(推荐)</strong>
此方式会在游戏详情页点击立即体验按钮时,跳转至手机的Safari浏览器打开下载页</p>
<pre><code class="language-java">-(void)setupView{
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
//注册供js调用的方法
_userContentController =[[WKUserContentController alloc]init];
[_userContentController addScriptMessageHandler:self name:@"StartGameTask"];
configuration.userContentController = _userContentController;
configuration.preferences.javaScriptEnabled = YES;
self.WebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
self.WebView.UIDelegate = self;
self.WebView.navigationDelegate = self;
[self.view addSubview:self.WebView];
}
#pragma mark - 实现注册的供js调用的oc方法 关键
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if(message.name == nil || [message.name isEqualToString:@""]){
return;
}
//message.name js发送的方法名称
if ([message.name isEqualToString:@"StartGameTask"]) {
NSDictionary* dic = message.body;
//url js发送过来的下载链接
NSString *url = [dic[@"url"] description];
NSURL *downloadURL = [NSURL URLWithString:url];
if(@available(iOS 10, *)) {
[[UIApplication sharedApplication] openURL:downloadURL options:@{} completionHandler:nil];
}else{
[[UIApplication sharedApplication] openURL: downloadURL];
}
}
}</code></pre>