Last active
October 13, 2015 03:08
-
-
Save imrekel/4129904 to your computer and use it in GitHub Desktop.
bme-ios - Messenger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UITableViewCell *)tableView:(UITableView *)tableView | |
cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *CellIdentifier = @"MessagesCell"; | |
UITableViewCell *cell = [tableView | |
dequeueReusableCellWithIdentifier:CellIdentifier]; | |
MEMessage* message = [_messages objectAtIndex:indexPath.row]; | |
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", | |
message.fromUser, message.toUser]; | |
cell.detailTextLabel.text = message.topic; | |
return cell; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSURL* url = [NSURL URLWithString:@"http://atleast.aut.bme.hu/ios/messenger/add-message"]; | |
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; | |
request.HTTPMethod = @"POST"; | |
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; | |
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig | |
delegate:nil | |
delegateQueue:nil]; | |
NSURLSessionUploadTask *postTask = [session uploadTaskWithRequest:request fromData:xml completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | |
}]; | |
[postTask resume]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSError* xmlError = nil; | |
DDXMLDocument* xmlDoc = [[DDXMLDocument alloc] | |
initWithXMLData:data options:0 | |
error:&xmlError]; | |
if (xmlError) | |
{ | |
NSLog([xmlError description]); | |
} | |
else | |
{ | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
UIAlertView* alertView = [[UIAlertView alloc] | |
initWithTitle: @"A szerver válasza" | |
message:[xmlDoc.rootElement stringValue] | |
delegate:nil | |
cancelButtonTitle:@"Ok" | |
otherButtonTitles:nil]; | |
[alertView show]; | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)composeMessageViewControllerDidSend: | |
(MEComposeMessageViewController *)viewController | |
{ | |
DDXMLDocument* document = [[DDXMLDocument alloc] | |
initWithXMLString:@"<message />" options:0 error:nil]; | |
DDXMLElement* message = [document rootElement]; | |
[message addAttribute:[DDXMLNode attributeWithName:@"from_user" | |
stringValue:kUserName]]; | |
[message addAttribute:[DDXMLNode attributeWithName:@"to_user" | |
stringValue:viewController.toUserTextField.text]]; | |
[message addAttribute:[DDXMLNode attributeWithName:@"topic" | |
stringValue:viewController.topicTextField.text]]; | |
// CDATA elemeket csak trükközéssel támogatja a KissXML | |
NSString* contentNodeString = [NSString | |
stringWithFormat:@"<content><![CDATA[%@]]></content>", | |
viewController.contentTextView.text]; | |
DDXMLElement* contentElement = [[DDXMLDocument alloc] | |
initWithXMLString:contentNodeString options:DDXMLDocumentXMLKind | |
error:nil].rootElement; | |
[message addChild:[contentElement copy]]; | |
NSData* xml = [document XMLData]; | |
NSLog([[NSString alloc] initWithData:xml | |
encoding:NSUTF8StringEncoding]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock | |
{ | |
NSString* currentElementName = [_currentXmlElements lastObject]; | |
if ([currentElementName isEqualToString:@"content"]) | |
{ | |
MEMessage* message = [self.messages lastObject]; | |
message.content = | |
[[NSString alloc] initWithData:CDATABlock | |
encoding:NSUTF8StringEncoding]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__weak UITableViewCell *weakCell = cell; | |
NSURL* imageUrl = [NSURL URLWithString:message.imageUrl]; | |
// nem ideális megoldás, mert minden alkalommal újra letölti a képet, ha egy cella megjelenik | |
// egy éles alkalmazásban a képek letöltését és tárolását mindenképp külön kell kezelni | |
NSURLSessionDataTask* imageDownloadTask = [_urlSession dataTaskWithURL:imageUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
UIImage* image = [UIImage imageWithData:data]; | |
weakCell.imageView.image = image; | |
[weakCell setNeedsLayout]; | |
}); | |
}]; | |
[imageDownloadTask resume]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict | |
{ | |
if ([elementName isEqualToString:@"message"]) | |
{ | |
MEMessage* message = [[MEMessage alloc] init]; | |
message.fromUser = [attributeDict objectForKey:@"from_user"]; | |
message.toUser = [attributeDict objectForKey:@"to_user"]; | |
message.topic = [attributeDict objectForKey:@"topic"]; | |
message.imageUrl = [attributeDict objectForKey:@"imageuri"]; | |
[self.messages addObject:message]; | |
} | |
[_currentXmlElements addObject:elementName]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment