直播 IM

IOS获取静默推送通知后,获取用户当前位置信息,返回给服务器

2016-06-02  本文已影响241人  Loki_

使用CLLocationManager定位,由于是在- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResultresult))handler收到推送信息后开始定位,直接使用CLLocationManagerDelegate,会出现有时进入代理,有时不进入代理的问题

解决方案:使用单例模式

(1)CurrentLocationManager.h

#import

#import"AppDelegate.h"

@interfaceCurrentLocationManager :CLLocationManager

+ (instancetype)sharedManager;

@property(nonatomic,assign)NSDictionary*myPush;

@property(nonatomic,assign)doubledeviceLevel;

@end

(2)CurrentLocationManager.m

#import"CurrentLocationManager.h"

@interfaceCurrentLocationManager()

@end

@implementationCurrentLocationManager

+ (instancetype)sharedManager

{

staticCurrentLocationManager*instance =nil;

staticdispatch_once_tonceToken;

dispatch_once(&onceToken,  ^{

instance  = [[CurrentLocationManageralloc]init];

});

returninstance;

}

- (instancetype)init

{

self= [superinit];

if(self)

{

self.delegate=self;

self.desiredAccuracy=kCLLocationAccuracyHundredMeters;

}

returnself;

}

- (void)locationManager:(CLLocationManager*)manager

didUpdateLocations:(NSArray*)locations

{

intlength

=[locationscount] -1;

CLLocation*newLocation = [locationsobjectAtIndex:length];

NSTimeIntervallocationAge = -[newLocation.timestamptimeIntervalSinceNow];

if(locationAge >5.0|| newLocation.horizontalAccuracy<0){

return;

}else{

floatlat =

newLocation.coordinate.latitude;

floatlng =

newLocation.coordinate.longitude;

NSLog(@"--------------> %f , %f ",newLocation.coordinate.latitude,newLocation.coordinate.longitude);

//具体和服务器的操作

[selfstopUpdatingLocation];

}

}

@end

(3)AppDelegate.m

if([CLLocationManagerlocationServicesEnabled]) {//判断是否打开了位置服务

#if

__IPHONE_OS_VERSION_MAX_ALLOWED >=80000

[[CurrentLocationManagersharedManager]requestAlwaysAuthorization];

#endif

#if

__IPHONE_OS_VERSION_MAX_ALLOWED >=90000

[[CurrentLocationManagersharedManager]setAllowsBackgroundLocationUpdates:YES];

#endif

[[CurrentLocationManagersharedManager]startMonitoringSignificantLocationChanges];

[[CurrentLocationManagersharedManager]setMyPush:myPush];

[[CurrentLocationManagersharedManager]setDeviceLevel:deviceLevel];

[[CurrentLocationManagersharedManager]startUpdatingLocation];

handler(UIBackgroundFetchResultNewData);

}

上一篇下一篇

猜你喜欢

热点阅读