首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用的UIApplicationDelegate方法

未调用的UIApplicationDelegate方法
EN

Stack Overflow用户
提问于 2012-05-12 14:43:05
回答 2查看 1.1K关注 0票数 1

我正在尝试创建一个用于区域监控的插件。区域监控启动正常,但函数未完成启动和didrecievelocalnotification通知未被调用。我不确定为什么会发生这种情况。

regionMonitoring.h

代码语言:javascript
复制
 #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

代码语言:javascript
复制
#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;

        }
}

插件统一代码: RegionMonitorMediater.h

代码语言:javascript
复制
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;

    }
}

主叫区域监听

我做的OnPress事件

代码语言:javascript
复制
bool startedRM = RegionMonitoringMediator.startRegionMonitoring(77.0f,28.0f,10.0f);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-12 18:20:15

每个应用程序只允许一个UIApplicationDelegate。当Unity3D为iPhone player构建应用程序时,会生成一个AppController类,它充当接口。

这个类是插入代码来调用RegionMonitoringPlugin的地方。

票数 0
EN

Stack Overflow用户

发布于 2012-05-12 14:46:34

init应该在开始时包含一个对super的调用:

代码语言:javascript
复制
- (id) init
{
    if (self = [super init])
    {
        // initialize everything else
    }
    return self;
}

注意,我们使用的是赋值(=)运算符,而不是比较(==)运算符。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10561655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档