如何解决-[NSURLSession dataTaskWithRequest:]在调用JSContext的共享实例时抛出奇怪的错误
我正在尝试从NSURLSession类(群集)的挂钩方法中执行JavaScript,该方法返回LocalDataTask。出于某种原因,我的iOS调整导致应用程序在使用Theos调用我钩住的方法(-[NSURLSession dataTaskWithRequest:])时崩溃。
我不知道为什么会这样,因为我没有收到任何有用的错误消息。
之所以要从UIWebView中获取JSContext,是因为我想利用WebKit中的所有JavaScript对象(例如XMLHttpRequest)。
我的Tweak.xm文件中包含以下代码:
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
@interface myJSContext : NSObject
+(JSContext *)sharedContext;
@end
@implementation myJSContext
+(JSContext *)sharedContext {
static JSContext * jsCtxt = nil;
static UIWebView * webView = nil;
static dispatch_once_t onceToken;
if (!webView) {
dispatch_once(&onceToken,^{
webView = [[%c(UIWebView) alloc] init];
jsCtxt = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
});
}
return jsCtxt;
}
@end
%hook SpringBoard
-(void)applicationDidFinishLaunching:(id)arg1 {
%orig;
NSLog(@"%@",[myJSContext sharedContext]); // prints the pointer as expected
NSLog(@"%@",[[myJSContext sharedContext] evaluateScript:@"'js test result'.toString()"]); // successfully executes JavaScript as expected
}
%end
%hook NSURLSession
-(id)dataTaskWithRequest:(id)arg1 {
NSLog(@"%@",[myJSContext sharedContext]); // this causes an error,I've tried calling this from the main and global thread which didn't work either
return %orig;
}
%end
我没有看到任何有用的错误消息,但是我将包括可能相关的日志。
编辑:我无法在此处发布所有日志消息,因为该站点认为它“看起来像垃圾邮件” ...
证明已创建JSContext实例并起作用:
Aug 17 07:04:55 SpringBoard(TestTweak.dylib)[7402] <Notice>: js test result
我只能看到以下错误消息:
Aug 17 07:07:00 MobileSafari(SafariServices)[7464] <Error>: Displaying webpage loading error to user: Error Domain=WebKitErrorDomain Code=300,networkTaskDescription: (null).
Aug 17 07:07:00 MobileSafari(WebKit)[7464] <Error>: 0x104fcbc00 - ProcessAssertion::processAssertionWasInvalidated()
Aug 17 07:16:41 runningboardd(RunningBoard)[39] <Notice>: [xpcservice<com.apple.WebKit.Networking>:7504] Error 45 setting darwin role to NonUserInteractive: Operation not supported,falling back to setting priority
Aug 17 07:16:41 com.apple.WebKit.Networking(CFNetwork)[7504] <Notice>: Faulting in NSHTTPCookieStorage singleton
Aug 17 07:16:41 com.apple.WebKit.Networking(CFNetwork)[7504] <Notice>: Faulting in CFHTTPCookieStorage singleton
这些错误消息都没有明确告诉我出了什么问题。
为什么从属于SpringBoard的钩子方法调用此方法能正常工作,但从属于NSURLSession的钩子方法调用时却无效?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。