NSNotification with multithreading XCODE - multithreading

I am a newbie to IPHONE development. I am facing a issue in working with NSNotification with multithreading.
I have a few images in a gallery.I select the image. Selected images get stored in core data.I have a button (upload). When i click on it i need to show a hud with a NSNotification saying (Uploading with image name). ie. "uploading image1.jpg" then i need to call the next thread to display "uploading image2.jpg" and so on. I need a sample code for this.
I need to know how to send and receive NSNotification with multithreading. Kindly help me in this issue.
Thanks in advance.

Consider using MBProgressHUD for this.
The demo project include examples very similar to what you're doing.
The components also has other feature you may want, like a progress indicator.
From the main page, configuring an async task to have a HUD notification is as simple as:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});

Related

Dismiss UIAlertView programmatically in Xamarin.iOS

I am stuck in a weird scenario where I need to programmatically dismiss the active UIAlertView from the other viewcontroller by getting the instance.
Actually, I am working on AutoClose feature (working on System.Timer) where when Timer would be elapsed, I need to perform some operation (performs in View Model) and close the active UIAlertView or UIViewController window and Navigate To HomeView.
I am able to do with UIViewController but I couldn't find any way to dismiss the UIAlertView which is presented from another view controller?
Note - I understand this API is deprecated but for using UIAlertController it would lead to ample code changes, and I don't have time to make them.
I am using below method to accomplish but once the AlertView gets dismissed I am unable to click anywhere on screen.
UIWindow window = UIApplication.SharedApplication.KeyWindow;
UIViewController rootViewController=window.RootViewController;
if(rootViewController.ModalViewController is UIAlertController)
{
rootViewController.ModalViewController.DismissViewController(true,null);
}
Also, may I know how do we see the hierarchy of Views in Xamarin Studio?
Try to use the following code:
UIAlertView alertView = new UIAlertView("Title", "Content",this,"OK");
alertView.Show();
UIActivityIndicatorView indicatorView = new UIActivityIndicatorView();
indicatorView.BackgroundColor = UIColor.Red;
indicatorView.Center = new CGPoint(alertView.Bounds.Size.Width / 2,
alertView.Bounds.Size.Height - 40.0);
indicatorView.StartAnimating();
alertView.InsertSubview(indicatorView,0);
And if when you want to Dissmiss the AlertView(for emample in the following code I dissmiss it after 3 seconds):
NSTimer.CreateScheduledTimer(3, (t) =>
{
alertView.DismissWithClickedButtonIndex(0, true);
});
I have asked the same question in Xamarin Forum and I got my answer from one of the Xamarin Professional which I am sharing here.
Dismiss UIAlertView programmatically in Xamarin.iOS
I hope this may help others.

Error on TextView with detection phone number IOS 9

I'm testing my app on an iPod Touch running iOS 9 (on iOS 8.4 it was working for other functions: FaceTime, copy to contacts, etc.). I have a textview with phone number detection and I receive the following error:
Warning: Attempt to present <_UIRotatingAlertController: 0x16250e00> on whose view is not in the window hierarchy!
Assertion failure in -[UITextView startInteractionWithLinkAtPoint:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3505.16/UITextView_LinkInteraction.m:377
Any fix to this?
Not a perfect solution but very simple and may help a desperate developer:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
[[UIApplication sharedApplication] openURL:URL];
return NO;
}
You will lose the Copy, Open URL, Cancel popover on long press but you SHOULD at least be able to open url same as you would previously. In iOS 9, this still opens a browser window inside your app for standard URLs (which is nice).
This seems like an Apple bug (posted to radar already).
We were seeing very similar error when trying to open links in a modal view since Apple is trying to display a new modal alert view. Outside of modal view data detection worked just fine in iOS 9 for us.
This looks quite similar to a bug we recently had to fix for PSPDFKit where presenting sheets did not work when the rootViewController was already presenting another controller. (Is your rootViewController maybe not set?)
You can read the source code here. This might help you to figure out where the issue is for you:
https://gist.github.com/steipete/b00fc02aa9f1c66c11d0f996b1ba1265
And please dupe rdar://26295020 so this will get hopefully fixed in time for iOS 10. (The bug exists since iOS 8 and was first reported on iOS 8b5.)

WKWebview does not run javascript(XML HTTP request) with out adding a parent view

I am updating my project from UIWebView to WKWebView. In existing UIWebView approach where UIWebView does't have any parent until it runs javascript and once it has content, it adds a parent to the webview. Content renders fine using UIWebView. I am using the same approach for WKWebView, however WKWebview stuck at loading. Is there any way we can execute javascript on wkwebview without adding parent to the webview. Another interesting thing is it works on simulator not on device. Using localHTTPServer to serve the html, css and images.
Any Suggestions or Tips to solve the above issue.
I have observed similar behavior in my app running socket.io in a headless WKWebView. In my case, the web socket would disconnect all the time and not reconnect.
It seems that since WKWebView runs javascript off-process that it will pause any javascript that is considered idle to save resources (<-IMO). Idle includes not having a parent or the app/device is inactive. So, yes, it seems like you are correct in that it needs a parent view in most cases. I was able to work around this using the following code:
WKWebViewConfiguration* webViewConfig = // set up config
// provide empty frame rect
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:webViewConfig];
// add to keyWindow to ensure it is 'active'
[UIApplication.sharedApplication.keyWindow addSubview:webView];
This approach at least ensures the javascript runs while the app is active and doesn't effect the UI. If you need to display it later you can remove the webView from the keyWindow after render and reset the frame. And, yes, I also observed that in the simulator this is not an issue (javascript ALWAYS runs, regardless of parent or app state)
Hope this helps!
The accepted solution worked for me some of the time. I tried something else which appears to work for me consistently:
self.keepWebViewActiveTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:#selector(_keepWKWebViewActive:)
userInfo:nil
repeats:YES];
Where _keepWKWebViewActive is:
-(void) _keepWKWebViewActive:(NSTimer*) timer{
if(self.webView) {
[self.webView evaluateJavaScript:#"1+1" completionHandler:nil];
}
}

ios7 navigationbar causes EXC_BAD_ACCESS after VC with keyboard shown dismiss

I have a Notes-like app: UITableViewController showing up individual notes by pushing them onto navigation stack.
The problem arises when I have UITextView with the FirstResponder status (keyboard is shown) and I touch Back button. The current view controller is dismissed with the animation as expected, BUT the navigation bar is broken now! If I press any of the bar buttons, it will cause EXC_BAD_ACCESS.
I would say that it was not transitioned properly. The table VC is broken somehow as well, as it may appear empty on further manipulations... Very strange behaviour!
By the way, it did not cause any problems with iOS5 and iOS6, but there I used a custom chevron Back button.
I've checked the standard Notes app and it works like a charm.
What is the trick?
Thanks a lot for your advice!
EXC_BAD_ACCESS means you are trying to access an object that has been deallocated. Best thing you can do to trace this is enabling NSZombie, it will tell you what released object is being sent a message (aka EXC_BAD_ACCESS).
You can get how to enable it from here.
I got it and will try to explain to help somebody else to save their day...
EXC_BAD_ACCESS was raised because UITableViewController was not properly transitioned to during Back pop-animation (its viewWillAppear: and viewDidAppear: method were not triggered at all).
In its turn, the animation was not properly performed, as popViewControllerAnimated: was called twice or even more times: 1) as part of the system Back-button callback; 2) inside textViewDidEndEditing: in case no text was entered.
The solution is to check whether the back button has been pressed before calling popViewControllerAnimated:. The trick is to check if the detail-view controller is still in the navigation stack.
Here is the helper method:
-(void) returnToTheListOfRecords {
self.textView.delegate = nil; // this is to avoid the second call of `textViewDidEndEditing:`
if ([self.navigationController.viewControllers indexOfObject:self.delegate]==NSNotFound) {
// Back button has been pressed.
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
The problem happens on iOS7 only due to its brand-new animation.

MotionEnded is not called continuously

There is a list of videos in main view. In that main view, i have added a UIWebView as subview to play videos.
I suppose to do, when i shake iPhone it should play the next video in UIWebView.
It is working fine for the very first time but as come back to main view and again try to perform the same thing, it is not reacting.
Any solution please?
Make sure when the view (re)appears that you set it as the first responder.
-(void)viewDidAppear:(BOOL)animated
{
[self becomeFirstResponder];
}
It needs to be in viewDidAppear and not viewWillAppear

Resources