我有以下代码,可以在应用终止时完美运行。
-(void)beaconManager:(id)manager didEnterRegion:(CLBeaconRegion *)region{
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = @"TEST NOTIFICATION";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
但是当我执行以下操作时:
-(void) createTestNotification {
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = @"TEST";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
-(void)beaconManager:(id)manager didEnterRegion:(CLBeaconRegion *)region{
[self createTestNotification];
}
它不起作用?我只将我的代码移到外面没有别的?我知道当didEnterRegion被调用时应用程序只有5秒,但调用此方法不会花费这个时间吗?
更新1:
我刚发现区域 didEnterRegion
返回的是我开始监控而不是属于信标的那个 >该应用程序刚看到它。我将使用此区域来获取我的自定义消息取决于此区域的主要和次要。
但是在我的情况下这是不可能的,所以当didEnterRegion
被调用时我开始范围,然后在didRangeBeacons
中返回信标的数组>我从 localDataBase "取决于Major& minor"
这是我的代码: 此代码在后台的app中工作并获得通知但是当app close永远不会收到通知? < / p>
-(void)beaconManager:(id)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
[self handelBeaconsNotification:beacons];
}
-(NSString *) getBeaconFromLocalData:(NSNumber *)regionMajor andRegionMin:(NSNumber *)regionMinor{
PFQuery *query = [PFQuery queryWithClassName:@"Beacons"];
NSArray *allRegions_id = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:1], nil];
[query whereKey:@"region_id" containedIn:allRegions_id];
NSLog(@"%@",regionMajor);
NSLog(@"%@",regionMinor);
[query whereKey:@"major" equalTo:regionMajor];
[query whereKey:@"minor" equalTo:regionMinor];
[query fromLocalDatastore];
NSArray *data = [query findObjects];
return [[data objectAtIndex:0] valueForKey:@"enter_message"];
}
-(void) handelBeaconsNotification:(NSArray *)beaconsArray{
for (int i = 0 ; i<beaconsArray.count; i++) {
NSString *currentBeaconKey = [NSString stringWithFormat:@"%@-%@",[beaconsArray[i] major],[beaconsArray[i] minor]];
if ([beaconsDictionary valueForKey:currentBeaconKey] == nil) {
NSMutableDictionary *dateDic = [[NSMutableDictionary alloc] init];
[dateDic setValue:[NSDate date] forKey:@"StartVisit"];
[dateDic setValue:nil forKey:@"EndVisit"];
[beaconsDictionary setValue:dateDic forKey:currentBeaconKey];
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = [self getBeaconFromLocalData:[beaconsArray[i] major] andRegionMin:[beaconsArray[i] minor]];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
}
更新2:
我只是将日志添加到didEnterRegion
和didRangeBeacons
,以确定只显示来自didEnterRegion
的 NSLog ,然后该应用看起来像 崩溃 ,didRangeBeacons
的日志未显示
Oct 11 00:27:25 Moe-Amleh-s-iPhone kernel[0] <Notice>: xpcproxy[220] Container: /private/var/mobile/Containers/Data/Application/76CE6FC9-46AA-4C5B-9F8C-F31C1F38CAD2 (sandbox)
Oct 11 00:27:26 Moe-Amleh-s-iPhone Swipt[220] <Warning>: 111111111111111111111111
Oct 11 00:27:26 Moe-Amleh-s-iPhone locationd[70] <Notice>: Location icon should now be in state 'Active'
Oct 11 00:27:27 Moe-Amleh-s-iPhone SpringBoard[48] <Warning>: HW kbd: Failed to set (null) as keyboard focus
Oct 11 00:27:27 Moe-Amleh-s-iPhone com.apple.xpc.launchd[1] (UIKitApplication:com.swiptrewards.Swipt[0x8b1f][220]) <Notice>: Service exited due to signal: Abort trap: 6
Oct 11 00:27:27 Moe-Amleh-s-iPhone SpringBoard[48] <Warning>: UNNotificationRegistrarConnectionListener connection invalidated
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Notice>: platform_thread_get_unique_id matched 6383
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Notice>: Formulating report for corpse[220] Swipt
Oct 11 00:27:27 Moe-Amleh-s-iPhone assertiond[65] <Error>: assertion failed: 13A452: assertiond + 60449 [BDEA3ABF-EF9E-34B9-A166-8AE775484A67]: 0x3
Oct 11 00:27:27 Moe-Amleh-s-iPhone Unknown[65] <Error>:
Oct 11 00:27:27 Moe-Amleh-s-iPhone SpringBoard[48] <Warning>: Application 'UIKitApplication:com.swiptrewards.Swipt[0x8b1f]' crashed.
Oct 11 00:27:27 Moe-Amleh-s-iPhone UserEventAgent[23] <Warning>: 13250595064: id=com.swiptrewards.Swipt pid=220, state=0
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Warning>: report not saved because the limit of 25 for 109_Swipt logs has been reached.
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Notice: This report is abbreviated for syslog inclusion because it could not be saved to disk.
Symbolication may be possible by manually cleaning up and including the Binary Image section of a full report from this same device -- good luck!
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Process: Swipt [220]
Path: /private/var/mobile/Containers/Bundle/Application/0E547BB5-F423-48DC-B013-B549C0AE6A0E/Swipt.app/Swipt
OS Version: iOS 9.0.2 (13A452)
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 0
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Global Trace Buffer (reverse chronological seconds):
0.484432 CFNetwork 0x0000000025682aa1 TCP Conn 0x15ec9800 SSL Handshake DONE
0.586995 CFNetwork 0x0000000025682aa1 TCP Conn 0x15eb5080 SSL Handshake DONE
0.666939 CFNetwork 0x0000000025682aa1 TCP Conn 0x15eb6980 SSL Handshake DONE
0.742367 CFNetwork 0x00000000256829db TCP Conn 0x15ec9800 starting SSL negotiation
0.742681 CFNetwork 0x00000000257024cd TCP Conn 0x15ec9800 complete. fd: 13, err: 0
0.742994 CFNetwork 0x00000000257035cf TCP Conn 0x15ec9800 event 1. err: 0
0.939516 CFNetwork 0x000000002570364d TCP Conn 0x15ec9800 started
0.983614 CFNetwork 0x00000000256829db TCP Conn 0x15eb5080 starting SSL negotiation
0.983989 CFNetwork 0x00000000257024cd TCP Conn 0x15eb5080 complete. fd: 6, err: 0
0.984285 CFNetwork 0x00000000257035cf TCP Conn 0x15eb5080 event 1. err: 0
1.050325 CFNetwork 0x00000000256829db TCP Conn 0x15eb6980 starting SSL negotiation
1.052768 CFNetwork 0x00000000257024cd TCP Conn 0x15eb6980 complete. fd: 5, err: 0
1.053148 CFNetwork 0x00000000257035cf TCP Conn 0x15eb6980 event 1. err: 0
1.282007 CFNetwork 0x000000002570364d TCP Conn 0x15eb6980 started
1.282007 CFNetwork 0x000000002570364d TCP Conn 0x15eb5080 started
1.294984 CFNetwork 0x00000000257442df Creating default cookie storage with default identifier
1.294984 CFNetwork 0x00000000257442bb Faulting in CFHTTPCookieStorage singleton
1.294984 CFNetwork 0x000000002578673b Faulting in NSHTTPCookieStorage singleton
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x3800cd24 0x37ff8000 + 85284
1 libsystem_pthread.dylib 0x380acb5a 0x380a9000 + 15194
2 libsystem_c.dylib 0x37fa4f50 0x37f5a000 + 307024
3 Swipt 0x003860a6 0xf0000 + 2711718
4 CoreFoundation 0x25e5fba8 0x25d6a000 + 1006504
5 libobjc.A.dylib 0x37813086 0x3780c000 + 28806
6 libc++abi.dylib 0x36ff7e16 0x36fe1000 + 93718
7 libc++abi.dylib 0x36ff78f6 0x36fe1000 + 92406
8 libobjc.A.dylib 0x37812f46 0x3780c000 + 28486
9 CoreFoundation 0x25d7326e 0x25d6a000 + 37486
10 CoreFoundation 0x25d72fdc 0x25d6a000 + 36828
11 GraphicsServices 0x2f017af8 0x2f00e000 + 39672
12 UIKit 0x29fd817c 0x29f5f000 + 495996
13 Swipt 0x00116ab0 0xf0000 + 158384
14 libdyld.dylib 0x37f3d872 0x37f3b000 + 10354
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 1:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 2 name: Dispatch queue: com.apple.libdispatch-manager
Thread 2:
0 libsystem_kernel.dylib 0x3800e3c0 0x37ff8000 + 91072
1 libdispatch.dylib 0x37f249a6 0x37f14000 + 68006
2 libdispatch.dylib 0x37f16b2e 0x37f14000 + 11054
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 3:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 4:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 5:
0 libsystem_kernel.dylib 0x37ff9130 0x37ff8000 + 4400
1 libsystem_kernel.dylib 0x37ff8f30 0x37ff8000 + 3888
2 CoreFoundation 0x25e224ec 0x25d6a000 + 754924
3 CoreFoundation 0x25e20872 0x25d6a000 + 747634
4 CoreFoundation 0x25d731e8 0x25d6a000 + 37352
5 CoreFoundation 0x25d72fdc 0x25d6a000 + 36828
6 Foundation 0x26b63d7c 0x26b58000 + 48508
7 Swipt 0x0032ef3e 0xf0000 + 2355006
8 Foundation 0x26c347fc 0x26b58000 + 903164
9 libsystem_pthread.dylib 0x380abc92 0x380a9000 + 11410
10 libsystem_pthread.dylib 0x380abc06 0x380a9000 + 11270
11 libsystem_pthread.dylib 0x380a9a24 0x380a9000 + 2596
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 6 name: Dispatch queue: NSOperationQueue 0x15ea11c0 :: NSOperation 0x15e933c0 (QOS: USER_INITIATED)
Thread 6:
0 libsystem_kernel.dylib 0x37ff9130 0x37ff8000 + 4400
1 libsystem_kernel.dylib 0x37ff8f30 0x37ff8000 + 3888
2 CoreFoundation 0x25e224ec 0x25d6a000 + 754924
3 CoreFoundation 0x25e20872 0x25d6a000 + 747634
4 CoreFoundation 0x25d731e8 0x25d6a000 + 37352
5 CoreFoundation 0x25db3faa 0x25d6a000 + 303018
6 Foundation 0x26c1ecc4 0x26b58000 + 814276
7 libdispatch.dylib 0x37f1f04e 0x37f14000 + 45134
8 libdispatch.dylib 0x37f1802e 0x37f14000 + 16430
9 libdispatch.dylib 0x37f203dc 0x37f14000 + 50140
10 libdispatch.dylib 0x37f2024a 0x37f14000 + 49738
11 libsystem_pthread.dylib 0x380a9e28 0x380a9000 + 3624
12 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 7:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 8 name: com.apple.NSURLConnectionLoader
Thread 8:
0 libsystem_kernel.dylib 0x37ff9130 0x37ff8000 + 4400
1 libsystem_kernel.dylib 0x37ff8f30 0x37ff8000 + 3888
2 CoreFoundation 0x25e224ec 0x25d6a000 + 754924
3 CoreFoundation 0x25e20872 0x25d6a000 + 747634
4 CoreFoundation 0x25d731e8 0x25d6a000 + 37352
5 CoreFoundation 0x25d72fdc 0x25d6a000 + 36828
6 CFNetwork 0x256e40ae 0x2565c000 + 557230
7 Foundation 0x26c347fc 0x26b58000 + 903164
8 libsystem_pthread.dylib 0x380abc92 0x380a9000 + 11410
9 libsystem_pthread.dylib 0x380abc06 0x380a9000 + 11270
10 libsystem_pthread.dylib 0x380a9a24 0x380a9000 + 2596
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 9:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 10:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 11:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 12:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 13:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 14:
0 libsystem_kernel.dylib 0x3800d92c 0x37ff8000 + 88364
1 libsystem_pthread.dylib 0x380a9e34 0x380a9000 + 3636
2 libsystem_pthread.dylib 0x380a9a18 0x380a9000 + 2584
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 15 name: com.apple.CFSocket.private
Thread 15:
0 libsystem_kernel.dylib 0x3800cfb4 0x37ff8000 + 85940
1 CoreFoundation 0x25e27990 0x25d6a000 + 776592
2 libsystem_pthread.dylib 0x380abc92 0x380a9000 + 11410
3 libsystem_pthread.dylib 0x380abc06 0x380a9000 + 11270
4 libsystem_pthread.dylib 0x380a9a24 0x380a9000 + 2596
Oct 11 00:27:27 Moe-Amleh-s-iPhone ReportCrash[221] <Error>: Thread 0 crashed with ARM Thread State (32-bit):
r0: 0x00000004 r1: 0x00000000 r2: 0x15eb25d0 r3: 0x00000000
r4: 0x00000000 r5: 0x00000000 r6: 0x15e81c60 r7: 0x4072cfb4
r8: 0x15eb25d0 r9: 0x3ae01900 r10: 0x00000000 r11: 0x00000020
ip: 0x0000005d sp: 0x40724bec lr: 0x25e27991 pc: 0x3800cfb4
答案 0 :(得分:4)
mmm我认为您正在使用空区域变量,因此请确保正确发送它并且不要为startRanging方法使用空变量。
当您的应用关闭时,所有变量都将被终止,因此请确保您将其传递给非空。