NSURLConnection Blocking UI - uiscrollview

I am trying to run an asynchronous NSURLConnection but its causing my UI to be blocked everytime i receive a response. I was under the impression that an async connection does not block the UI i have also setup a runloop so i can respond to incoming touch events.
Below is a snippet of the code
self.playerConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[self.playerConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[self.playerConnection start];
Any suggestions ?

Related

How to prevent setting of [NSApp dockTile] badgeLabel value that is set not by my code?

I have a push notifications with badge, sounds and alerts enabled.
If the program is not running - everything is fine. But when the program is running - I need to block all sounds, alerts and badges that are not generated by program, because I have a live connection to my server and receiving all events before APNS sends notifications to my mac device.
I've found the way to hide alerts, but I couldn't find any way to take over control on dockTile's icon badge.
if i do this:
-(void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSApp dockTile] setBadgeLabel:nil];
}
nothing is happened, the badge that is set by APNS still persists. I tried to KVO on badgeLabel or dockTileNumber property as shown here, but observeValueForKeyPath:ofObject:change:context: is never called. How do APNS sets the badgeLabel? Maybe I am doing something wrong and there is a correct way to disable alerts/sounds/badges when the program is running?
Since i did not found any solutions to do it correctly, i will accept my workaround:
-(void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[application dockTile].badgeLabel = #" ";
[application dockTile].badgeLabel = #"";
}
Alerts may be disabled via NSUserNotificationCenterDelegate delegate method userNotificationCenter:shouldPresentNotification::
-(BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
{
//apple push notification alert will contain userInfo with aps payload, so disable here
if (notification.userInfo[#"aps"])
return NO;
return YES;
}
I haven't found any ways to disable sounds.

Webview did finish load for frame not called for HTML5 video players

I am using WebView to play few flash videos, but in some cases the URL received can be a MP4 file. In this case it will play using inbuilt HTML5 player. I need to know when webpage has finished loading as I need to inject Javascript. But it wont get called if its a HTML5 player.
I have set the frameload & resource load delegates and I can see that its getting called if its any other web page.
Have you tried adding an observer for your webview?
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:webView];
And then you can call your javascript code inside:
- (void)_webViewProgressFinished:(NSNotification *)notification
{
[[webView windowScriptObject] callWebScriptMethod:#"YourJavascriptMethod" withArguments:
[NSArray arrayWithObjects: nil]];
}

WebView loads slowly in Mac OS

I have created plugin for Mail.app on Mac OS.
I'm using WebView to display web pages, all would be fine but web pages load slowly.
Then I created test cocoa application to compare loading time.
I was surprised when the test application loads page ~5 times faster.
In developer bar I saw my test application receives 304 code that indicates "the resource for the requested URL has not changed and cached resource can be used".
In contrast to the test application the plugin always receives 200 http code and loads resource again.
Maybe I should specify to use a cache in the webview, or I have some bundle permissions problems.
In the plugin, I tried to specify SharedURLCache like this
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1024*1024*20
diskCapacity:1024*1024*5
diskPath:NSHomeDirectory()];
[NSURLCache setSharedURLCache:cache];
Then I tried subscribe to the ResourceLoadDelegate on the WebView and change request object like this
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
{
if ([request cachePolicy] != NSURLRequestReturnCacheDataElseLoad)
{
return [NSURLRequest requestWithURL:[request URL]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:[request timeoutInterval]];
} else {
return request;
}
}
Also I tried to change properties on WebView
[[webView preferences] setUsesPageCache:YES];
[[webView preferences] setCacheModel:WebCacheModelPrimaryWebBrowser];
but it's all not working.
Thanks for help.

Problem when submitting job using XGrid API

I'm attempting to submit a job using the XGrid API. I've opened a connection, and that seems to work ok, although it sometimes doesn't open, but when I do the following:
jobSubmissionMonitor = [controller performSubmitJobActionWithJobSpecification:jobSpecification gridIdentifier: nil];
[jobSubmissionMonitor addObserver:self forKeyPath:#"outcome" options:0 context:NULL];
My observeValueForKeyPath is never called. Any idea what that would be?
I've solved this. Turns out that Xgrid does not seem to work well when you've got the garbage collector enabled.

Using web view behind a proxy (cocoa)

I'm creating a web-browser type app (using a web view object) that needs to be able to connect to the internet via a proxy. Server, port, username and password can all be hardcoded into the app but unfortunately I have no idea how to customise the proxy settings of a web view without changing the system wide proxy settings.
If you know how to do this please provide some example code, thanks a lot!
(Also, if it changes anything - I'm developing for mac, not iPhone)
The easiest way I know is to wire up a UIWebView delegate and listen to all requests before they go through, and redirect the ones you care about through ASIHttpRequest and your custom proxy settings.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// Configure a proxy server manually
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com/ignore"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setProxyHost:#"192.168.0.1"];
[request setProxyPort:3128];
// Alternatively, you can use a manually-specified Proxy Auto Config file (PAC)
// (It's probably best if you use a local file)
[request setPACurl:[NSURL URLWithString:#"file:///Users/ben/Desktop/test.pac"]];
// fire the request async
[request setDelegate:self];
[request startAsynchronous];
return NO;
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
// todo: save data to disk and load with [self webView]
}
It's a bit wonky,but it should work. Just remember to manage your memory properly and don't use this leaky example code... YMMV, I haven't even tested if this compiles, typed it all in the browser window with some copy and paste hackery.

Resources