因此,我已经尝试了一个多星期来让谷歌用户消息传递平台正常工作(这个问题将在接下来的几周/几个月内到处出现)。
问题是启动guid是用obj C写的,我看不懂!
我已经设法将 obj C 代码插入到我的 swift 项目中,它可以运行,但在尝试呈现表单时会引发异常。
我认为这是因为它是一个 .m 文件,并且它试图从 self 呈现表单,并且“self”不是视图控制器,那么它会崩溃吗?
obj C 代码(如谷歌网站推荐)在这里:
#import "CustomObject.h"
@implementation CustomObject
- (void) someMethod {
// Create a UMPRequestParameters object.
UMPRequestParameters *parameters = [[UMPRequestParameters alloc] init];
// Set tag for under age of consent. Here NO means users are not under age.
parameters.tagForUnderAgeOfConsent = NO;
// Request an update to the consent information.
[UMPConsentInformation.sharedInstance
requestConsentInfoUpdateWithParameters:parameters
completionHandler:^(NSError *_Nullable error) {
if (error) {
// Handle the error.
} else {
// The consent information state was updated.
// You are now ready to check if a form is
// available.
UMPFormStatus formStatus = UMPConsentInformation.sharedInstance.formStatus;
if (formStatus == UMPFormStatusAvailable) {
[self loadForm];
}
}
}];
}
-(void) loadForm {
[UMPConsentForm loadWithCompletionHandler:^(UMPConsentForm *form, NSError *loadError) {
if (loadError) {
// Handle the error
} else {
// Present the form. You can also hold on to the reference to present
// later.
if (UMPConsentInformation.sharedInstance.consentStatus ==
UMPConsentStatusRequired) {
[form
presentFromViewController: self
completionHandler:^(NSError *_Nullable dismissError) {
if (UMPConsentInformation.sharedInstance.consentStatus ==
UMPConsentStatusObtained) {
// App can start requesting ads.
}
}];
} else {
// Keep the form available for changes to user consent.
}
}
}];
}
@end
我在这里拔掉我的头发。
我真正需要的是
A。将此代码转换为 swift 以便我可以理解它或
b.谁能建议我需要对 presentfromviewcontroller 函数做什么以阻止它抛出异常并呈现表单?
谢谢.....
好吧,既然我的问题没有得到回应,我终于设法通过将 obj c 翻译成 swift 来让它工作。
我认为将其发布在这里会很有帮助,以防其他人遇到同样的问题。
这是我想出的代码。将这 2 个函数添加到视图控制器中,并从 viewdidload 调用第一个函数。
注释掉的位是调试集,允许您尝试不同的地理位置等以确保其工作。
这段代码可以工作,但我不太擅长快速,所以请随意指出错误或纠正它。
不要忘记在顶部添加 import userMessagingPlatform ...
func setPermissions() {
// UMPConsentInformation.sharedInstance.reset() // always render a consent form
// let debugSettings = UMPDebugSettings()
// debugSettings.testDeviceIdentifiers = ["your test ID here"]
// debugSettings.geography = .notEEA
// parameters.debugSettings = debugSettings
let parameters = UMPRequestParameters()
parameters.tagForUnderAgeOfConsent = false
UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters, completionHandler: {(error) in
if let error = error as NSError? {
// deal with error
} else { // now check for available form and load it
let formStatus = UMPConsentInformation.sharedInstance.formStatus
if formStatus == .available {
self.loadForm()
}
}
})
}
func loadForm() {
UMPConsentForm.load(completionHandler: { (form, loadError) in
if let error = loadError as NSError? {
// deal with error loading form
} else {
if UMPConsentInformation.sharedInstance.consentStatus == .required {
form?.present(from: self, completionHandler: {(error) in
if UMPConsentInformation.sharedInstance.consentStatus == .obtained {
// OK to serve ads
} else {
// not OK to serve ads
}
})
}
}
})
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)