Cocos2d-x iOS 提审ITMS-90809

遇到的问题

用cocos2dx3.17.2打的iOS包,提审商店时遇到的问题。

ITMS-90809: Deprecated API Usage - New apps that use UIWebView are no longer accepted. Instead, use WKWebView for improved security and reliability. Learn more(https://developer.apple.com/documentation/uikit/uiwebview).

参考的文章

CSDN博客

Cocos论坛

GitHub issue

解决办法

  1. 添加需要用到的库WebKit.frameworkAVKit.frameworkCoreMedia.framework

  2. 修改cmake/Modules/CocosConfigDepend.cmake文件。截取了修改这一段:

    elseif(IOS)
        # Locate system libraries on iOS
        find_library(UIKIT_LIBRARY UIKit)
        find_library(OPENGLES_LIBRARY OpenGLES)
        find_library(CORE_MOTION_LIBRARY CoreMotion)
        find_library(AVKIT_LIBRARY AVKit)
        find_library(CORE_MEDIA_LIBRARY CoreMedia)
        find_library(CORE_TEXT_LIBRARY CoreText)
        find_library(SECURITY_LIBRARY Security)
        find_library(CORE_GRAPHICS_LIBRARY CoreGraphics)
        find_library(AV_FOUNDATION_LIBRARY AVFoundation)
        find_library(Z_LIBRARY z)
        find_library(WEBKIT_LIBRARY WebKit)
        list(APPEND PLATFORM_SPECIFIC_LIBS
                ${UIKIT_LIBRARY}
                ${OPENGLES_LIBRARY}
                ${CORE_MOTION_LIBRARY}
                ${AVKIT_LIBRARY}
                ${CORE_MEDIA_LIBRARY}
                ${CORE_TEXT_LIBRARY}
                ${SECURITY_LIBRARY}
                ${CORE_GRAPHICS_LIBRARY}
                ${AV_FOUNDATION_LIBRARY}
                ${Z_LIBRARY}
                ${WEBKIT_LIBRARY}
                ${COCOS_APPLE_LIBS}
                )
    endif()
    
  3. 修改cocos/ui/UIWebViewImpl-ios.mm

    /****************************************************************************
    Copyright (c) 2014-2016 Chukong Technologies Inc.
    Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
    
    http://www.cocos2d-x.org
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    ****************************************************************************/
    
    #include "platform/CCPlatformConfig.h"
    
    // Webview not available on tvOS
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
    
    #import <WebKit/WKWebView.h>
    #import <WebKit/WKUIDelegate.h>
    #import <WebKit/WKNavigationDelegate.h>
    #import <WebKit/WKNavigationAction.h>
    
    #include "ui/UIWebViewImpl-ios.h"
    #include "renderer/CCRenderer.h"
    #include "base/CCDirector.h"
    #include "platform/CCGLView.h"
    #include "platform/ios/CCEAGLView-ios.h"
    #include "platform/CCFileUtils.h"
    #include "ui/UIWebView.h"
    
    @interface UIWebViewWrapper : NSObject
    @property (nonatomic) std::function<bool(std::string url)> shouldStartLoading;
    @property (nonatomic) std::function<void(std::string url)> didFinishLoading;
    @property (nonatomic) std::function<void(std::string url)> didFailLoading;
    @property (nonatomic) std::function<void(std::string url)> onJsCallback;
    
    @property(nonatomic, readonly, getter=canGoBack) BOOL canGoBack;
    @property(nonatomic, readonly, getter=canGoForward) BOOL canGoForward;
    
    + (instancetype)newWebViewWrapper;
    
    - (void)setVisible:(bool)visible;
    
    - (void)setBounces:(bool)bounces;
    
    - (void)setOpacityWebView:(float)opacity;
    
    - (float)getOpacityWebView;
    
    - (void)setBackgroundTransparent;
    
    - (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height;
    
    - (void)setJavascriptInterfaceScheme:(const std::string &)scheme;
    
    - (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL;
    
    - (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL;
    
    - (void)loadUrl:(const std::string &)urlString cleanCachedData:(BOOL) needCleanCachedData;
    
    - (void)loadFile:(const std::string &)filePath;
    
    - (void)stopLoading;
    
    - (void)reload;
    
    - (void)evaluateJS:(const std::string &)js;
    
    - (void)goBack;
    
    - (void)goForward;
    
    - (void)setScalesPageToFit:(const bool)scalesPageToFit;
    @end
    
    
    @interface UIWebViewWrapper () <WKUIDelegate, WKNavigationDelegate>
    @property(nonatomic, retain) WKWebView *wkWebView;
    
    @property(nonatomic, copy) NSString *jsScheme;
    @end
    
    @implementation UIWebViewWrapper {
        
    }
    
    + (instancetype) newWebViewWrapper {
        return [[self alloc] init];
    }
    
    - (instancetype)init {
        self = [super init];
        if (self) {
            self.wkWebView = nil;
            self.shouldStartLoading = nullptr;
            self.didFinishLoading = nullptr;
            self.didFailLoading = nullptr;
        }
        return self;
    }
    
    - (void)dealloc {
        self.wkWebView.UIDelegate = nil;
        self.wkWebView.navigationDelegate = nil;
        [self.wkWebView removeFromSuperview];
        [self.wkWebView release];
        self.wkWebView = nil;
        self.jsScheme = nil;
        [super dealloc];
    }
    
    - (void)setupWebView {
        if (!self.wkWebView) {
            self.wkWebView = [[WKWebView alloc] init];
            self.wkWebView.UIDelegate = self;
            self.wkWebView.navigationDelegate = self;
        }
        if (!self.wkWebView.superview) {
            auto view = cocos2d::Director::getInstance()->getOpenGLView();
            auto eaglview = (CCEAGLView *) view->getEAGLView();
            [eaglview addSubview:self.wkWebView];
        }
    }
    
    - (void)setVisible:(bool)visible {
        if (!self.wkWebView) {[self setupWebView];}
        self.wkWebView.hidden = !visible;
    }
    
    - (void)setBounces:(bool)bounces {
    self.wkWebView.scrollView.bounces = bounces;
    }
    
    - (void)setOpacityWebView:(float)opacity {
        if (!self.wkWebView) { [self setupWebView]; }
        self.wkWebView.alpha = opacity;
        [self.wkWebView setOpaque:YES];
    }
    
    -(float) getOpacityWebView{
        return self.wkWebView.alpha;
    }
    
    -(void) setBackgroundTransparent{
        if (!self.wkWebView) {[self setupWebView];}
        [self.wkWebView setOpaque:NO];
        [self.wkWebView setBackgroundColor:[UIColor clearColor]];
    }
    
    - (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height {
        if (!self.wkWebView) {[self setupWebView];}
        CGRect newFrame = CGRectMake(x, y, width, height);
        if (!CGRectEqualToRect(self.wkWebView.frame, newFrame)) {
            self.wkWebView.frame = CGRectMake(x, y, width, height);
        }
    }
    
    - (void)setJavascriptInterfaceScheme:(const std::string &)scheme {
        self.jsScheme = @(scheme.c_str());
    }
    
    - (void)loadData:(const std::string &)data MIMEType:(const std::string &)MIMEType textEncodingName:(const std::string &)encodingName baseURL:(const std::string &)baseURL {
        auto path = [[NSBundle mainBundle] resourcePath];
        path = [path stringByAppendingPathComponent:@(baseURL.c_str() )];
        auto url = [NSURL fileURLWithPath:path];
    
        [self.wkWebView loadData:[NSData dataWithBytes:data.c_str() length:data.length()]
                        MIMEType:@(MIMEType.c_str())
        characterEncodingName:@(encodingName.c_str())
                        baseURL:url];
    }
    
    - (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL {
        if (!self.wkWebView) {[self setupWebView];}
        auto path = [[NSBundle mainBundle] resourcePath];
        path = [path stringByAppendingPathComponent:@(baseURL.c_str() )];
        auto url = [NSURL fileURLWithPath:path];
        [self.wkWebView loadHTMLString:@(string.c_str()) baseURL:url];
    }
    
    - (void)loadUrl:(const std::string &)urlString cleanCachedData:(BOOL) needCleanCachedData {
        if (!self.wkWebView) {[self setupWebView];}
        NSURL *url = [NSURL URLWithString:@(urlString.c_str())];
    
        NSURLRequest *request = nil;
        if (needCleanCachedData)
            request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
        else
            request = [NSURLRequest requestWithURL:url];
    
        [self.wkWebView loadRequest:request];
    }
    
    - (void)loadFile:(const std::string &)filePath {
        if (!self.wkWebView) {[self setupWebView];}
        NSURL *url = [NSURL fileURLWithPath:@(filePath.c_str())];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.wkWebView loadRequest:request];
    }
    
    - (void)stopLoading {
        [self.wkWebView stopLoading];
    }
    
    - (void)reload {
        [self.wkWebView reload];
    }
    
    - (BOOL)canGoForward {
        return self.wkWebView.canGoForward;
    }
    
    - (BOOL)canGoBack {
        return self.wkWebView.canGoBack;
    }
    
    - (void)goBack {
        [self.wkWebView goBack];
    }
    
    - (void)goForward {
        [self.wkWebView goForward];
    }
    
    - (void)evaluateJS:(const std::string &)js {
        if (!self.wkWebView) {[self setupWebView];}
        [self.wkWebView evaluateJavaScript:@(js.c_str()) completionHandler:nil];
    }
    
    - (void)setScalesPageToFit:(const bool)scalesPageToFit {
    // TODO: there is not corresponding API in WK.
    // https://stackoverflow.com/questions/26295277/wkwebview-equivalent-for-uiwebviews-scalespagetofit/43048514 seems has a solution,
    // but it doesn't support setting it dynamically. If we want to set this feature dynamically, then it will be too complex.
    }
    
    
    
    #pragma mark - WKNavigationDelegate
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
        NSString *url = [webView.URL absoluteString];
        if ([[webView.URL scheme] isEqualToString:self.jsScheme]) {
            self.onJsCallback([url UTF8String]);
            decisionHandler(WKNavigationActionPolicyCancel);
            return;
        }
        if (self.shouldStartLoading && url) {
            if (self.shouldStartLoading([url UTF8String]) )
                decisionHandler(WKNavigationActionPolicyAllow);
            else
                decisionHandler(WKNavigationActionPolicyCancel);
    
            return;
        }
    
        decisionHandler(WKNavigationActionPolicyAllow);
    }
    
    - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
        if (self.didFinishLoading) {
            NSString *url = [webView.URL absoluteString];
            self.didFinishLoading([url UTF8String]);
        }
    }
    
    - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
        if (self.didFailLoading) {
            NSString *errorInfo = error.userInfo[NSURLErrorFailingURLStringErrorKey];
            if (errorInfo) {
                self.didFailLoading([errorInfo UTF8String]);
            }
        }
    }
    
    #pragma WKUIDelegate
    
    // Implement js alert function.
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)())completionHandler
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message
                                                                                message:nil
                                                                        preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"Ok"
                                                            style:UIAlertActionStyleCancel
                                                        handler:^(UIAlertAction *action) {
                                                            completionHandler();
                                                        }]];
    
        auto rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        [rootViewController presentViewController:alertController animated:YES completion:^{}];
    }
    
    @end
    
    
    
    namespace cocos2d {
    namespace experimental {
        namespace ui{
    
    WebViewImpl::WebViewImpl(WebView *webView)
            : _uiWebViewWrapper([UIWebViewWrapper newWebViewWrapper]),
            _webView(webView) {
                
        _uiWebViewWrapper.shouldStartLoading = [this](std::string url) {
            if (this->_webView->_onShouldStartLoading) {
                return this->_webView->_onShouldStartLoading(this->_webView, url);
            }
            return true;
        };
        _uiWebViewWrapper.didFinishLoading = [this](std::string url) {
            if (this->_webView->_onDidFinishLoading) {
                this->_webView->_onDidFinishLoading(this->_webView, url);
            }
        };
        _uiWebViewWrapper.didFailLoading = [this](std::string url) {
            if (this->_webView->_onDidFailLoading) {
                this->_webView->_onDidFailLoading(this->_webView, url);
            }
        };
        _uiWebViewWrapper.onJsCallback = [this](std::string url) {
            if (this->_webView->_onJSCallback) {
                this->_webView->_onJSCallback(this->_webView, url);
            }
        };
    }
    
    WebViewImpl::~WebViewImpl(){
        [_uiWebViewWrapper release];
        _uiWebViewWrapper = nullptr;
    }
    
    void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) {
        [_uiWebViewWrapper setJavascriptInterfaceScheme:scheme];
    }
    
    void WebViewImpl::loadData(const Data &data,
                            const std::string &MIMEType,
                            const std::string &encoding,
                            const std::string &baseURL) {
        
        std::string dataString(reinterpret_cast<char *>(data.getBytes()), static_cast<unsigned int>(data.getSize()));
        [_uiWebViewWrapper loadData:dataString MIMEType:MIMEType textEncodingName:encoding baseURL:baseURL];
    }
    
    void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) {
        [_uiWebViewWrapper loadHTMLString:string baseURL:baseURL];
    }
    
    void WebViewImpl::loadURL(const std::string &url) {
        this->loadURL(url, false);
    }
    
    void WebViewImpl::loadURL(const std::string &url, bool cleanCachedData) {
        [_uiWebViewWrapper loadUrl:url cleanCachedData:cleanCachedData];
    }
    
    void WebViewImpl::loadFile(const std::string &fileName) {
        auto fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName);
        [_uiWebViewWrapper loadFile:fullPath];
    }
    
    void WebViewImpl::stopLoading() {
        [_uiWebViewWrapper stopLoading];
    }
    
    void WebViewImpl::reload() {
        [_uiWebViewWrapper reload];
    }
    
    bool WebViewImpl::canGoBack() {
        return _uiWebViewWrapper.canGoBack;
    }
    
    bool WebViewImpl::canGoForward() {
        return _uiWebViewWrapper.canGoForward;
    }
    
    void WebViewImpl::goBack() {
        [_uiWebViewWrapper goBack];
    }
    
    void WebViewImpl::goForward() {
        [_uiWebViewWrapper goForward];
    }
    
    void WebViewImpl::evaluateJS(const std::string &js) {
        [_uiWebViewWrapper evaluateJS:js];
    }
    
    void WebViewImpl::setBounces(bool bounces) {
        [_uiWebViewWrapper setBounces:bounces];
    }
    
    void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) {
        [_uiWebViewWrapper setScalesPageToFit:scalesPageToFit];
    }
    
    void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) {
        if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) {
            
            auto director = cocos2d::Director::getInstance();
            auto glView = director->getOpenGLView();
            auto frameSize = glView->getFrameSize();
            
            auto scaleFactor = [static_cast<CCEAGLView *>(glView->getEAGLView()) contentScaleFactor];
    
            auto winSize = director->getWinSize();
    
            auto leftBottom = this->_webView->convertToWorldSpace(cocos2d::Vec2::ZERO);
            auto rightTop = this->_webView->convertToWorldSpace(cocos2d::Vec2(this->_webView->getContentSize().width, this->_webView->getContentSize().height));
    
            auto x = (frameSize.width / 2 + (leftBottom.x - winSize.width / 2) * glView->getScaleX()) / scaleFactor;
            auto y = (frameSize.height / 2 - (rightTop.y - winSize.height / 2) * glView->getScaleY()) / scaleFactor;
            auto width = (rightTop.x - leftBottom.x) * glView->getScaleX() / scaleFactor;
            auto height = (rightTop.y - leftBottom.y) * glView->getScaleY() / scaleFactor;
    
            [_uiWebViewWrapper setFrameWithX:x
                                        y:y
                                    width:width
                                    height:height];
        }
    }
    
    void WebViewImpl::setVisible(bool visible){
        [_uiWebViewWrapper setVisible:visible];
    }
            
    void WebViewImpl::setOpacityWebView(float opacity){
        [_uiWebViewWrapper setOpacityWebView: opacity];
    }
            
    float WebViewImpl::getOpacityWebView() const{
        return [_uiWebViewWrapper getOpacityWebView];
    }
    
    void WebViewImpl::setBackgroundTransparent(){
        [_uiWebViewWrapper setBackgroundTransparent];
    }
    
            
        } // namespace ui
    } // namespace experimental
    } //namespace cocos2d
    
    #endif // CC_TARGET_PLATFORM == CC_PLATFORM_IOS
    
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容