- (IBAction)selectFont:(id)sender { [self saveSetting]; } - (void) saveSetting { NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; // [userDefaults setInteger:self.fontSegmentedControl.selectedSegmentIndex forKey:kSettingsFont]; [userDefaults synchronize]; } - (void) loadSetting { NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; // self.fontSegmentedControl.selectedSegmentIndex = [userDefaults integerForKey:kSettingsFont]; }
- (void) loadPage { NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; NSInteger indexFont = [userDefaults integerForKey:@"font"]; if (indexFont == 0) { // NSString* filePath = [[NSBundle mainBundle] pathForResource:@"preOnePart" ofType:@"html"]; // NSString NSString* html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; // UIWebView [_webView loadHTMLString:html baseURL:nil]; } else if (indexFont == 1) { // NSString* filePath = [[NSBundle mainBundle] pathForResource:@"preOnePartMediumFont" ofType:@"html"]; // NSString NSString* html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; // UIWebView [_webView loadHTMLString:html baseURL:nil]; } else if (indexFont == 2) { // NSString* filePath = [[NSBundle mainBundle] pathForResource:@"preOnePartLargeFont" ofType:@"html"]; // NSString NSString* html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; // UIWebView [_webView loadHTMLString:html baseURL:nil]; }
#pragma mark - share actions - (IBAction)twitterShare:(UIButton *)sender { if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) { SLComposeViewController* controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; [controller setInitialText:@" #AppStore «Military! - & » App Store. ;)"]; [controller addURL:[NSURL URLWithString:@"https://itunes.apple.com/ru/app/military!-novosti-ustav-vnutrennej/id914651209?l=en&mt=8"]]; controller.completionHandler = ^(SLComposeViewControllerResult result) { NSLog(@"Completed"); }; [self presentViewController:controller animated:YES completion:nil]; } else { UIAlertView* error = [[UIAlertView alloc] initWithTitle:@"" message:@" Twitter- . \n , !" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil]; [error show]; NSLog(@"The twitter sevice is not evailalble"); } } - (IBAction)facebookShare:(UIButton *)sender { // Put together the dialog parameters NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Military!", @"name", @" & ", @"caption", @" , , !", @"description", @"https://itunes.apple.com/ru/app/military!-novosti-ustav-vnutrennej/id914651209?l=en&mt=8", @"link", @"http://cs619627.vk.me/v619627853/19d6e/xdHr-XTVs8k.jpg", @"picture", nil]; // Show the feed dialog [FBWebDialogs presentFeedDialogModallyWithSession:nil parameters:params handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) { if (error) { // An error occurred, we need to handle the error // See: https://developers.facebook.com/docs/ios/errors NSLog(@"Error publishing story: %@", error.description); } else { if (result == FBWebDialogResultDialogNotCompleted) { // User cancelled. NSLog(@"User cancelled."); } else { // Handle the publish feed callback NSDictionary *urlParams = [self parseURLParams:[resultURL query]]; if (![urlParams valueForKey:@"post_id"]) { // User cancelled. NSLog(@"User cancelled."); } else { // User clicked the Share button NSString *result = [NSString stringWithFormat: @"Posted story, id: %@", [urlParams valueForKey:@"post_id"]]; NSLog(@"result %@", result); } } } }]; } //------------------Login implementation starts here------------------ - (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error { NSString *alertMessage, *alertTitle; // If the user should perform an action outside of you app to recover, // the SDK will provide a message for the user, you just need to surface it. // This conveniently handles cases like Facebook password change or unverified Facebook accounts. if ([FBErrorUtility shouldNotifyUserForError:error]) { alertTitle = @"Facebook error"; alertMessage = [FBErrorUtility userMessageForError:error]; // This code will handle session closures since that happen outside of the app. // You can take a look at our error handling guide to know more about it // https://developers.facebook.com/docs/ios/errors } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) { alertTitle = @"Session Error"; alertMessage = @"Your current session is no longer valid. Please log in again."; // If the user has cancelled a login, we will do nothing. // You can also choose to show the user a message if cancelling login will result in // the user not being able to complete a task they had initiated in your app // (like accessing FB-stored information or posting to Facebook) } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) { NSLog(@"user cancelled login"); // For simplicity, this sample handles other errors with a generic message // You can checkout our error handling guide for more detailed information // https://developers.facebook.com/docs/ios/errors } else { alertTitle = @"Something went wrong"; alertMessage = @"Please try again later."; NSLog(@"Unexpected error:%@", error); } if (alertMessage) { [[[UIAlertView alloc] initWithTitle:alertTitle message:alertMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; } } - (NSDictionary*)parseURLParams:(NSString *)query { NSArray *pairs = [query componentsSeparatedByString:@"&"]; NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; for (NSString *pair in pairs) { NSArray *kv = [pair componentsSeparatedByString:@"="]; NSString *val = [kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; params[kv[0]] = val; } return params; }
#pragma mark - NSXMLParser - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { element = elementName; if ([element isEqualToString:@"item"]) { item = [[NSMutableDictionary alloc] init]; title = [[NSMutableString alloc] init]; link = [[NSMutableString alloc] init]; image = [[NSMutableDictionary alloc] init]; description = [[NSMutableString alloc] init]; date = [[NSMutableString alloc] init]; } if ([element isEqualToString:@"enclosure"]) { linkImage = [attributeDict objectForKey:@"url"]; typeImage = [attributeDict objectForKey:@"type"]; } // NSLog(@"RSS Utility: didStartElement: %@", elementName); } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"item"]) { [item setObject:title forKey:@"title"]; [item setObject:link forKey:@"link"]; [item setObject:description forKey:@"description"]; [item setObject:image forKey:@"enclosure"]; [item setObject:typeImage forKey:@"imageType"]; [item setObject:linkImage forKey:@"imageUrl"]; [item setObject:date forKey:@"pubDate"]; [feeds addObject:[item copy]]; } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if ([element isEqualToString:@"title"]) { [title appendString:string]; } else if ([element isEqualToString:@"link"]) { [link appendString:string]; } else if ([element isEqualToString:@"imageUrl"]) { [linkImage appendString:string]; } else if ([element isEqualToString:@"description"]) { [description appendString:string]; } else if ([element isEqualToString:@"pubDate"]) { [date appendString:string]; } } - (void)parserDidEndDocument:(NSXMLParser *)parser { [self.tableView reloadData]; [self.refreshControl endRefreshing]; }
#pragma mark - Passed data to another controller - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"CustomSegue"]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; NSString *string = [feeds[indexPath.row] objectForKey: @"link"]; [[segue destinationViewController] setUrl:string]; NSString* string2 = [feeds [indexPath.row] objectForKey:@"title"]; [[segue destinationViewController] setTitleForShare:string2]; } }
@implementation IVDetailViewController @synthesize titleForShare; - (void)viewDidLoad { [super viewDidLoad]; NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSURLRequest *request = [NSURLRequest requestWithURL:myURL]; [self.webView loadRequest:request]; [self.navigationController.navigationBar addGestureRecognizer:self.revealViewController.panGestureRecognizer]; // Do any additional setup after loading the view. }
- (IBAction)activityAction:(id)sender { NSString *textToShare = titleForShare; NSURL *myWebsite = [NSURL URLWithString:self.url]; NSArray *objectsToShare = @[textToShare, myWebsite]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil]; NSArray *excludeActivities = @[UIActivityTypeAirDrop, UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo]; activityVC.excludedActivityTypes = excludeActivities; [self presentViewController:activityVC animated:YES completion:nil]; }
Source: https://habr.com/ru/post/240881/
All Articles