gpt4 book ai didi

ios - 共享扩展不显示数据 -- ios

转载 作者:可可西里 更新时间:2023-11-01 04:43:39 25 4
gpt4 key购买 nike

我正在尝试创建共享扩展。我将它们显示到 TableView 中。我的数据即将到来,但我看不到。它还卡住(就像我们有数据和弹出窗口但没有显示)。

这是我的示例代码

//
// ShareViewController.m
// myAppShare
//
//
//
//

#import "ShareViewController.h"
#import "MBProgressHUD.h"

@import MobileCoreServices;

static NSString *const AppGroupId = @"group.tag.BiblioHive7";

@implementation ShareViewController
{
NSArray *arrayCategory;
__weak IBOutlet UITextField *textFieldCategoryName;
__weak IBOutlet UIButton *btnPost;
__weak IBOutlet UIBarButtonItem *btnCancel;
__weak IBOutlet UINavigationItem *nayTitle;
__weak IBOutlet UINavigationBar *navBar;
}
-(void)viewDidLoad
{
sharedUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:AppGroupId];
[sharedUserDefaults setObject:@"hello" forKey:@"hello"];

NSString *userId= [sharedUserDefaults objectForKey:@"userId"];

NSLog(@"%@", [[sharedUserDefaults objectForKey:@"categories"] class]);
NSError* error;
[self didSelectPost];
if (userId==nil || [userId isEqualToString:@""] )
{
[self hideBackground];
self.view.hidden= YES;

UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Error"
message:@"Please login into myApp"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[self hideExtentionView];

}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

}
else
{
self.view.hidden= NO;

id jsonObject = [NSJSONSerialization JSONObjectWithData:[sharedUserDefaults objectForKey:@"categories"] options: NSJSONReadingMutableContainers error:&error];
arrayCategory= [NSArray arrayWithArray:jsonObject];

// here i am able to print object
NSLog(@"%@", [arrayCategory class]);

NSMutableArray *cityArray = [[NSMutableArray alloc] init];
NSMutableArray *testFeeds = [NSJSONSerialization JSONObjectWithData: [sharedUserDefaults objectForKey:@"categories"] options:NSJSONReadingMutableContainers error:nil];
[cityArray addObjectsFromArray:testFeeds];
NSLog(@"%lu", (unsigned long)[cityArray count]);

// [self.tableViewmyApp reloadData];
// here you must have to provide your app group id that you already created
}
}


-(void) hideBackground
{
[self.view setBackgroundColor:[UIColor clearColor]];
[self.tableViewmyApp setBackgroundColor:[UIColor clearColor]];
[textFieldCategoryName setBackgroundColor:[UIColor clearColor]];
[nayTitle setTitle:@""];
[btnPost setHidden:YES];
[navBar setHidden:YES];
}

- (BOOL)isContentValid {
// Do validation of contentText and/or NSExtensionContext attachments here
return YES;
}

- (void)didSelectPost {

// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.

// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.


inputItem = self.extensionContext.inputItems.firstObject;
NSItemProvider *urlItemProvider = [[inputItem.userInfo valueForKey:NSExtensionItemAttachmentsKey] objectAtIndex:0];

if ([urlItemProvider hasItemConformingToTypeIdentifier:(__bridge NSString *)kUTTypeURL])
{
[urlItemProvider loadItemForTypeIdentifier:(__bridge NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error)
{
if (error)
{
NSLog(@"Error occured");
}
else
{
if ([sharedUserDefaults objectForKey:@"userId"]==nil)
{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Error"
message:@"You must be logged In to share"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil];

[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];

return;

}


NSMutableArray *previousUrlsArray = [[NSMutableArray alloc]initWithArray:[sharedUserDefaults objectForKey:@"urlsArray"]];
if (previousUrlsArray==nil)
{
previousUrlsArray =[[NSMutableArray alloc]init];

}
[previousUrlsArray addObject:url.absoluteString];
NSMutableDictionary *dict= [[NSMutableDictionary alloc]init];
[dict setObject:previousUrlsArray forKey:@"urls"];
[sharedUserDefaults setObject:dict forKey:@"urlsDictionary"];

[sharedUserDefaults setObject:previousUrlsArray forKey:@"urlsArray"];
// [sharedUserDefaults synchronize];
[sharedUserDefaults setObject:url.absoluteString forKey:@"siteURL"];

[sharedUserDefaults setObject:[NSString stringWithFormat:@"%@",url.absoluteString] forKey:@"sURL"];
NSLog(@"%@",[sharedUserDefaults objectForKey:@"sURL"]);

// NSMutableArray *arrSites;
// if ([sharedUserDefaults valueForKey:@"SharedExtension"])
// arrSites = [sharedUserDefaults valueForKey:@"SharedExtension"];
// else
// arrSites = [[NSMutableArray alloc] init];
// NSDictionary *dictSite = [NSDictionary dictionaryWithObjectsAndKeys:self.contentText, @"Text", url.absoluteString, @"URL",nil];
// [arrSites addObject:dictSite];
// [sharedUserDefaults setObject:arrSites forKey:@"SharedExtension"];
// [sharedUserDefaults synchronize];

// UIAlertController * alert= [UIAlertController
// alertControllerWithTitle:@"Success"
// message:@"Posted Successfully."
// preferredStyle:UIAlertControllerStyleAlert];
//
// UIAlertAction* ok = [UIAlertAction
// actionWithTitle:@"OK"
// style:UIAlertActionStyleDefault
// handler:^(UIAlertAction * action)
// {
// [UIView animateWithDuration:0.20 animations:^
// {
// self.view.transform = CGAffineTransformMakeTranslation(0, self.view.frame.size.height);
// }
// completion:^(BOOL finished)
// {
// [self.extensionContext completeRequestReturningItems:nil completionHandler:nil];
// }];
// }];
//
// [alert addAction:ok];
// [self presentViewController:alert animated:YES completion:nil];
}
}];
}
}



- (NSArray *)configurationItems {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
return @[];
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

/*
* If the cell is nil it means no cell was available for reuse and that we should
* create a new one.
*/
if (cell == nil) {

/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] ;

cell.selectionStyle = UITableViewCellSelectionStyleDefault;

}
cell.textLabel.text=[[arrayCategory objectAtIndex:indexPath.row] valueForKey:@"category_name"];
return cell;
}


-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayCategory count];
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self saveInUserDefaultsWithCategoryName:[[arrayCategory objectAtIndex:indexPath.row] valueForKey:@"category_name"] andId:[[[arrayCategory objectAtIndex:indexPath.row] valueForKey:@"id"] integerValue]];

}
- (IBAction)postBtnTapped:(id)sender {

[self saveInUserDefaultsWithCategoryName:textFieldCategoryName.text andId:-1];

}

-(void) saveInUserDefaultsWithCategoryName : (NSString*) categoryName andId : (NSInteger) categoryId
{

if ([categoryName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length==0)
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Please choose a category or create a new one "
message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:okAction];

[self presentViewController:alert animated:YES completion:nil];

return;
}




NSString *stringId=[sharedUserDefaults objectForKey:@"id"];

([stringId length]>0) ? [sharedUserDefaults setObject:[NSString stringWithFormat:@"%@,%ld",stringId, (long)categoryId] forKey:@"id"] : [sharedUserDefaults setObject:[NSString stringWithFormat:@"%ld", (long)categoryId] forKey:@"id"];
NSLog(@"%@", [sharedUserDefaults objectForKey:@"id"]);

NSString *stringCategoryName=[sharedUserDefaults objectForKey:@"category_name"];

([stringCategoryName length]>0) ? [sharedUserDefaults setObject:[NSString stringWithFormat:@"%@,%@",stringCategoryName, categoryName] forKey:@"category_name"] : [sharedUserDefaults setObject:[NSString stringWithFormat:@"%@",categoryName] forKey:@"category_name"];
NSLog(@"%@", [sharedUserDefaults objectForKey:@"category_name"]);

NSString *stringURL=[sharedUserDefaults objectForKey:@"URL"];

([stringURL length]>0) ? [sharedUserDefaults setObject:[NSString stringWithFormat:@"%@,%@",stringURL, [sharedUserDefaults objectForKey:@"sURL"]] forKey:@"URL"] : [sharedUserDefaults setObject:[NSString stringWithFormat:@"%@",[sharedUserDefaults objectForKey:@"sURL"]] forKey:@"URL"];
NSLog(@"%@", [sharedUserDefaults objectForKey:@"URL"]);




NSDictionary *categoryDictionary = [NSDictionary dictionaryWithObjectsAndKeys:categoryName,@"categoryName", nil];
NSArray *categoryArray = [NSArray arrayWithObject:categoryDictionary];


NSMutableDictionary *mainDictionary = [[NSMutableDictionary alloc]init];
[mainDictionary setObject:categoryArray forKey:@"categories"];
[mainDictionary setObject:@"0" forKey:@"categoryType"];
[mainDictionary setObject:[sharedUserDefaults objectForKey:@"siteURL"] forKey:@"url"];
[mainDictionary setObject:[sharedUserDefaults objectForKey:@"access_token"] forKey:@"access_token"];

AppServices *appServices = [AppServices sharedInstance];
appServices.delegate = self;
[appServices postCategoriesOnServer:mainDictionary withSucessBlock:nil withfailureBlock:nil];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
// [self enableUserInteraction:NO];
// [[AppServices sharedInstance] sendData:nil withSucessBlock:nil withfailureBlock:nil];


// [sharedUserDefaults setObject:[NSNumber numberWithInteger:categoryId] forKey:@"id"];
// [sharedUserDefaults setObject:categoryName forKey:@"category_name"];




// NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
//
// [dict setObject:categoryName forKey:@"category_name"];
// [dict setObject:[NSNumber numberWithInteger:categoryId] forKey:@"id"];
// NSLog(@"%@", [sharedUserDefaults objectForKey:@"siteURL"]);
//
// [dict setObject:[sharedUserDefaults objectForKey:@"siteURL"] forKey:@"siteurl"];
//
//
// [sharedUserDefaults setObject:[self dictToJson:dict] forKey:@"categorydictionary"];
}

-(NSString *)dictToJson:(NSDictionary *)dict
{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: dict
options:(NSJSONWritingOptions) (/* DISABLES CODE */ (YES) ? NSJSONReadingMutableContainers : 0)
error:&error];

if (! jsonData) {
NSLog(@"bv_jsonStringWithPrettyPrint: error: %@", error.localizedDescription);
return @"{}";
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

}


- (IBAction)cancelBtnTapped:(id)sender {
[self hideExtentionView];
}

-(void) hideExtentionView
{
[self.extensionContext completeRequestReturningItems: nil completionHandler: nil];

}

-(void) showAlertViewWithMessage:(NSString *)message
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[self hideExtentionView];

}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

}

-(void)enableUserInteraction:(BOOL)enable
{
[self.view setUserInteractionEnabled:enable];
}

#pragma mark- AppServicesDelegate methods
-(void)showMessage:(NSString *)message
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
// [self enableUserInteraction:YES];
[self showAlertViewWithMessage:message];
}



@end

最佳答案

您的问题是您想要使用未使用键值保存的数组对象填充表格单元格 textLabel。在

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

您尝试获取尚未保存在该键上的对象的方法。

cell.textLabel.text=[[arrayCategory objectAtIndex:indexPath.row] valueForKey:@"category_name"];

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self saveInUserDefaultsWithCategoryName:[[arrayCategory objectAtIndex:indexPath.row] valueForKey:@"category_name"] andId:[[[arrayCategory objectAtIndex:indexPath.row] valueForKey:@"id"] integerValue]];
}

方法,这里保存对象。

这是一种不好的做法。

关于ios - 共享扩展不显示数据 -- ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com