(iOS)how to make the button and label working? - uibutton

please take a look my code
-(IBAction)clickButton1:(id)sender{
label1.hidden=NO;
button1.userInteractionEnabled = NO;
NSString *sql = [NSString stringWithFormat:#"SELECT * FROM table order by id asc"];
NSArray *all = [datebase selectData:sql columns:71];
index = [all count]-1;
if (index < 0) {
NSLog(#"no");
}
else{
for(i=0;i<=index;i++){
NSArray *row = [all objectAtIndex:i];
NSString *serid = [row objectAtIndex:0];
NSString *logtime = [row objectAtIndex:1];
NSString *post =[NSString stringWithFormat:#"?id=%#&logtime=%#",id,ttime];
NSString *searchTerm = [post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *hostStr = #"http://www.theurl.com/sy.php";
hostStr = [hostStr stringByAppendingString:searchTerm];
NSData *dataURL = [NSData dataWithContentsOfURL: [NSURL URLWithString: hostStr]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSUTF8StringEncoding];
if([serverOutput isEqualToString:#"1"]){
if (i==index) {
NSLog(#"success");
}
}
else {
NSLog(#"error");
}
}
}
}
i use the code,and clicked the button, but the label is still hidden, and so to the button1, but if i move the code only left
-(IBAction)clickButton1:(id)sender{
label1.hidden=NO;
button1.userInteractionEnabled = NO;
}
ok they work, so what's wrong with my code? can anybody help me? many thanks

USE NSLog(#"bla bla bla") for trace, like this:
-(IBAction)clickButton1:(id)sender {
NSLog(#"bla bla bla")
label1.hidden=NO;
button1.userInteractionEnabled = NO;
}
to check connection with label1 and button1 in IB. (outlets)

Related

Image not displayed on UICollectionViewCell

I am trying to get images on contacts,here i used UICollectionViewCell but in the collection view i didn't get image for the contact,i get only name and number.Here my code is
- (IBAction)ContactDisplay:(id)sender {
_addressBookController = [[ABPeoplePickerNavigationController alloc] init];
[_addressBookController setPeoplePickerDelegate:self];
[self presentViewController:_addressBookController animated:YES completion:nil];
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
[self displayPerson:person];
}
- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
NSLog(#"%#",name);
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = #"[None]";
}
NSLog(#"%#",phone);
UIImage *img ;
if (person != nil && ABPersonHasImageData(person)) {
if ((&ABPersonCopyImageDataWithFormat) != nil ) {
img= [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
}
} else {
NSString *imageUrlString = #"http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
NSURL *url = [NSURL URLWithString:imageUrlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
img= [UIImage imageWithData:data];
}
NSString *string ;//
string =[NSString stringWithFormat:#"%#",img];
NSLog(#"%#",img);
self.name.text=name;
self.number.text=phone;
[self.nameArray addObject:name];
[self.imageArray addObject:string];
NSLog(#"%#",self.nameArray);
NSLog(#"%#",self.imageArray);
[self.collectionView reloadData];
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:nil];
}
finally an image array i got like this
(
"add-button.png",
"<UIImage: 0x17e56c80>, {148, 148}"
)
On image array every image like display .PNG format it will display fine ,then how can modify it.
Can you please suggest me how can you solve this,thank you.
I don't fully agree with everything you're doing there but I think you're getting your data wrong. Try using this instead when you're fetching the ABPerson image data.
if (person != nil) {
CFDataRef imageData = ABPersonCopyImageData(person);
NSData *data = CFBridgingRelease(imageData);
if (data != nil && data.length > 10){ //arbitrary length to make sure our data object isnt' really empty
img = [UIImage imageWithData:data];
} else {
NSString *imageUrlString = #"http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
NSURL *url = [NSURL URLWithString:imageUrlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
img= [UIImage imageWithData:data];
}
Then don't store your images as Strings in your array. Store them either as NSData or UIImage, but NOT STRINGS.
so
[myArray addObject:img]; //not the string.
And when you fetch it later, make sure you treat is as an image and not as a string
on your storyboard, select the image and look at the properties panel.
there are "Installed" options at the bottom. check the topmost "Installed" box.
I think there might be issue with conversion of image to string
NSString *string ;//
string =[NSString stringWithFormat:#"%#",img];
Add image to image array without converting to string
[self.imageArray addObject:img];
I do it like this in my app. Assuming 'person' is an ABRecordRef.
NSMutableDictionary *contactInfoDict = [[NSMutableDictionary alloc]
initWithObjects:#[#"", #"", #"", #""]
forKeys:#[#"firstName", #"lastName", #"birthday", #"picture"]];
CFTypeRef generalCFObject;
// Firtname
generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:#"firstName"];
CFRelease(generalCFObject);
}
// Lastname
generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:#"lastName"];
CFRelease(generalCFObject);
}
// Birthday
generalCFObject = ABRecordCopyValue(person, kABPersonBirthdayProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:#"birthday"];
NSLog(#"Date : %#", [contactInfoDict objectForKey:#"birthday"]);
CFRelease(generalCFObject);
}
// User image
CFDataRef photo = ABPersonCopyImageData(person);
if (photo) {
CFRelease(photo);
UIImage *image = [UIImage imageWithData:(__bridge NSData*)photo];
[contactInfoDict setObject:image forKey:#"picture"];
}

iOS AVFoundation: How do I fetch artwork from an mp3 file?

My code:
- (void)metadata {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];
NSArray *titles = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
NSArray *artists = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon];
NSArray *albumNames = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon];
AVMetadataItem *artwork = [artworks objectAtIndex:0];
AVMetadataItem *title = [titles objectAtIndex:0];
AVMetadataItem *artist = [artists objectAtIndex:0];
AVMetadataItem *albumName = [albumNames objectAtIndex:0];
if ([artwork.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSDictionary *dictionary = [artwork.value copyWithZone:nil];
self.currentSongArtwork = [UIImage imageWithData:[dictionary objectForKey:#"data"]];
}
else if ([artwork.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
self.currentSongArtwork = [UIImage imageWithData:[artwork.value copyWithZone:nil]];
}
self.currentSongTitle = [title.value copyWithZone:nil];
self.currentSongArtist = [artist.value copyWithZone:nil];
self.currentSongAlbumName = [albumName.value copyWithZone:nil];
self.currentSongDuration = self.audioPlayer.duration;
}
This works for fetching artwork from m4a files, but doesn’t work for mp3 files. If the asset points to an mp3 file, artworks is empty. What am I doing wrong and how do I fix it?
I found that the artworks were being loaded asynchronously while the image was being set synchronously. I solved it this way:
- (void)metadata {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];
NSArray *titles = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
NSArray *artists = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon];
NSArray *albumNames = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon];
AVMetadataItem *title = [titles objectAtIndex:0];
AVMetadataItem *artist = [artists objectAtIndex:0];
AVMetadataItem *albumName = [albumNames objectAtIndex:0];
NSArray *keys = [NSArray arrayWithObjects:#"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
withKey:AVMetadataCommonKeyArtwork
keySpace:AVMetadataKeySpaceCommon];
for (AVMetadataItem *item in artworks) {
if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSDictionary *d = [item.value copyWithZone:nil];
self.currentSongArtwork = [UIImage imageWithData:[d objectForKey:#"data"]];
} else if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
self.currentSongArtwork = [UIImage imageWithData:[item.value copyWithZone:nil]];
}
}
}];
self.currentSongTitle = [title.value copyWithZone:nil];
self.currentSongArtist = [artist.value copyWithZone:nil];
self.currentSongAlbumName = [albumName.value copyWithZone:nil];
self.currentSongDuration = self.audioPlayer.duration;
}
I just found the answer to that here: How can I extract metadata from mp3 file in ios development and was now looking to see why that code doesn't work on an m4a file.
I took a little bit from your code and present the following which seems to work for both m4a and mp3. Note that I left the code which works for mp3 as an open else clause without the qualifier - if anybody gains more experience with this they are welcome to refine!
- (void)getMetaDataForSong:(MusicItem *)musicItem {
AVAsset *assest;
// filePath looks something like this: /var/mobile/Applications/741647B1-1341-4203-8CFA-9D0C555D670A/Library/Caches/All Summer Long.m4a
NSURL *fileURL = [NSURL fileURLWithPath:musicItem.filePath];
NSLog(#"%#", fileURL);
assest = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSLog(#"%#", assest);
for (NSString *format in [assest availableMetadataFormats]) {
for (AVMetadataItem *item in [assest metadataForFormat:format]) {
if ([[item commonKey] isEqualToString:#"title"]) {
musicItem.strSongTitle = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:#"artist"]) {
musicItem.strArtistName = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:#"albumName"]) {
musicItem.strAlbumName = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:#"artwork"]) {
UIImage *img = nil;
if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
img = [UIImage imageWithData:[item.value copyWithZone:nil]];
}
else { // if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSData *data = [(NSDictionary *)[item value] objectForKey:#"data"];
img = [UIImage imageWithData:data] ;
}
musicItem.imgArtwork = img;
}
}
}
}
As Ryan Heitner pointed out in a comment to Andy Weinstein, the way to retrieve the binary artwork data from the AVMetadataKeySpaceID3 has changed somewhere from IOS7 to IOS8. Casting blindly item to NSDictionary will crash in the ID3 case nowadays. To make code bullet proof just check dataValue, otherwise the Classes. In fact today item.value points to item.dataValue and has the NSData class.
- (UIImage*)imageFromItem:(AVMetadataItem*)item
{
NSData* data=nil;
if (item.dataValue!=nil) {
data=item.dataValue;
} else if ([item.value isKindOfClass:NSData.class]) { //never arrive here nowadays
data=(NSData*)item.value;
} else if ([item.value isKindOfClass:NSDictionary.class]) { //never arrive here nowadays...
NSDictionary* dict=(NSDictionary*)item.value;
data=dict[#"data"];
}
if (data==nil) {
return nil;
}
return [UIImage imageWithData:data];
}

How to parse the string using TBXML in iphone sdk?

I tried to parse following String using TBXML.
<panel><start><post_id>4</post_id><user_id>2</user_id><post>Hyder here</post><created_on>2012-01-09 06:36:59</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>3</post_id><user_id>2</user_id><post>Hello hyder here</post><created_on>2012-01-09 06:34:09</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>2</post_id><user_id>0</user_id><post>Hi, This is Syed Hyder....</post><created_on>2012-01-09 01:07:36</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>1</post_id><user_id>0</user_id><post>Hello, gaurav....How are you.</post><created_on>2012-01-09 01:05:11</created_on><likes>0</likes><noc>0</noc><status>A</status></start></panel>
My code: -
NSMutableArray *newArr = [[NSMutableArray alloc] init];//21
RXMLElement *rxml = [[RXMLElement alloc] initFromXMLString:response];
[rxml iterate:#"panel.start" with:^(RXMLElement *start) {
ModelPost *newPost = [[ModelPost alloc] init];
newPost.message = [NSString stringWithFormat:#"%#", [start child:#"post"]];
NSLog(#"Post = %#", [start child:#"post"]);
[newArr addObject:newPost];
}];
but I am unable to do it or enter in the iterate loop.
Please help me to over come this by sample code, suggestions and tutorials.
Thanks in Advance....
- (void)xmlparserurl{
//xml data url calling....
tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:#"http://karen.webmascot.com/iapi/orders.php?max_count=10"]] retain];
records = [NSMutableArray array];
[records retain];
//retrieving all data from the xml
if (tbxml.rootXMLElement){
NSLog(#"inserting");
[self traverseElement:tbxml.rootXMLElement];
}
[tbxml release];
}
- (void)traverseElement:(TBXMLElement *)element {
do {
//NSLog(#"%#",[TBXML elementName:element]);
if (element->firstChild)
[self traverseElement:element->firstChild];
if ([[TBXML elementName:element] isEqualToString:#"start"]) {
//NSLog(#"xml element checking");
TBXMLElement *id = [TBXML childElementNamed:#"attribute name" parentElement:element];
//inserting into the mutable array
[records addObject:[NSArray arrayWithObjects:
[TBXML textForElement:elementname],
[TBXML textForElement:elementname],
[TBXML textForElement:elementname],nil]];
}
} while ((element = element->nextSibling));
[self.tableview reloadData];
}
i am only sending the code of retrieving the xml data. i am also sending the tutorial where you will get the answer of your further question.
http://www.tbxml.co.uk/TBXML/API.html

my xmlDictionary its allway (null)

im trying to get the same value on xmlDictionary and dicionarioXML but, my dicionarioXML its allway (null), any help?
#synthesize xmlDictionary;
-(NSString*)buscaDados:(NSData*) dados
{
NSString * responseContent = [[NSString alloc] initWithBytes:[dados bytes] length:[dados length] encoding:NSUTF8StringEncoding];
NSError *parseError = nil;
xmlDictionary = [XMLReader dictionaryForXMLString:responseContent error:&parseError];
[responseContent release];
NSString* sucesso=[xmlDictionary valueForKeyPath:#"receitas.total.text"];
NSLog(#"xmlDictionary: %#",xmlDictionary);
return sucesso;
}
-(NSDictionary*)trataDados
{
NSDictionary* dicionarioXML = [self xmlDictionary];
NSLog(#"dicionarioXML: %#",dicionarioXML);
return dicionarioXML;
}
Null means something doesn't exist. What error are you getting back?
NSLog(#"Error: %#", parseError);

Add data to plist

I want to create a plist in code and add more "records" from user input to it. I have created a plist, and I can write the user input, but I can't add data to my plist. Every time, it saves the last input and keeps only one "record". This is the code:
-(void) createPlistFile {
manager = [NSFileManager defaultManager];
filepath = #"/Users/agnostos_el/Downloads/PhoneBook/PhoneBook/Epafes.txt";
if ( [manager fileExistsAtPath:filepath] == NO )
{
NSLog(#"Το Αρχειο Δεν Υπαρχει Το δημειουργω");
[[NSFileManager defaultManager]createFileAtPath:filepath contents:nil attributes:nil];
}
}
-(void) openPlistFile {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if( [path count] > 0 ){
pListFile = [[path objectAtIndex:0] stringByAppendingPathComponent:filepath];
NSDictionary *pListDict = [[NSDictionary alloc]initWithContentsOfFile:filepath];
if ( [pListDict count] <= 0 )
{
}
[self createPlistFile];
}
}
- (void) times
{
filepath = #"/Users/agnostos_el/Downloads/PhoneBook/PhoneBook/Epafes.txt";
stoixeia = [NSArray arrayWithObjects:onoma.stringValue, poli.stringValue, odos.stringValue, per.stringValue, tk.stringValue, xora.stringValue,kin.stringValue, kin1.stringValue, kin2.stringValue, kat.stringValue, erg.stringValue, fax.stringValue, email.stringValue, email1.stringValue, email2.stringValue, pros.stringValue, sim.stringValue, nil];
eponimos = epitheto.stringValue;
NSMutableDictionary *atomo = [NSMutableDictionary dictionary];
innerDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: eponimos, stoixeia, nil]
forKeys:[NSArray arrayWithObjects:#"eponimo", #"stoixeia", nil]];
[atomo setObject:innerDict forKey:eponimos];
id plist = [NSPropertyListSerialization dataFromPropertyList:(id)atomo
format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
record = [[NSData alloc]initWithData:plist];
[record writeToFile:filepath atomically:YES];
}
- (void) eggrafi
{
[self openPlistFile];
[self times];
}
Any suggestions? Thanks.
You are not passing the file contents to createFileAtPath:contents:attributes::
[[NSFileManager defaultManager]createFileAtPath:filepath contents:nil attributes:nil];