[英]How to handle socket connection's events when app is in background?
即使應用程序處於后台,我也想使用以下功能?
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
case NSStreamEventHasBytesAvailable:
{ NSLog(@"Event:NSStreamEventHasBytesAvailable");
if (theStream == _inputStream) {
NSLog(@"NSStreamEventHasBytesAvailable: on Input Stream");
uint8_t buffer[1024];
int len;
while ([_inputStream hasBytesAvailable]) {
len = [_inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
NSLog(@"server said: %@", output);
// to get local notification I am calling below method.
[self scheduleNotification];
}
}
}
}
break;
}
上面的代碼在前台工作。 我已經完成了蘋果文檔中給出的所有更改,以便在后台模式下運行應用程序 - voip。 我應該在 AppDelegate 方法中寫什么?
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
如何獲取后台調用的stream:handleEvent?
我剛才正在處理類似的問題。 需要牢記的幾件重要事情:
因此,如果這不是 voip 應用程序,您實際上可能希望使用遠程通知直接提醒用戶而不是顯示本地通知。 我想這是您的應用程序通過 App Store 驗證的唯一方法。
無論如何,SO 上的兩個鏈接對您可能會有所幫助:
我最終使用了 voip(就像你所做的那樣)並按照此處的建議播放無聲音頻循環 - 它有效。 不確定是否仍然需要此無聲音頻循環。
使用此代碼讓您的應用程序在 ios 中保持活動狀態
var backgroundUpdateTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier(rawValue: 0)
func endBackgroundUpdateTask() {
UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
}
func applicationWillResignActive(_ application: UIApplication) {
self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
self.endBackgroundUpdateTask()
})
}
func applicationDidBecomeActive(_ application: UIApplication) {
self.endBackgroundUpdateTask()
}
我希望你能通過這種幫助獲得成功
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.