I am trying to execute JavaScript in a hooked method from the NSURLSession class (cluster) which returns a LocalDataTask. For some reason my iOS tweak causes apps to crash when they call the method that I've hooked (-[NSURLSession dataTaskWithRequest:]) using Theos.
I have no idea why this is happening because I am not getting any useful error messages.
The reason why I am grabbing the JSContext from a UIWebView is because I want to make use of all the JavaScript objects from WebKit (e.g. XMLHttpRequest).
I have the following code in my Tweak.xm file:
#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
I don't see any useful error messages, but I will include the logs of potential relevance.
EDIT: I cannot post all the log messages here because this site thinks it "looks like spam"...
Proof that the JSContext instance is created and functions:
Aug 17 07:04:55 SpringBoard(TestTweak.dylib)[7402] <Notice>: js test result
The only error messages that I can see are the following:
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
None of those error messages explicitly tell me what went wrong.
Why does this work fine when called from a hooked method that belongs to SpringBoard, but not when called from a hooked method that belongs to NSURLSession?