Setting the AVAudioSession category in AppDelegate.m - objective-c

So I hate to have to ask this question but I've spent a fair bit of time searching through Apple's documentation and Google with no avail. I'm simply trying to set the AVAudioSession category for my app ONCE, when the applicationDidFinishLaunching. I have an app that plays an audio stream and I would like it to continue playing when the app enters the background, so I'm trying to use the Playback category. Here is my code for AppDelegate.m :
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Set AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
[[AVAudioSession sharedInstance] setDelegate:self];
// create window and set up navigation controller
[window addSubview:myNavController.view];
[window makeKeyAndVisible];
}
# pragma mark -
# pragma mark AVAudioSession Delegate Methods
- (void)beginInterruption {
}
- (void)endInterruption {
}
- (void)endInterruptionWithFlags:(NSUInteger)flags {
}
- (void)inputIsAvailableChanged:(BOOL)isInputAvailable {
}
With this code, the audio fades out anytime I hit the home button, putting
the app in the background. Any help is much appreciated, I hope that it
is a quick fix type of answer for anybody who has done this before.

First add the UIBackgroundModes key to your Info.plist file if you haven't done already.
More info here.
If you have done that already, which framework do you use to play your media?

Thanks for the help Irene. You are pretty much right with your answer except I just wanted to provide the steps that were necessary for it to work for me. I read the apple documentation that you posted and for some reason it left these important details out:
When you add the UIBackgroundModes key in the .plist file, you have to make it an array.
The value for Item 0 of the array should be audio.
Of course your app should also take care of setting its audio session category in combination with setting this key.

Related

iOS 9 is 'Picture In Picture' mode available with 'Requires Fullscreen' set to YES?

I have an app that requires fullscreen, but want to make Picture In Picture feature available when user opens video playback modal controller (My own player written with AVFoundation).
I have 'app requires fullscreen' flag set to YES in settings.
Can I use Picture in Picture mode for video playback while all my app requires to be fullscreen?
I've created a sample project and found that YES, Picture In Picture feature doesn't depend whether your app needs fullscreen or not.
Maybe this would be helpful for those who are looking for the same question as I did.
Set base SDK version to latest (9.0)
Set "App requires fullscreen" flag in the project settings
Set AVAudioSession category to AVAudioSessionCategoryPlayback in application:didFinishLaunchingWithOptions:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
Just created AVPlayerViewController and presented it from my root controller
AVPlayerViewController *moviePlayerController = [[AVPlayerViewController alloc] init];
AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:#"http://127.0.0.1:12345/movie.mp4"]];
moviePlayerController.player = player;
[self presentViewController:moviePlayerController animated:YES completion:nil];
PiP button appeared at the bottom right corner of playback controls and it works!
P.S. I may remove the question and answer if it's obvious or too simple and nobody find it useful.

iCloud enabled - Stop the open file displaying on application launch?

I've just added iCloud support to an app that I am working on. Its working great, except that when I open the application without a document in focus the iCloud open file dialog appears and I don't want it to!
In my app delegate I have:
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[mainWindowController.window makeKeyAndOrderFront:self];
return NO;
}
Which I use to show my own custom window. However now, both the iCloud open file dialog and my own dialog are displayed. Any ideas on how I can get rid of the iCloud dialog?
https://developer.apple.com/library/prerelease/content/releasenotes/AppKit/RN-AppKitOlderNotes/index.html
NSDocument Support for iCloud
In 10.8, NSDocument-based applications with a ubiquity-container-identifiers entitlement gain new functionality and UI to facilitate iCloud document management.
When iCloud is enabled and an application is first launched or re-activated and no windows are visible or being restored, instead of creating a new Untitled document, NSDocumentController will display a non-modal open panel showing the user's iCloud library.
...
Applications that do not wish to use these features for any or all of their NSDocument subclasses can override +[NSDocument usesUbiquitousStorage] and return NO. If all of the application's declared NSDocument subclasses return NO from this method, then NSDocumentController will never show the new non-modal open panel.
So if you can give up using the features listed in this release note, return NO at +[NSDocument usesUbiquitousStorage].
I confirmed you can still open/save your file into iCloud storage from the normal dialog.
Putting below codes in your App Delegate lets you bypass that iCloud pop up New Document screen. Tested for High Sierra.
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
// Schedule "Checking whether document exists." into next UI Loop.
// Because document is not restored yet.
// So we don't know what do we have to create new one.
// Opened document can be identified here. (double click document file)
NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:#selector(openNewDocumentIfNeeded) object:nil];
[[NSOperationQueue mainQueue] addOperation: op];
}
-(void)openNewDocumentIfNeeded
{
NSUInteger documentCount = [[[NSDocumentController sharedDocumentController] documents]count];
// Open an untitled document what if there is no document. (restored, opened).
if(documentCount == 0){
[[NSDocumentController sharedDocumentController]openUntitledDocumentAndDisplay:YES error: nil];
}
}
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[mainWindowController.window makeKeyAndOrderFront:self];
return NO;
}
This part is correct. I've just tested it.
Just make sure your that this class is really your app delegate.
Make a new class called prefixAppDelegate
In your MainMenu.xib, drag a new object to the side and set it's custom class to the app delegate class
Right click Application and drag from Delegate down to your app delegate object.
Now just paste the code above into your app delegate class
If that still doesn't help, try logging something in applicationShouldOpenUntitledFile:.
Also, I recommend not to set [mainWindowController.window makeKeyAndOrderFront:self]; in this method. You should rather use the app delegate method applicationDidFinishLaunching: method.
My observation and fix:
[applicationShouldOpenUntitledFile:] won't be executed except you remove Key NSDocumentClass from *-info.plist. But this is harmful if your app is document based application, it won't open the document type you linked.
My fix is open my customised window directly in -(void)applicationWillFinishLaunching:(NSNotification *)notification method (Application delegate)
ETDocumentWindowController *windowController = (ETDocumentWindowController*)get your own window controller here...;
[windowController.window makeKeyAndOrderFront:nil];
I thought I would share my solution to this issue as I see others still looking for an answer. Its not a great solution but it does the trick.
Subclass NSDocumentController and add the following:
+ (void) setCanOpenUntitledDocument: (BOOL) _canOpenUntitledDocument
{
canOpenUntitledDocument = _canOpenUntitledDocument;
} // End of setCanOpenUntitledDocument:
- (void) openDocument: (id) sender
{
// With iCloud enabled, the app keeps trying to run openDocument: on first launch (before apphasfinishedlaunching gets set.
// This method lets us check and see if the app has finished launching or not. If we try to open a document before
// its finished, then don't let it.
if(!canOpenUntitledDocument)
{
return;
} // End of appHasFinishedLaunching not set
[super openDocument: sender];
} // End of openDocument:
Add the following to your app delegate:
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
// Finished launching. Let us open untitled documents.
[SQLProDocumentController setCanOpenUntitledDocument: true];
...
}
And the reasoning -- By setting a breakpoint in openDocument I've found that its called before applicationDidFinishLaunching, applicationShouldOpenUntitledFile or applicationShouldHandleReopen:hasVisibleWindows: get called, meaning adding those methods is useless. Again, it's not great code but it works and does the trick. (None of the other solutions have worked for me).
I ran into a similar problem -- it turned out that in my case, I had to remove the NSDocumentClass key and value from my Info.plist in the CFBundleDocumentTypes array. Only then would the applicationShouldOpenUntitledFile: method get called and thus allow me to prevent the iCloud/Document window from opening.

iOS 6 local notifications fail when the phone is locked with app open

I have an app with basic alarm functionality. In my applicationWillResignActive: method I have it setup to create notifications to set off the alarm. This works pretty great, and I believe this is the proper way to do it (let me know if you think there is a better way).
Only in the specific situation, ONLY ON iOS 6, when the application is not "quit" (the home button is never pressed) but the user merely locks the phone or the phone auto locks, the notifications don't go off.
I have traced through the code, and the notifications are indeed being created and it worked perfectly in iOS 5.
Here is my code:
- (void)applicationWillResignActive:(UIApplication *)application
{
[UIApplication sharedApplication].idleTimerDisabled = NO;
[alarm setupForBackground];
if ([alarm isRunning]) {
[alarm stop];
}
}
Here is the notification creation method:
- (void)setupForBackground
{
UILocalNotification* alarmNotification = [[UILocalNotification alloc] init];
if (alarmNotification) {
alarmNotification.fireDate = alarmDate;
alarmNotification.timeZone = [NSTimeZone defaultTimeZone];
alarmNotification.repeatInterval = 0;
alarmNotification.soundName = #"NotificationSound.aif";
[[UIApplication sharedApplication] scheduleLocalNotification:alarmNotification];
}
}
I have been searching for an answer for a while, and I could not find anything stating something about notification changes. Thanks for any help.
I have a semi-solution. Apparently if you add an AlertBody to the notification, then it works.
My belief is that this is a bug in iOS 6. As I mentioned it worked in iOS 5, the documentation makes no mention of having such a requirement, and the notification does work without the AlertBody if the application is quit (the home button is pressed).
Still curious to see if my understanding is correct and if I should file a bug report with Apple.
Thoughts anybody?

IOS FacebooSDK 3.0 FBLoginVIew in modal viewController

I have modal view controller displayed on rightBarButtonItem click. I'm using FbLoginView in this controller as in sample ios-Facebook SDK 3.0 Error 5 When Posting Status Update.
But i'm unable to show modal view controller more than one time.
I tried to release FBLoginView on ViewDidUnload but it always crashes on second atempt to open modal view controller.
Got the same problem and deal with it for couple days already. And finally this is my solution:
if (!FBSession.activeSession.isOpen) {
theLoginView = [[FBLoginView alloc] init];
theLoginView.frame = CGRectOffset(theLoginView.frame,
([[UIScreen mainScreen] bounds].size.width-theLoginView.frame.size.width)/2,
([[UIScreen mainScreen] bounds].size.height-theLoginView.frame.size.height)/2 -50);
theLoginView.delegate = self;
[self.view addSubview:theLoginView];
[theLoginView sizeToFit];
}
//Only close the session when application is terminating, this will save the token information:
- (void)applicationWillTerminate:(UIApplication *)application {
[FBSession.activeSession close];
}
//And keep the FBSession within the app until the user want to logout:
[FBSession.activeSession closeAndClearTokenInformation];
Right now for me its working completely fine. Hope this help.
The FB SDK doesn't seem to like you creating more than one FBLoginView. Maybe you can if you properly terminate the session, but I found it easier just to create the LoginView once and keep it around.
I did this as follows:
1) in my .m modal view controller file, I created a static variable
static FBLoginView* loginView;
2) When loading the modal view controller in my viewDidLoad, instead of
FBLoginView *loginview = [[FBLoginView alloc] initWithPermissions:
[NSArray arrayWithObject:#"status_update"]];
loginview.frame = CGRectOffset(loginview.frame, 10, 10);
I added a check to find if its already initialized, like this:
if (!loginView) {
loginView = [[FBLoginView alloc] initWithPermissions:
[NSArray arrayWithObject:#"status_update"]];
loginView.frame = CGRectOffset(loginView.frame, 10, 10);
}
Beyond that, I just followed the example of FB's HelloFacebook project.
Not pretty code, but it seems to work.
I had the same problem. Try to add something like this:
if(!yourFBLoginView)
{
yourFBLoginView = [FBLoginView alloc] init...];
}
And/or do not forget to close your active session when you dismissing your modalViewController.
if ([[FBSession activeSession] isOpen])
{
[[FBSession activeSession] close];
}
I think the answer for me (a variant of what was said) was only that I needed to have:
[FBSession.activeSession closeAndClearTokenInformation];
in the:
(void)applicationWillTerminate:(UIApplication *)application
function. The problem specifically for me was, while I was testing...I was constantly terminating the app without actually logging out the user without ever destroying the FBSession, so that when I went back into the app to test what I had changed - my Facebook user was still logged in, and thus some of the conditionals were being incorrectly met. I think this is very important for anyone who is testing (and I'm actually thinking that you should have that line in there anyway) to make sure to clear the session every time the application terminates to avoid this problem...I can imagine a scenario where my app just crashed on somebody and now they are reopening it and they experience the crash because the session was never cleared.

Get ADInterstitialAd to load more often on iPad: possible?

I'm testing my iAd on my iPad, and I can't seem to get my ADInterstitialAd to load very often.
It does run occasionally but most of the time the first method below is called
- (void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError*)error{
NSLog(#"Ad Failed");
//[self cycleInterstitial];
}
However when I try to reload it upon failing.(see below method) It just fails over and over. I dont see why apple would let it just fail in a testing environment. I have read that the fill rate can be low, but it seems really low just for a test iPad app.
- (void)cycleInterstitial{
// Clean up the old interstitial...
interstitial.delegate = nil;
[interstitial release];
// and create a new interstitial. We set the delegate so that we can be notified of when
interstitial = [[ADInterstitialAd alloc] init];
interstitial.delegate = self;
}
Can anyone please advise? Thanks!
No, sorry. Apple is solely responsible for the fill rate of iAds, including interstitial iAds for iPad. As developers and users, we do not have any direct control over this.