Objective-C open links in Safari and Alert Dialog - ios

I have a UIwebView, it has a few links within the webpage. I want a certain link to open an alert modal like in image #1
Also, how can use the code below to make something like this - (IMAGE #1) http://screenshot.it.sftcdn.net/blog/it/2014/01/Block-user-03-Tasto-Block-378x568.png in objective-c
- (IBAction)showAlert:(id)sender
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Open In..."
message:#"Which app would you like to open?"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *mapsAction = [UIAlertAction actionWithTitle:#"Maps"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
if (![self openURLForString:#"maps://"]) {
NSLog(#"Couldn't Open Maps");
}
}];
UIAlertAction *youtubeAction = [UIAlertAction actionWithTitle:#"YouTube"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
if (![self openURLForString:#"http://www.youtube.com/watch?v=dQw4w9WgXcQ"]) {
NSLog(#"Couldn't Open YouTube");
}
}];
UIAlertAction *messagesAction = [UIAlertAction actionWithTitle:#"Messages"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
if (![self openURLForString:#"sms://"]) {
NSLog(#"Couldn't Open Messages");
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
[alertController addAction:mapsAction];
[alertController addAction:youtubeAction];
[alertController addAction:messagesAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
- (BOOL)openURLForString:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
return YES;
}
return NO;
}
Where the "report inappropriate" being a link opening in a second UIwebView that opens up OVER the parent webview and the "share" opens up in Safari.
(Disregard the "copy link/URL")

You would need to first register your view controller for the web view protocol.
And then in your view controller implement the delegate method for web view did start loading and check the URL being called and do your tasks accordingly using an if else check
Here's the delegate reference:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/
Answered from my phone. Pardon syntax.
Hope this helps.

Related

Detect Alert Button Click Event

I have the following implementation to be reused entire app. I store the following code in the Utility.m class.
CustomViewController.m
How can I capture click event in the following
[self presentViewController:[Utility oneButtonDisplayAlert:#"Error" withMessage:#"Please try again later"] animated:YES completion:nil];
Utility.m
+ (UIAlertController *)oneButtonDisplayAlert : (NSString*)title withMessage : (NSString*) message
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
[alert addAction:yesButton];
return alert;
}
Add a block parameter to your oneButtonDisplayAlert:withMessage: method. Call that block inside the alert action's handler.
+ (UIAlertController *)oneButtonDisplayAlert:(NSString *)title withMessage:(NSString *)message andOKHandler:(void (^)(void))handler
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (handler) {
handler();
}
}];
[alert addAction:yesButton];
return alert;
}
And then call it as:
UIAlertController *alert = [Utility oneButtonDisplayAlert:#"Error" withMessage:#"Please try again later" andOKHandler:^{
// whatever code you need when OK tapped
}];
[self presentViewController:alert animated:YES completion:nil];
Note: Code in this answer might have a typo. Syntax not verified.

UIAlertController Action isnt working?

I have added a UIAlterController to my view. IT triggers fine, looks fine, and has its action, however tapping the Cancel action does nothing, it doesnt run any code, even if i add a block it wont execute it, it just doesnt seem to recognise the tap at all.
The code looks fine to me, so is there some other cause?
- (void)connectionFailed:(NSString *)title {
NSString *message = nil;
if ([title isEqualToString:NSLocalizedString(#"Connection Refused", #"connection Refused")]) {
message = NSLocalizedString(#"You do not have permission to use this console", #"incorrect permissions message");
}
else {
message = NSLocalizedString(#"Connection Failed", #"connection failed error message");
}
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:nil];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
});
Edit: it appears the issue is down to view leaks causing problems, not sure of the fix but here is the breakdown in debugging:
debugging image of view stack
Use this:
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
Change UIAlertActionStyleDefault to UIAlertActionStyleCancel
Try this:
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
//your code here ...
}];
- (void)connectionFailed:(NSString *)title {
NSString *message = nil;
if ([title isEqualToString:NSLocalizedString(#"Connection Refused", #"connection Refused")]) {
message = NSLocalizedString(#"You do not have permission to use this console", #"incorrect permissions message");
}
else {
message = NSLocalizedString(#"Connection Failed", #"connection failed error message");
}
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style: UIAlertActionStyleCancel
handler:nil];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
});
just edited your cancel button alert style....
Try this :
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
UIAlertActionStyleDefault
handler:nil];

How to create UIActionSheet or UIAlertController(UIAlertControllerStyleActionSheet) like Apple Music?

In my app, I want to create a UIActionSheetor UIAlertController (UIAlertControllerStyleActionSheet) like Apple Music.
I want to show user profile picture and some basic details of user in first row of UIActionSheet and other option like normal UIActionSheet.
In somewhat I can give you the sample coding for your question
UIAlertController *alertMusicVideos = [UIAlertController
alertControllerWithTitle:#"Music Videos"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionAddProfilePhoto = [UIAlertAction
actionWithTitle:#""
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionPlayNext = [UIAlertAction
actionWithTitle:#"Play Next"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionAddToUpNext = [UIAlertAction
actionWithTitle:#"Add to Up Next"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here when click this button
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionAddToPlayList = [UIAlertAction
actionWithTitle:#"Add To PlayList"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here when click this button
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionDeleteFromMyMusic = [UIAlertAction
actionWithTitle:#"Delete From My Music"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here when click this button
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *actionCancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
//Do Whatever you want to do here when click this button
[alertMusicVideos dismissViewControllerAnimated:YES completion:nil];
}];
[actionAddProfilePhoto setValue:[[UIImage imageNamed:#"yourProfile.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:#"image"];
[alertMusicVideos addAction:actionAddProfilePhoto];
[alertMusicVideos addAction:actionPlayNext];
[alertMusicVideos addAction:actionAddToUpNext];
[alertMusicVideos addAction:actionAddToPlayList];
[alertMusicVideos addAction:actionDeleteFromMyMusic];
[alertMusicVideos addAction:actionCancel];
[self presentViewController:alertMusicVideos animated:YES completion:nil];

Open Application settings bundle inside my App

I am using this code to present App settings
- (IBAction)showSettings:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
I want to show the settings inside my Application, not jump to iPhone settings.
Can anyone help?
Modified:
I try to write this code in objective C:
UIAlertController *alertControll = [UIAlertController alertControllerWithTitle:#"Sad Face Emoji!"
message:#"The calendar permission was not authorized. Please enable it in Settings to continue."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:#"Settings"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:nil];
[alertControll addAction:settingsAction];
[alertControll addAction:cancelAction];
[self presentViewController:alertControll animated:YES completion:nil];
But still jumping to iPhone settings

how to prevent ABPeoplePickerNavigationController from automatically dismissing a presented view

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:#"testTitle"
message:#"test"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* okAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"User clicked button called %#",action.title);
}];
UIAlertAction* cancelAction = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"User clicked button called %#",action.title);
}];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
After selecting a property, the Alert will show up for a second then dismisses itself.
How can I prevent ABP from dismissing my alert?
OR
How can I use another event to bring my alert up to the user.

Resources