仅在特定位置启用电子邮件

时间:2014-02-11 00:24:06

标签: ios objective-c gps

我创建了一个应用程序,如果用户位于特定区域内,我只想工作(即我只希望用户能够在该国家的特定州内发送电子邮件)

可以请一些 -

a)指出我如何设置GPS边界的教程,以及如何检查用户是否在所述GPS边界内

b)写一个创建GPS区域的例子,以及如何检查用户是否在该区域内。

我将用户的纬度和经度存储到变量中。

这是我的位置管理员代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longditute = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        Latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
    // Stop Location Manager
    [locationManager stopUpdatingLocation];
}

和我的电子邮件代码:

-(IBAction)actionEmailComposer{

    if ([MFMailComposeViewController canSendMail]) {
        if ([witnessResponse  isEqual: @"Yes"]) {
            MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc]init];
            mailViewController.mailComposeDelegate = self;
            [mailViewController setSubject:[NSString stringWithFormat:@"Dob in a Hoon Report(Y)"]];
            [mailViewController setMessageBody:[NSString stringWithFormat:@"Am I prepared to be called upon as a witness?: %@ \n Rego Number: %@ \n  Vehicle Make: %@ \n Vehicle Colour: %@ \n Vehicle Type: %@ \n Incident Date: %@ \n Incident Time: %@ \n Incident Location: %@ \n Do I know the Driver?: %@ \n Drivers Name (if Known) %@ \n Driver Gender: %@ \n Description of Incident: %@ \n Name of Person Reporting Incident: %@ \n Contact Number: %@ \n" , witnessResponse, regoNumber.text, vehilceMake.text, vehilceColour.text, vehicleType.text, incidentDate.text, incidentTime.text, location.text, driverKnownResponse, driverName.text, driverGenderResponse, additionalInfo.text, usersName.text, usersPhoneNumber.text] isHTML:NO];
            [self presentViewController:mailViewController animated:YES completion:nil];
            NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
            [mailViewController addAttachmentData:imageData  mimeType:@"image/jpg"   fileName: [NSString stringWithFormat:@"Dob in a Hoon %@ , %@.jpg", Latitude,longditute]];
            [mailViewController setToRecipients:toRecipients];

        } else {

            MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc]init];
            mailViewController.mailComposeDelegate = self;
            [mailViewController setSubject:[NSString stringWithFormat:@"Dob in a Hoon Report(N)"]];
            [mailViewController setMessageBody:[NSString stringWithFormat:@"Am I prepared to be called upon as a witness?: %@ \n Rego Number: %@ \n  Vehicle Make: %@ \n Vehicle Colour: %@ \n Vehicle Type: %@ \n Incident Date: %@ \n Incident Time: %@ \n Incident Location: %@ \n Do I know the Driver?: %@ \n Drivers Name (if Known) %@ \n Driver Gender: %@ \n Description of Incident: %@ \n Name of Person Reporting Incident: %@ \n Contact Number: %@ \n" , witnessResponse, regoNumber.text, vehilceMake.text, vehilceColour.text, vehicleType.text, incidentDate.text, incidentTime.text, location.text, driverKnownResponse, driverName.text, driverGenderResponse, additionalInfo.text, usersName.text, usersPhoneNumber.text] isHTML:NO];
            [self presentViewController:mailViewController animated:YES completion:nil];
            NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
            [mailViewController addAttachmentData:imageData  mimeType:@"image/jpg"   fileName: [NSString stringWithFormat:@"Dob in a Hoon %@ , %@", Latitude,longditute]];
            [mailViewController setToRecipients:toRecipients];

        }



    }



    else{

        NSLog(@"Device is unable to send email");
    }
}

我的目标是:如果此人在该区域之外,我想显示一个UIAlert,通知用户他们在该区域之外。如果此人在该区域内,则将通过电子邮件设置。

1 个答案:

答案 0 :(得分:2)

您需要CLLocationManager和startMonitoringForRegion:等方法,它们使用CLCircularRegion对象来定义地理区域。

如果您查看Xcode文档中的startMonitoringForRegion,它会有一个名为“Regions”的演示项目的链接,这应该可以帮助您入门。