2012-05-12 91 views
1

我想創建一個區域監控插件。區域監控開始正常,但功能didfinishlaunching和didrecievelocalnotification沒有被喚起。我不知道爲什麼會發生這種情況。UIApplicationDelegate方法不被稱爲

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

#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

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; 

    } 
} 

調用區域監控

OnPres的活動中我做

bool startedRM = RegionMonitoringMediator.startRegionMonitoring(77.0f,28.0f,10.0f); 

回答

0

每個應用只允許有一個UIApplicationDelegate。當Unity3D爲iPhone播放器構建應用程序時,會生成一個類AppController,它充當接口。

本課是插入您的代碼的地方,以致電RegionMonitoringPlugin

+0

謝謝凱。我注意到AppController正在將通知傳遞給Unity。所以我用統一的NotificationServices類來處理通知。然而,我還有另一個問題,我意識到即使位置管理器事件沒有被稱爲膨脹任何線索?謝謝 – ila

+0

對不起,我到目前爲止還沒有使用它。 – Kay

0

init應包括超級呼叫在其啓動:我們使用分配(=)運營商,而不是比較(==)運算符

- (id) init 
{ 
    if (self = [super init]) 
    { 
     // initialize everything else 
    } 
    return self; 
} 

音符。

+1

恩,我認爲你的意思是'自我'作爲賦值的左值和'return'。 –

+0

@KenThomases - 很長一段時間最令人尷尬的錯字......修正,謝謝。 – MByD

+0

@ila - 我有一個錯字,請參閱編輯。 – MByD