ios - handleWatchKitExtensionRequest+ asynchronous call -
i trying use mkmapsnapshotter take snapshot map on containing iphone app, , send image watch. when containing iphone app not running on background, cannot work.
this how trying do:
on appdelegate:
- (void)application:(uiapplication *)application handlewatchkitextensionrequest:(nsdictionary *)userinfo reply:(void ( ^)( nsdictionary * ))reply { __block uibackgroundtaskidentifier watchkithandler; watchkithandler = [[uiapplication sharedapplication] beginbackgroundtaskwithname:@"backgroundtask" expirationhandler:^{ watchkithandler = uibackgroundtaskinvalid; }]; nsmutabledictionary *response = [nsmutabledictionary dictionary]; dispatch_semaphore_t sema = dispatch_semaphore_create(0); //========================building snapshot=========== cllocationdegrees latitude = 37.331793f; cllocationdegrees longitude = -122.029584f; mkmapview* mapview = [[mkmapview alloc] initwithframe:cgrectmake(0, 0, (156*2), (108*2))]; mapview.maptype = mkmaptypehybrid; cllocationcoordinate2d coord = cllocationcoordinate2dmake(latitude, longitude); mkcoordinatespan span = {.latitudedelta = 0.01, .longitudedelta = 0.01}; mkcoordinateregion region = {coord, span}; [mapview setregion:region]; mkmapsnapshotoptions *options = [[mkmapsnapshotoptions alloc] init]; options.region = mapview.region; options.size = mapview.frame.size; options.scale = [[uiscreen mainscreen] scale]; mkmapsnapshotter *snapshotter = [[mkmapsnapshotter alloc] initwithoptions:options]; //======================================================== [snapshotter startwithcompletionhandler:^(mkmapsnapshot *snapshot, nserror *error) { nsdata *imagedata = uiimagepngrepresentation(snapshot.image); [response setobject:imagedata forkey:@"snapshotimage"]; reply(response); }]; dispatch_semaphore_wait(sema, dispatch_time_forever); dispatch_after(dispatch_time( dispatch_time_now, (int64_t)nsec_per_sec * 1), dispatch_get_global_queue( dispatch_queue_priority_default, 0), ^{ [[uiapplication sharedapplication] endbackgroundtask:watchkithandler]; }); } i trying different codes suggested people should doing wrong in here. please notice "snapshotter startwithcompletionhandler" done asynchronously.
i have tried following suggestions should missing here. https://stackoverflow.com/a/30000323/4982919
any comment , appreciated.
Comments
Post a Comment