Can a UIWebView raise events to the containing view controller? - iphone-sdk-3.0

I have a UIWebController embedded in a UIViewController.
I write the HTML code for the UIViewcontroller so I have complete control over the HTML page.
When the user clicks a button in the HTML page - can the containing UIViewController be notified about it?
Can the UIWebView send/raise an event to the UIViewControler?

You need to implement the webView:shouldStartLoadRequest:navigationType: delegate method for your web view (Apple Docs).
Here's a quick example:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
if ([[url scheme] isEqualToString:#"someLinkURL"]) {
// Display alert
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"I'm a message!" delegate:self cancelButtonTitle:#"Okay!" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
return NO;
}
return YES;
}
Your VC must be the webViewDelegate for this method to work.
If you want to handle all clicks manually (i.e. don't go to other pages in the web view) return NO on that final line.

Related

Load a web view after opening a notification alert?

I made a RSS app, it works perfectly, I also added Push Notification Support, everything is good so far, am passing a title, alert, and a url as an extra field.
When my notification arrives, I show my alert, and you have 2 options "Thanx | Open"
All I want to do now is, when you click open from this alert, I need to redirect my user to a view that has a UIwebview and load the url I passed along with the notification.
Can anyone help me?
Here is my code:
// here is my app delegate
-(void)onReceivePushNotification:(NSDictionary *) pushDict andPayload:(NSDictionary *)payload {
[payload valueForKey:#"title"];
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"New Alert !" message:[pushDict valueForKey:#"alert"] delegate:self cancelButtonTitle:#"Thanks !" otherButtonTitles: #"Open",nil];
NSLog(#"message was : %#", [pushDict valueForKey:#"alert"]);
NSLog(#"url was : %#", [payload valueForKey:#"url"]);
[message show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Open"]) {
[[Pushbots getInstance] OpenedNotification];
// Decrease Badge count by 1
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[UIApplication sharedApplication].applicationIconBadgeNumber -1];
// Decrease badge count on the server
[[Pushbots getInstance] decreaseBadgeCountBy:#"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateRoot" object:nil];
} else {
// set Badge to 0
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
// reset badge on the server
[[Pushbots getInstance] resetBadgeCount];
}
}
My detail view is called : "APPDetailViewController", with a web view called "webView"
Please help me?
Thanks in advance.
You could create an instance of APPDetailViewController in your clickedButtonAtIndex method and present it. If you have tabs, you can switch to that tab, and then have a method to pass the url to so that it opens that page.

UIAlertView for user to download another app

I downloaded an app from app store and noticed that once I installed and opened the app, I got a UIAlertView suggesting that I download another app..
How can I achieve this? Are there any existing code that I can be directed towards?
Seems like a simple feature but I'd like to integrate it. Please see attachment
Here is my edited code, but the 'Hello' button won't link anywhere.. What am I doing wrong?
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Hello World!"
message:#"This is your first UIAlertview message."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:#"Hello", nil];
[message show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Hello"])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"https://itunes.apple.com/gb/app/islam/id667021108?mt=8&affId=1930871&ign-mpt=uo%3D4"]];
}
}
first you have to make alertView, if you want to show the alert as first thing but this code inside (ViewDidLoad) method
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"your title"
message:#"your message"
delegate:self
cancelButtonTitle:#"Not now"
otherButtonTitles:#"Download now"
, nil];
[alert show];
after viewDidLoad but this method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"here but your application link"]];
}else{
}
}
//=================================================//
To know what's NSUserDefaults, I suggest to take a look the official doc.
And of course you can use it to fulfill your goal. You use a user default to store information about the current amount of runs in the app.
More or less like:
BOOL NotTheFirstTime = [[NSUserDefaults standardUserDefaults] boolForKey:#"NotTheFirstTime"];
if(!NotTheFirstTime){
// Show the alert view
// Then set the first run flag
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"NotTheFirstTime"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if that help you please make it the best question.
It is pretty simple:
1) Set up an UIAlertView as usual with the text and the button titles you like.
2) Set up the UIAlertView's delegate and use this method to figure out which button was clicked (whether the user does or does not want to go to the App Store):
alertView:clickedButtonAtIndex:
3) If the user wants to go to the App Store, open up an App Store link:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"<#This is your App Store Link#>"]];

error: address doesn't contain a section that points to a section in a object file

Hi i have searched here on the forum but no help found so i am posting it new. Here is the scenario, i am creating a mfmailcomposeviewcontroller in the main rootviewcontroller, i am displaying it by calling presentviewcontroller but when it is dismissed i get this error :
error: address doesn't contain a section that points to a section in a object file
The code i am using is given below:
-(void) mailButtonTapped
{
if ([MFMailComposeViewController canSendMail]) {
mailViewController_ = [[MFMailComposeViewController alloc] init];
mailViewController_.mailComposeDelegate = self;
[mailViewController_ setSubject:#"Try ..."];
[mailViewController_ setMessageBody:#"Hey I just tried ..." isHTML:NO];
NSData *videoData = [NSData dataWithContentsOfURL:movieURL_];
[mailViewController_ addAttachmentData:videoData mimeType:#"video/quicktime" fileName:#"Video.mov"];
[self presentViewController:mailViewController_ animated:YES completion:nil];
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sharing Not Possible" message:#"Configure your mail to send the mail" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
[alertView release];
}
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
NSString *title = #"Email";
NSString *msg = nil;
if (result == MFMailComposeResultFailed)
msg = #"Unable to send, check your email settings";
else if (result == MFMailComposeResultSent)
msg = #"Email Sent Successfully!";
else if (result == MFMailComposeResultCancelled || result == MFMailComposeResultSaved)
msg = #"Sending Cancelled";
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
[self dismissViewControllerAnimated:YES completion:nil];
}
After dismissing i receive the error:
error: address doesn't contain a section that points to a section in a object file
Please help me
I also had this error but with another scenario. I had a block property defined with #property (assign, nonatomic).
To fix this issue, I declared my block property with #property (copy, nonatomic).
Cheers
This error happens when an object / pointer is being accessed that doesn't exist anymore . And can also cause other bad access, 0x00000 value accessed etc.. errors.
So you are deleting/releasing a pointer , and then accessing later .
From looking at the code, and this is just a guess without debugging, you set the second AlertView's delegate to self, but then immediately dismiss the viewcontroller.
Try dismissing after the alert view is dismissed or the button is pressed, or maybe just setting the AlertView delegate to nil.
Even if that's not exactly the error, the main reason is somewhere you are releasing an object then trying to call a function or access it.
You can use it like this:
MFMailComposeViewController *mailViewController_ = [[MFMailComposeViewController alloc] init];
mailViewController_.mailComposeDelegate = self;
[mailViewController_ setSubject:#"Try ..."];
[mailViewController_ setMessageBody:#"Hey I just tried ..." isHTML:NO];
NSData *videoData = [NSData dataWithContentsOfURL:movieURL_];
[mailViewController_ addAttachmentData:videoData mimeType:#"video/quicktime" fileName:#"Video.mov"];
[self presentViewController:mailViewController_ animated:YES completion:nil];
[mailViewController_ release];
I also had this problem but caused by a very silly error, i wrote a property called frameon a class that inherits from UIView (it was a UITableViewCell but i think this would happen with every class that inherits from UIView) this overwrote the original frame property and caused this error.
Fixed just by changing property name.
The completion expects a block to be called when the animation of dismissal is completed.
Just remove "completion:nil" and it should work!
Regards.

Unable to send email through my app

I am new to iphone development. I am building an app through which i can send the email.I am using the following code
- (IBAction)btnsendPressAction:(id)sender {
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mailer=[[MFMailComposeViewController alloc]init];
mailer.mailComposeDelegate=self;
[mailer setSubject:#"A Message for testing"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"rajshetty2125#gmail.com", #"secondMail#example.com", nil];
[mailer setToRecipients:toRecipients];
UIImage *image=[UIImage imageNamed:#"logo.png"];
NSData *imageData=UIImagePNGRepresentation(image);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"logo.png"];
NSString *emailBody=#"This is test email";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
mailer.modalPresentationStyle = UIModalPresentationPageSheet ;
}
else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"failure" message:#"Alert" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
My problem is mailComposeController delegate is not getting called and I am not able to send email through my app.I went through many links , but every where I found the same method. Can anyone help me on this ?
You won't be able to send email from Simulator. You can only check this functionality on iOS device with any account configured in Mail application.
Here use this link and your add(configured) an account in device then only you can send the mail.
This snippet is working for me:
if ([MFMailComposeViewController canSendMail]) {
self.mailController = [[MFMailComposeViewController alloc] init];
[self.mailController setSubject:#"Invitation"];
[self.mailController setMessageBody:#"Message" isHTML:NO];
self.mailController.mailComposeDelegate = self;
[self presentModalViewController:self.mailController animated:YES];
}
but make sure that you're implement delegate in interface
#interface MyClass() <MFMailComposeViewControllerDelegate>
and check your callback method:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}
hey i am not sure but may be it's helpful, replace your last line
mailer.modalPresentationStyle = UIModalPresentationPageSheet ;
with
[self presentModalViewController: mailer animated:YES];

Trying to Integrate Mail Into my app, getting 2 warnings

I'm trying to integrate sending mail into my app, but I end up with 2 warnings. I am using Obj-C, the Cocos2d Framework. This is my code.
-(void) mailTapped: (id) sender {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"", nil]];
[composer setSubject:#"Check Out This Awesome App!"];
[composer setMessageBody:#"I found this great game on the App Store! It's called Mole Attack. It's a side scroller with an epic story. You can check out some screenshots of the gameplay and download it here. Download link - " isHTML:NO]; //Include link and pics
[self presentModalViewController:composer animated:YES]; // <--- warning - GameOver (name of class) may not respond to '-presentModalViewController:animated:'
}
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES]; // <--- warning - GameOver may not respond to '-dismissModalViewControllerAnimated:YES'
if (result == MFMailComposeResultFailed) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"The email was not sent. You must be in wifi or 3G range. Try again later." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Thanks in advance!
Are you sure your GameOver class is inheriting from UIViewController? That's the class that defines the two methods that you're getting warnings about.