[英]UIApplicationDelegate methods not being called
I am trying to create a plugin for region monitoring. 我正在尝试创建用于区域监视的插件。 Region Monitoring is starting fine but the function didfinishlaunching and didrecievelocalnotification are not being evoked.
区域监视已开始正常,但未启用功能didfinishlaunching和didrecevelocalnotification。 I am not sure why this is happening.
我不确定为什么会这样。
regionMonitoring.h regionMonitoring.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface RegionMonitoringPlugin : NSObject <UIApplicationDelegate,CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
-(void)enterRegionNotify;
-(void)leaveRegionNotify;
-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)raduis;
@end
regionMonitoring.mm regionMonitoring.mm
#import "RegionMonitoringPlugin.h"
@implementation RegionMonitoringPlugin
- (id) init
{
//if ( init == [super init]){
if (locationManager==nil){
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
return self;
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self enterRegionNotify];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self leaveRegionNotify];
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)regionwithError:(NSError *)error
{
NSLog(@"Location error %@, %@", error, @"Fill in the reason here");
}
-(void)leaveRegionNotify
{
NSLog(@"Starting region monitoring - check point 3");
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody= @"Region Left"; // ToAsk: What should be displayed
note.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:note];
[note release];
}
-(void)enterRegionNotify
{
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody= @"Region Left"; //ToAsk: what should be displayed ?
note.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:note];
[note release];
}
-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)radius
{
NSLog(@"Starting region monitoring - check point 2");
[self leaveRegionNotify];
CLLocationCoordinate2D home;
home.latitude = latitude;
home.longitude = longitude;
CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:radius identifier:@"region"];
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
[region release];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"Starting region monitoring - checkpoint 4");
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Region Monitor Notification" message:notification.alertBody delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
- (BOOL)application:(UIApplication *) application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"Test");
return TRUE;
}
@end
extern "C" {
static RegionMonitoringPlugin *regionMonitor;
// Unity callable function to start region monitoring
BOOL _startRegionMonitoring(float m_latitude,float m_longitude, float m_radius)
{
NSLog(@"Starting region monitoring");
if (![CLLocationManager regionMonitoringAvailable] || ![CLLocationManager regionMonitoringEnabled] )
return NO;
if (regionMonitor == nil){
regionMonitor = [[RegionMonitoringPlugin alloc] init] ;
}
[regionMonitor startMonitor:m_latitude longitude:m_longitude radius:m_radius];
return YES;
}
}
Unity Code for plugin : RegionMonitorMediater.h 插件的Unity代码:RegionMonitorMediater.h
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class RegionMonitoringMediator {
/*Interface to native implementation */
[DllImport ("__Internal")]
private static extern bool _startRegionMonitoring (float m_latitude,float m_longitude, float m_radius);
public static bool startRegionMonitoring (float latitude,float longitude, float radius)
{
/*Call plugin only when running on real device*/
if (Application.platform != RuntimePlatform.OSXEditor)
return _startRegionMonitoring ( latitude , longitude , radius);
else return false;
}
}
Calling region monitor 呼叫区域监控器
OnPress event I do 我做的OnPress活动
bool startedRM = RegionMonitoringMediator.startRegionMonitoring(77.0f,28.0f,10.0f);
init
should include a call to super at its start: init
在开始时应包含对super的调用:
- (id) init
{
if (self = [super init])
{
// initialize everything else
}
return self;
}
note that we used assignment ( =
) operator, and not comparison ( ==
) operator. 请注意,我们使用赋值(
=
)运算符,而不是比较( ==
)运算符。
There is only one UIApplicationDelegate
allowed per app. 每个应用程序仅允许一个
UIApplicationDelegate
。 When Unity3D builds your application for iPhone player a class AppController
is generated which acts as interface. 当Unity3D为iPhone播放器构建您的应用程序时,将生成一个类
AppController
用作接口。
This class is the place for inserting your code to call RegionMonitoringPlugin
. 此类是用于插入代码以调用
RegionMonitoringPlugin
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.